From bbc383ecf8406b7b578f1e368626b0dd6a2752c0 Mon Sep 17 00:00:00 2001 From: Graham Lyus Date: Tue, 4 Sep 2018 13:23:58 -0700 Subject: [PATCH 1/2] add onColumnClick to Table --- source/Table/Table.jest.js | 24 ++++++++++++++++++++++++ source/Table/Table.js | 12 ++++++++++++ 2 files changed, 36 insertions(+) diff --git a/source/Table/Table.jest.js b/source/Table/Table.jest.js index 2fec94102..4e28223fa 100644 --- a/source/Table/Table.jest.js +++ b/source/Table/Table.jest.js @@ -702,6 +702,30 @@ describe('Table', () => { }); }); + describe('onColumnClick', () => { + it('should call :onColumnClick with the correct arguments when a column is clicked', () => { + const onColumnClick = jest.fn(); + const rendered = findDOMNode( + render( + getMarkup({ + onColumnClick, + }), + ), + ); + const nameColumn = rendered.querySelector( + '.ReactVirtualized__Table__rowColumn:first-of-type', + ); + + Simulate.click(nameColumn); + + expect(onColumnClick).toHaveBeenCalledTimes(1); + const params = onColumnClick.mock.calls[0][0]; + expect(params.dataKey).toEqual('name'); + expect(params.columnData.data).toEqual(123); + expect(params.event.type).toEqual('click'); + }); + }); + describe('onHeaderClick', () => { it('should call :onHeaderClick with the correct arguments when a column header is clicked and sorting is disabled', () => { const onHeaderClick = jest.fn(); diff --git a/source/Table/Table.js b/source/Table/Table.js index 4a06e460e..8fe47a9fe 100644 --- a/source/Table/Table.js +++ b/source/Table/Table.js @@ -84,6 +84,12 @@ export default class Table extends React.PureComponent { /** Optional renderer to be used in place of table body rows when rowCount is 0 */ noRowsRenderer: PropTypes.func, + /** + * Optional callback when a column is clicked. + * ({ columnData: any, dataKey: string }): void + */ + onColumnClick: PropTypes.func, + /** * Optional callback when a column's header is clicked. * ({ columnData: any, dataKey: string }): void @@ -426,6 +432,7 @@ export default class Table extends React.PureComponent { } _createColumn({column, columnIndex, isScrolling, parent, rowData, rowIndex}) { + const {onColumnClick} = this.props; const { cellDataGetter, cellRenderer, @@ -447,6 +454,10 @@ export default class Table extends React.PureComponent { rowIndex, }); + const onClick = event => { + onColumnClick && onColumnClick({columnData, dataKey, event}); + }; + const style = this._cachedColumnStyles[columnIndex]; const title = typeof renderedCell === 'string' ? renderedCell : null; @@ -459,6 +470,7 @@ export default class Table extends React.PureComponent { aria-describedby={id} className={cn('ReactVirtualized__Table__rowColumn', className)} key={'Row' + rowIndex + '-' + 'Col' + columnIndex} + onClick={onClick} role="gridcell" style={style} title={title}> From 995ad0280ec18e681d06815200b8210276d06fc2 Mon Sep 17 00:00:00 2001 From: Graham Lyus Date: Tue, 4 Sep 2018 13:45:10 -0700 Subject: [PATCH 2/2] Add documentation for onColumnClick --- docs/Table.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/Table.md b/docs/Table.md index 6598b9dc0..7df6440e9 100644 --- a/docs/Table.md +++ b/docs/Table.md @@ -22,6 +22,7 @@ This component expects explicit `width` and `height` parameters. | height | Number | ✓ | Fixed/available height for out DOM element | | id | String | | Optional custom id to attach to root `Table` element. | | noRowsRenderer | Function | | Callback used to render placeholder content when :rowCount is 0 | +| onColumnClick | Function | | Callback invoked when a user clicks on a table column. `({ columnData: any, dataKey: string, event: Event }): void` | | onHeaderClick | Function | | Callback invoked when a user clicks on a table header. `({ columnData: any, dataKey: string, event: Event }): void` | | onRowClick | Function | | Callback invoked when a user clicks on a table row. `({ event: Event, index: number, rowData: any }): void` | | onRowDoubleClick | Function | | Callback invoked when a user double-clicks on a table row. `({ event: Event, index: number, rowData: any }): void` |