From faf0190c084aa645ce0f4dad6cbb4717d02d2ab9 Mon Sep 17 00:00:00 2001 From: Chika Date: Sun, 20 Sep 2020 14:25:29 +0300 Subject: [PATCH] delete row in table feature --- .../Containers/Calculation/Sections/list.js | 3 ++ src/client/Elements/Table.jsx | 29 ++++++++++++++-- src/client/hocs/withStore.js | 13 +++++++- .../stores/CalculationStore/Data/tables.js | 10 +++--- src/core/config/initialTables.ts | 33 ++++++++++--------- src/core/types/stores.ts | 2 +- 6 files changed, 67 insertions(+), 23 deletions(-) diff --git a/src/client/Containers/Calculation/Sections/list.js b/src/client/Containers/Calculation/Sections/list.js index c82a906..f203e5e 100644 --- a/src/client/Containers/Calculation/Sections/list.js +++ b/src/client/Containers/Calculation/Sections/list.js @@ -1360,6 +1360,9 @@ export default [ Component: Table, props: { name: 'fruitTable', + features: { + canDeleteRow: true, + }, columns: [ { name: 'fruit', diff --git a/src/client/Elements/Table.jsx b/src/client/Elements/Table.jsx index ec9a5b5..015a177 100644 --- a/src/client/Elements/Table.jsx +++ b/src/client/Elements/Table.jsx @@ -1,9 +1,11 @@ import { withTableValue } from 'client/hocs/withStore'; import colors from 'client/UIKit/colors'; -import { Box } from 'client/UIKit/grid'; +import { Box, Flex } from 'client/UIKit/grid'; import React from 'react'; import styled from 'styled-components'; +import { Button as AntButton } from 'antd'; + const TableWrapper = styled(Box)` table { width: 100%; @@ -19,6 +21,10 @@ const TableWrapper = styled(Box)` border-bottom: 0; } } + transition: background 1s cubic-bezier(0.075, 0.82, 0.165, 1); + :hover { + background: ${colors.white[25]}; + } } th { @@ -47,7 +53,16 @@ const TableWrapper = styled(Box)` } `; -const Table = ({ name: tableName, columns, values }) => { +const DeleteRowButton = ({ onClick }) => ( + + + Удалить + + +); + +const Table = ({ name: tableName, columns, values, features, actions }) => { + const { canDeleteRow } = features; if (!values) { values = []; } @@ -59,6 +74,7 @@ const Table = ({ name: tableName, columns, values }) => { {columns.map(({ name, title }, ci) => { return {title}; })} + {canDeleteRow && } @@ -80,6 +96,15 @@ const Table = ({ name: tableName, columns, values }) => { ); })} + {canDeleteRow && ( + + { + actions.deleteRow(ri); + }} + /> + + )} ); })} diff --git a/src/client/hocs/withStore.js b/src/client/hocs/withStore.js index 90fbc6c..add003e 100644 --- a/src/client/hocs/withStore.js +++ b/src/client/hocs/withStore.js @@ -48,10 +48,21 @@ export const withStoreValue = Component => ({ export const withTableData = Table => props => { const { name: tableName } = props; + const ObservedTable = observer(Table); const TableWithStore = useObserver(() => { const { calculationStore } = useStores(); const tableData = calculationStore.tables[tableName]; - return ; + return ( + { + calculationStore.deleteTableRow(tableName, rowIndex); + }, + }} + /> + ); }); return () => TableWithStore; }; diff --git a/src/client/stores/CalculationStore/Data/tables.js b/src/client/stores/CalculationStore/Data/tables.js index bf1f3fd..9d49a7c 100644 --- a/src/client/stores/CalculationStore/Data/tables.js +++ b/src/client/stores/CalculationStore/Data/tables.js @@ -53,10 +53,12 @@ const tablesActions = { deleteTableRow(tableName, rowIndex) { const targetTable = this.tables[tableName]; - targetTable.values.splice(rowIndex, 1); - targetTable.options.splice(rowIndex, 1); - targetTable.filters.splice(rowIndex, 1); - targetTable.statuses.splice(rowIndex, 1); + if (!targetTable) { + throw new Error(`Table ${tableName} doesn't exist in store`); + } + if (targetTable.values) targetTable.values.splice(rowIndex, 1); + if (targetTable.filters) targetTable.filters.splice(rowIndex, 1); + if (targetTable.statuses) targetTable.statuses.splice(rowIndex, 1); }, cleanTable(tableName) { diff --git a/src/core/config/initialTables.ts b/src/core/config/initialTables.ts index 6ef14f5..7c3703d 100644 --- a/src/core/config/initialTables.ts +++ b/src/core/config/initialTables.ts @@ -1,20 +1,23 @@ import { IStoreTable } from './../types/tables'; const initialTables: IStoreTable = { - // fruitTable: { - // values: [ - // { - // fruit: 'apple', - // number: '5', - // }, - // { - // fruit: 'orange', - // number: '10', - // }, - // ], - // options: { - // fruit: [{ name: 'Apple', value: 'apple' }], - // }, - // }, + fruitTable: { + values: [ + { + fruit: 'apple', + number: '5', + }, + { + fruit: 'orange', + number: '10', + }, + ], + options: { + fruit: [ + { name: 'Apple', value: 'apple' }, + { name: 'Grusha', value: 'grusha' }, + ], + }, + }, }; export default initialTables; diff --git a/src/core/types/stores.ts b/src/core/types/stores.ts index 91853db..4175246 100644 --- a/src/core/types/stores.ts +++ b/src/core/types/stores.ts @@ -46,6 +46,6 @@ export interface ICalculationStore { values: { [prop in TableValuesNames]?: any }[], ) => void; - deleteTableRow: (tableName: string, rowIndex: number) => void; + deleteTableRow: (tableName: TableNames, rowIndex: number) => void; cleanTable: (tableName: TableNames) => void; }