Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onTableChange callback function to EuiInMemoryTable #1060

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## [`master`](https://github.com/elastic/eui/tree/master)

- Added `onTableChange` callback to `EuiInMemoryTable` which notifies on sorting and pagination changes. ([#1060](https://github.com/elastic/eui/pull/1060))
- `EuiComboBox` now applies the provided `data-test-subj` to its options list element with the suffix `-optionsList` so you can find a specific combo box instance's options list. This wasn't previously possible because the options list is attached to the body element, not the combo box element. This is in addition to the existing `data-test-subj="comboBoxOptionsList"`. ([#1054](https://github.com/elastic/eui/pull/1054))

**Bug fixes**
Expand Down
7 changes: 7 additions & 0 deletions src-docs/src/views/tables/in_memory/props_info.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ export const propsInfo = {
type: { name: 'boolean | #Search' }
},
selection: basicPropsInfo.EuiBasicTable.__docgenInfo.props.selection,
onTableChange: {
description: `Callback for when table pagination or sorting is changed. This is meant to
be informational only, and not used to set any state as the in-memory table already
manages this state.`,
required: false,
type: { name: 'function' }
},
}
}
},
Expand Down
20 changes: 20 additions & 0 deletions src/components/basic_table/__snapshots__/basic_table.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ exports[`EuiBasicTable cellProps renders cells with custom props from a callback
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
data-test-subj="tableHeaderCell_name_0"
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -94,6 +95,7 @@ exports[`EuiBasicTable cellProps renders rows with custom props from an object 1
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
data-test-subj="tableHeaderCell_name_0"
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -178,6 +180,7 @@ exports[`EuiBasicTable empty is rendered 1`] = `
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
data-test-subj="tableHeaderCell_name_0"
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -213,6 +216,7 @@ exports[`EuiBasicTable empty renders a node as a custom message 1`] = `
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
data-test-subj="tableHeaderCell_name_0"
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -256,6 +260,7 @@ exports[`EuiBasicTable empty renders a string as a custom message 1`] = `
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
data-test-subj="tableHeaderCell_name_0"
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -291,6 +296,7 @@ exports[`EuiBasicTable itemIdToExpandedRowMap renders an expanded row 1`] = `
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
data-test-subj="tableHeaderCell_name_0"
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -379,6 +385,7 @@ exports[`EuiBasicTable rowProps renders rows with custom props from a callback 1
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
data-test-subj="tableHeaderCell_name_0"
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -461,6 +468,7 @@ exports[`EuiBasicTable rowProps renders rows with custom props from an object 1`
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
data-test-subj="tableHeaderCell_name_0"
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -543,6 +551,7 @@ exports[`EuiBasicTable with pagination - 2nd page 1`] = `
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
data-test-subj="tableHeaderCell_name_0"
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -611,6 +620,7 @@ exports[`EuiBasicTable with pagination 1`] = `
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
data-test-subj="tableHeaderCell_name_0"
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -695,6 +705,7 @@ exports[`EuiBasicTable with pagination and error 1`] = `
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
data-test-subj="tableHeaderCell_name_0"
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -752,6 +763,7 @@ exports[`EuiBasicTable with pagination and selection 1`] = `
</EuiTableHeaderCellCheckbox>
<EuiTableHeaderCell
align="left"
data-test-subj="tableHeaderCell_name_0"
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -908,6 +920,7 @@ exports[`EuiBasicTable with pagination, selection and sorting 1`] = `
</EuiTableHeaderCellCheckbox>
<EuiTableHeaderCell
align="left"
data-test-subj="tableHeaderCell_name_0"
isSortAscending={true}
isSorted={true}
key="_data_h_name_0"
Expand Down Expand Up @@ -1067,6 +1080,7 @@ exports[`EuiBasicTable with pagination, selection, sorting and a single record a
</EuiTableHeaderCellCheckbox>
<EuiTableHeaderCell
align="left"
data-test-subj="tableHeaderCell_name_0"
isSortAscending={true}
isSorted={true}
key="_data_h_name_0"
Expand Down Expand Up @@ -1317,6 +1331,7 @@ exports[`EuiBasicTable with pagination, selection, sorting and column dataType 1
</EuiTableHeaderCellCheckbox>
<EuiTableHeaderCell
align="right"
data-test-subj="tableHeaderCell_count_0"
isSortAscending={true}
isSorted={true}
key="_data_h_count_0"
Expand Down Expand Up @@ -1476,6 +1491,7 @@ exports[`EuiBasicTable with pagination, selection, sorting and column renderer 1
</EuiTableHeaderCellCheckbox>
<EuiTableHeaderCell
align="left"
data-test-subj="tableHeaderCell_name_0"
isSortAscending={true}
isSorted={true}
key="_data_h_name_0"
Expand Down Expand Up @@ -1635,6 +1651,7 @@ exports[`EuiBasicTable with pagination, selection, sorting and multiple record a
</EuiTableHeaderCellCheckbox>
<EuiTableHeaderCell
align="left"
data-test-subj="tableHeaderCell_name_0"
isSortAscending={true}
isSorted={true}
key="_data_h_name_0"
Expand Down Expand Up @@ -1879,6 +1896,7 @@ exports[`EuiBasicTable with pagination, selection, sorting, column renderer and
</EuiTableHeaderCellCheckbox>
<EuiTableHeaderCell
align="right"
data-test-subj="tableHeaderCell_count_0"
isSortAscending={true}
isSorted={true}
key="_data_h_count_0"
Expand Down Expand Up @@ -2008,6 +2026,7 @@ exports[`EuiBasicTable with sortable columns and sorting disabled 1`] = `
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
data-test-subj="tableHeaderCell_name_0"
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -2095,6 +2114,7 @@ exports[`EuiBasicTable with sorting 1`] = `
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
data-test-subj="tableHeaderCell_name_0"
isSortAscending={true}
isSorted={true}
key="_data_h_name_0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ exports[`EuiInMemoryTable behavior pagination 1`] = `
<tr>
<EuiTableHeaderCell
align="left"
data-test-subj="tableHeaderCell_name_0"
key="_data_h_name_0"
scope="col"
>
<th
className="euiTableHeaderCell"
data-test-subj="tableHeaderCell_name_0"
scope="col"
>
<div
Expand Down
1 change: 1 addition & 0 deletions src/components/basic_table/basic_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ export class EuiBasicTable extends Component {
width={width}
isMobileHeader={isMobileHeader}
hideForMobile={hideForMobile}
data-test-subj={`tableHeaderCell_${field}_${index}`}
{...sorting}
>
{name}
Expand Down
6 changes: 6 additions & 0 deletions src/components/basic_table/in_memory_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const InMemoryTablePropTypes = {
itemId: ItemIdType,
rowProps: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
cellProps: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
onTableChange: PropTypes.func,
};

const getInitialQuery = (search) => {
Expand Down Expand Up @@ -172,6 +173,10 @@ export class EuiInMemoryTable extends Component {
}

onTableChange = ({ page = {}, sort = {} }) => {
if (this.props.onTableChange) {
this.props.onTableChange({ page, sort });
}

const {
index: pageIndex,
size: pageSize
Expand Down Expand Up @@ -310,6 +315,7 @@ export class EuiInMemoryTable extends Component {
cellProps,
items: _unuseditems, // eslint-disable-line no-unused-vars
search, // eslint-disable-line no-unused-vars
onTableChange, // eslint-disable-line no-unused-vars
...rest
} = this.props;

Expand Down
54 changes: 54 additions & 0 deletions src/components/basic_table/in_memory_table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,5 +670,59 @@ describe('EuiInMemoryTable', () => {

expect(component).toMatchSnapshot();
});

test('onTableChange callback', () => {
const props = {
...requiredProps,
items: [
{ id: '1', name: 'name1' },
{ id: '2', name: 'name2' },
{ id: '3', name: 'name3' },
{ id: '4', name: 'name4' },
],
columns: [
{
field: 'name',
name: 'Name',
description: 'description',
sortable: true,
}
],
sorting: true,
pagination: {
pageSizeOptions: [2, 4, 6],
},
onTableChange: jest.fn(),
};

const component = mount(
<EuiInMemoryTable {...props}/>
);

expect(props.onTableChange).toHaveBeenCalledTimes(0);
component.find('EuiPaginationButton[data-test-subj="pagination-button-1"]').simulate('click');
expect(props.onTableChange).toHaveBeenCalledTimes(1);
expect(props.onTableChange).toHaveBeenCalledWith({
sort: {},
page: {
index: 1,
size: 2,
},
});

props.onTableChange.mockClear();
component.find('[data-test-subj*="tableHeaderCell_name_0"] [data-test-subj="tableHeaderSortButton"]').simulate('click');
expect(props.onTableChange).toHaveBeenCalledTimes(1);
expect(props.onTableChange).toHaveBeenCalledWith({
sort: {
direction: 'asc',
field: 'name',
},
page: {
index: 0,
size: 2,
},
});
});
});
});
1 change: 1 addition & 0 deletions src/components/table/table_header_cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const EuiTableHeaderCell = ({
className={buttonClasses}
onClick={onSort}
aria-label={statefulAriaLabel}
data-test-subj="tableHeaderSortButton"
>
<span className={contentClasses}>
<span className="euiTableCellContent__text">{children}</span>
Expand Down