1
- import React , { FunctionComponent } from 'react'
1
+ import React , { FunctionComponent , useCallback , useMemo } from 'react'
2
2
import { connect } from 'react-redux'
3
3
import { Dispatch } from 'redux'
4
4
import { Table } from 'antd'
5
5
import { ColumnProps , TableProps , TableRowSelection } from 'antd/es/table'
6
- import ActionLink from '../../ui/ActionLink/ActionLink'
7
6
import { $do } from '../../../actions/actions'
8
7
import { Store } from '../../../interfaces/store'
9
8
import { PaginationMode , WidgetListField , WidgetTableMeta } from '../../../interfaces/widget'
@@ -20,11 +19,9 @@ import cn from 'classnames'
20
19
import Pagination from '../../ui/Pagination/Pagination'
21
20
import HierarchyTable from '../../../components/HierarchyTable/HierarchyTable'
22
21
import { BcFilter , FilterGroup } from '../../../interfaces/filters'
23
- import { useTranslation } from 'react-i18next'
24
22
import FullHierarchyTable from '../../../components/FullHierarchyTable/FullHierarchyTable'
25
- import { parseFilters } from '../../../utils/filters'
26
- import Select from '../../ui/Select/Select'
27
23
import RowOperationsButton from '../../RowOperations/RowOperationsButton'
24
+ import Header from './components/Header'
28
25
import { useRowMenu } from '../../../hooks/useRowMenu'
29
26
30
27
type AdditionalAntdTableProps = Partial < Omit < TableProps < DataItem > , 'rowSelection' > >
@@ -44,6 +41,9 @@ export interface TableWidgetOwnProps extends AdditionalAntdTableProps {
44
41
export interface TableWidgetProps extends TableWidgetOwnProps {
45
42
data : DataItem [ ]
46
43
rowMetaFields : RowMetaField [ ]
44
+ /**
45
+ * @deprecated TODO: Remove 2.0 as it is never used
46
+ */
47
47
limitBySelf : boolean
48
48
/**
49
49
* @deprecated TODO: Remove in 2.0 in favor of `widgetName`
@@ -69,7 +69,13 @@ export interface TableWidgetProps extends TableWidgetOwnProps {
69
69
* @deprecated TODO: Remove in 2.0.0 as it's moved to RowOperationsMenu
70
70
*/
71
71
metaInProgress ?: boolean
72
+ /**
73
+ * @deprecated TODO: Remove 2.0 as it is never used
74
+ */
72
75
filters : BcFilter [ ]
76
+ /**
77
+ * @deprecated TODO: Remove 2.0 as it is never used
78
+ */
73
79
filterGroups : FilterGroup [ ]
74
80
/**
75
81
* @deprecated TODO: Remove 2.0 as it is never used
@@ -86,8 +92,17 @@ export interface TableWidgetProps extends TableWidgetOwnProps {
86
92
*/
87
93
onSelectRow ?: ( bcName : string , cursor : string ) => void
88
94
onSelectCell : ( cursor : string , widgetName : string , fieldKey : string ) => void
95
+ /**
96
+ * @deprecated TODO: Remove 2.0 as it is never used
97
+ */
89
98
onRemoveFilters : ( bcName : string ) => void
99
+ /**
100
+ * @deprecated TODO: Remove 2.0 as it is never used
101
+ */
90
102
onApplyFilter : ( bcName : string , filter : BcFilter , widgetName ?: string ) => void
103
+ /**
104
+ * @deprecated TODO: Remove 2.0 as it is never used
105
+ */
91
106
onForceUpdate : ( bcName : string ) => void
92
107
}
93
108
@@ -141,24 +156,23 @@ export const TableWidget: FunctionComponent<TableWidgetProps> = props => {
141
156
}
142
157
}
143
158
const isAllowEdit = ( props . allowEdit ?? true ) && ! props . meta . options ?. readOnly
144
- const { t } = useTranslation ( )
145
159
146
- const processCellClick = React . useCallback (
160
+ const processCellClick = useCallback (
147
161
( recordId : string , fieldKey : string ) => {
148
162
onSelectCell ( recordId , widgetMetaName , fieldKey )
149
163
} ,
150
164
[ onSelectCell , widgetMetaName ]
151
165
)
152
166
153
- const processedFields : WidgetListField [ ] = React . useMemo (
167
+ const processedFields : WidgetListField [ ] = useMemo (
154
168
( ) =>
155
169
fields . map ( item => {
156
170
return item . type === FieldType . multivalue ? { ...item , type : FieldType . multivalueHover } : item
157
171
} ) ,
158
172
[ fields ]
159
173
)
160
174
161
- const columns : Array < ColumnProps < DataItem > > = React . useMemo ( ( ) => {
175
+ const columns : Array < ColumnProps < DataItem > > = useMemo ( ( ) => {
162
176
return processedFields
163
177
. filter ( item => item . type !== FieldType . hidden && ! item . hidden )
164
178
. map ( item => {
@@ -217,7 +231,7 @@ export const TableWidget: FunctionComponent<TableWidgetProps> = props => {
217
231
selectedCell
218
232
] )
219
233
220
- const resultColumns = React . useMemo ( ( ) => {
234
+ const resultColumns = useMemo ( ( ) => {
221
235
const controlColumnsLeft : Array < ColumnProps < DataItem > > = [ ]
222
236
const controlColumnsRight : Array < ColumnProps < DataItem > > = [ ]
223
237
props . controlColumns ?. map ( item => {
@@ -226,61 +240,10 @@ export const TableWidget: FunctionComponent<TableWidgetProps> = props => {
226
240
return [ ...controlColumnsLeft , ...columns , ...controlColumnsRight ]
227
241
} , [ columns , props . controlColumns ] )
228
242
229
- const [ filterGroupName , setFilterGroupName ] = React . useState ( null )
230
- const filtersExist = ! ! props . filters ?. length
231
-
232
- const handleShowAll = React . useCallback ( ( ) => {
233
- onShowAll ( widgetBcName , cursor , null , widgetName )
234
- } , [ onShowAll , widgetBcName , cursor , widgetName ] )
235
-
236
- const handleRemoveFilters = React . useCallback ( ( ) => {
237
- onRemoveFilters ( widgetBcName )
238
- onForceUpdate ( widgetBcName )
239
- } , [ onRemoveFilters , onForceUpdate , widgetBcName ] )
240
-
241
- const handleAddFilters = React . useMemo ( ( ) => {
242
- return ( value : string ) => {
243
- const filterGroup = filterGroups . find ( item => item . name === value )
244
- const parsedFilters = parseFilters ( filterGroup . filters )
245
- setFilterGroupName ( filterGroup . name )
246
- onRemoveFilters ( widgetBcName )
247
- parsedFilters . forEach ( item => onApplyFilter ( widgetBcName , item , widgetName ) )
248
- onForceUpdate ( widgetBcName )
249
- }
250
- } , [ filterGroups , widgetBcName , widgetName , setFilterGroupName , onRemoveFilters , onApplyFilter , onForceUpdate ] )
251
-
252
- React . useEffect ( ( ) => {
253
- if ( ! filtersExist ) {
254
- setFilterGroupName ( null )
255
- }
256
- } , [ filtersExist ] )
257
-
258
- const defaultHeader = React . useMemo ( ( ) => {
259
- return (
260
- < div className = { styles . filtersContainer } >
261
- { ! ! filterGroups ?. length && (
262
- < Select
263
- value = { filterGroupName ?? t ( 'Show all' ) . toString ( ) }
264
- onChange = { handleAddFilters }
265
- dropdownMatchSelectWidth = { false }
266
- >
267
- { filterGroups . map ( group => (
268
- < Select . Option key = { group . name } value = { group . name } >
269
- < span > { group . name } </ span >
270
- </ Select . Option >
271
- ) ) }
272
- </ Select >
273
- ) }
274
- { filtersExist && < ActionLink onClick = { handleRemoveFilters } > { t ( 'Clear all filters' ) } </ ActionLink > }
275
- { props . limitBySelf && < ActionLink onClick = { handleShowAll } > { t ( 'Show all records' ) } </ ActionLink > }
276
- </ div >
277
- )
278
- } , [ filterGroups , filterGroupName , filtersExist , props . limitBySelf , t , handleAddFilters , handleRemoveFilters , handleShowAll ] )
279
-
280
243
const [ operationsRef , parentRef , onRow ] = useRowMenu ( )
281
244
return (
282
245
< div className = { styles . tableContainer } ref = { parentRef } >
283
- { props . header ?? defaultHeader }
246
+ { props . header ?? < Header bcName = { widgetBcName } widgetName = { widgetName } /> }
284
247
< Table
285
248
className = { cn ( styles . table , { [ styles . tableWithRowMenu ] : props . showRowActions } ) }
286
249
columns = { resultColumns }
@@ -311,7 +274,13 @@ function mapStateToProps(store: Store, ownProps: TableWidgetOwnProps) {
311
274
return {
312
275
data : store . data [ ownProps . meta . bcName ] ,
313
276
rowMetaFields : fields ,
277
+ /**
278
+ * @deprecated
279
+ */
314
280
limitBySelf,
281
+ /**
282
+ * @deprecated
283
+ */
315
284
bcName,
316
285
/**
317
286
* @deprecated
@@ -324,7 +293,13 @@ function mapStateToProps(store: Store, ownProps: TableWidgetOwnProps) {
324
293
* @deprecated
325
294
*/
326
295
pendingDataItem : null as PendingDataItem ,
296
+ /**
297
+ * @deprecated
298
+ */
327
299
filters,
300
+ /**
301
+ * @deprecated
302
+ */
328
303
filterGroups : bc ?. filterGroups
329
304
}
330
305
}
@@ -334,19 +309,31 @@ function mapDispatchToProps(dispatch: Dispatch) {
334
309
onSelectCell : ( cursor : string , widgetName : string , fieldKey : string ) => {
335
310
dispatch ( $do . selectTableCellInit ( { widgetName, rowId : cursor , fieldKey } ) )
336
311
} ,
312
+ /**
313
+ * @deprecated TODO: Remove in 2.0
314
+ */
337
315
onShowAll : ( bcName : string , cursor : string , route ?: Route ) => {
338
316
dispatch ( $do . showAllTableRecordsInit ( { bcName, cursor } ) )
339
317
} ,
340
318
/**
341
319
* @deprecated TODO: Remove in 2.0
342
320
*/
343
321
onDrillDown : null as ( widgetName : string , cursor : string , bcName : string , fieldKey : string ) => void ,
322
+ /**
323
+ * @deprecated TODO: Remove in 2.0
324
+ */
344
325
onRemoveFilters : ( bcName : string ) => {
345
326
dispatch ( $do . bcRemoveAllFilters ( { bcName } ) )
346
327
} ,
328
+ /**
329
+ * @deprecated TODO: Remove in 2.0
330
+ */
347
331
onApplyFilter : ( bcName : string , filter : BcFilter , widgetName ?: string ) => {
348
332
dispatch ( $do . bcAddFilter ( { bcName, filter, widgetName } ) )
349
333
} ,
334
+ /**
335
+ * @deprecated TODO: Remove in 2.0
336
+ */
350
337
onForceUpdate : ( bcName : string ) => {
351
338
dispatch ( $do . bcForceUpdate ( { bcName } ) )
352
339
}
0 commit comments