Skip to content

Commit 18cb960

Browse files
Merge pull request #1396 from vtex/feature/handle-toggle-columns
Add handle callback on toggle fields visibility
2 parents 4b386f2 + cc09514 commit 18cb960

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Possibility to get callback on toggle fields visibility with `onToggleColumn`, `onHideAllColumns` and `onShowAllColumns`
13+
1014
## [9.143.0] - 2021-06-16
1115

1216
### Fixed
1317

1418
- **Modal** sometimes not loading `window.scroll` polyfill after the browser hard refresh.
1519

16-
### Added
20+
### Added
1721

1822
- **Dropdown** `disabled` prop added to options
1923

react/components/Table/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,12 @@ class ResourceListExample extends React.Component {
790790
label: 'Toggle visible fields',
791791
showAllLabel: 'Show All',
792792
hideAllLabel: 'Hide All',
793+
onToggleColumn: (params) => {
794+
console.log(params.toggledField)
795+
console.log(params.activeFields)
796+
},
797+
onHideAllColumns: (activeFields) => console.log(activeFields),
798+
onShowAllColumns: (activeFields) => console.log(activeFields),
793799
},
794800
extraActions: {
795801
label: 'More options',
@@ -1714,6 +1720,12 @@ class ResourceListExample extends React.Component {
17141720
label: 'Toggle visible fields',
17151721
showAllLabel: 'Show All',
17161722
hideAllLabel: 'Hide All',
1723+
onToggleColumn: (params) => {
1724+
console.log(params.toggledField)
1725+
console.log(params.activeFields)
1726+
},
1727+
onHideAllColumns: (activeFields) => console.log(activeFields),
1728+
onShowAllColumns: (activeFields) => console.log(activeFields),
17171729
},
17181730
extraActions: {
17191731
label: 'More options',

react/components/Table/index.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import PropTypes from 'prop-types'
44
import reduce from 'lodash/reduce'
55
import map from 'lodash/map'
66
import isEmpty from 'lodash/isEmpty'
7+
import difference from 'lodash/difference'
78

89
import Box from '../Box'
910
import EmptyState from '../EmptyState'
@@ -81,14 +82,39 @@ class Table extends PureComponent {
8182
const index = hiddenFields.indexOf(key)
8283
index === -1 ? newFieldsArray.push(key) : newFieldsArray.splice(index, 1)
8384
this.setState({ hiddenFields: newFieldsArray })
85+
86+
const { toolbar } = this.props
87+
88+
const activeFields = difference(
89+
Object.keys(this.props.schema.properties),
90+
newFieldsArray
91+
)
92+
93+
if (toolbar?.fields?.onToggleColumn) {
94+
toolbar.fields.onToggleColumn({ toggledField: key, activeFields })
95+
}
8496
}
8597

8698
handleShowAllColumns = () => {
8799
this.setState({ hiddenFields: [] })
100+
101+
const { toolbar } = this.props
102+
103+
if (toolbar?.fields?.onShowAllColumns) {
104+
toolbar.fields.onShowAllColumns(Object.keys(this.props.schema.properties))
105+
}
88106
}
89107

90108
handleHideAllColumns = () => {
91-
this.setState({ hiddenFields: Object.keys(this.props.schema.properties) })
109+
const newFieldsArray = Object.keys(this.props.schema.properties)
110+
111+
this.setState({ hiddenFields: newFieldsArray })
112+
113+
const { toolbar } = this.props
114+
115+
if (toolbar?.fields?.onHideAllColumns) {
116+
toolbar.fields.onHideAllColumns([])
117+
}
92118
}
93119

94120
handleSelectionChange = () => {
@@ -441,6 +467,9 @@ Table.propTypes = {
441467
showAllLabel: PropTypes.string,
442468
hideAllLabel: PropTypes.string,
443469
alignMenu: PropTypes.oneOf(['right', 'left']),
470+
onToggleColumn: PropTypes.func,
471+
onHideAllColumns: PropTypes.func,
472+
onShowAllColumns: PropTypes.func,
444473
}),
445474
download: PropTypes.shape({
446475
label: PropTypes.string,

0 commit comments

Comments
 (0)