Skip to content

fix(AnalyticalTable): return correct values in onRowSelect for filtered rows #4902

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

Merged
merged 1 commit into from
Jul 19, 2023
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
114 changes: 112 additions & 2 deletions packages/main/src/components/AnalyticalTable/AnalyticalTable.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -366,21 +366,43 @@ describe('AnalyticalTable', () => {
cy.findByTestId('payloadHelper').should('have.text', '4{"0":true,"1":true,"0.2":true,"0.2.0":true}');
});

it('programmatic and user selection', () => {
const data = generateMoreData(20);
it('programmatic and user selection + filtering', () => {
const data = [
...generateMoreData(20),
{
name: `Name-7`,
age: 22,
friend: {
name: `FriendName-X`,
age: 22 + 10
}
}
];
const TestComp = ({ onRowSelect }: PropTypes) => {
const [selectedRowIds, setSelectedRowIds] = useState({});
const [selectedFlatRows, setSelectedFlatRows] = useState([]);
const [selectedRowIdsCb, setSelectedRowIdsCb] = useState({});
const [allRowsSelected, setAllRowsSelected] = useState(false);
const [globalFilterVal, setGlobalFilterVal] = useState('');
return (
<>
<Button onClick={() => setSelectedRowIds({ 2: true, 3: false })}>Set selected rows</Button>
<input
data-testid="input"
value={globalFilterVal}
onInput={(e) => {
setGlobalFilterVal(e.target.value);
}}
/>
<AnalyticalTable
filterable
data={data}
columns={columns}
globalFilterValue={globalFilterVal}
onRowSelect={(e) => {
setSelectedFlatRows(e.detail.selectedFlatRows.map((item) => item.id));
setSelectedRowIdsCb(e.detail.selectedRowIds);
setAllRowsSelected(e.detail.allRowsSelected);
onRowSelect(e);
}}
selectionMode={AnalyticalTableSelectionMode.MultiSelect}
Expand All @@ -392,6 +414,10 @@ describe('AnalyticalTable', () => {
<p>
"e.detail.selectedRowIds:"<span data-testid="payloadRowsById">{JSON.stringify(selectedRowIdsCb)}</span>
</p>
<p>
"e.detail.allRowsSelected:"
<span data-testid="payloadAllRowsSelected">{`${allRowsSelected}`}</span>
</p>
</>
);
};
Expand All @@ -404,13 +430,97 @@ describe('AnalyticalTable', () => {
cy.findByText('Name-5').click();
cy.findByTestId('payload').should('have.text', '["0","1"]');
cy.findByTestId('payloadRowsById').should('have.text', '{"0":true,"1":true}');
cy.findByTestId('payloadAllRowsSelected').should('have.text', 'false');
cy.get('@onRowSelectSpy').should('have.callCount', 4);

cy.findByText('Set selected rows').click();
cy.get('@onRowSelectSpy').should('have.callCount', 4);
cy.findByText('Name-1').click();
cy.get('@onRowSelectSpy').should('have.callCount', 5);
cy.findByTestId('payload').should('have.text', '["1","2"]');
cy.findByTestId('payloadRowsById').should('have.text', '{"1":true,"2":true,"3":false}');
cy.findByTestId('payloadAllRowsSelected').should('have.text', 'false');

cy.get('[data-row-index="0"][data-column-index="0"]').click();
cy.get('@onRowSelectSpy').should('have.callCount', 6);
cy.findByTestId('payload').should(
'have.text',
'["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"]'
);
cy.findByTestId('payloadRowsById').should(
'have.text',
'{"0":true,"1":true,"2":true,"3":true,"4":true,"5":true,"6":true,"7":true,"8":true,"9":true,"10":true,"11":true,"12":true,"13":true,"14":true,"15":true,"16":true,"17":true,"18":true,"19":true,"20":true}'
);
cy.findByTestId('payloadAllRowsSelected').should('have.text', 'true');

cy.get('[data-row-index="0"][data-column-index="0"]').click();

cy.findByText('Name-0').click();
cy.findByText('Name-1').click();
cy.findByText('Name-5').click();
cy.findByText('Name').click();
cy.get('[ui5-li-custom]').shadow().get('[ui5-input]').typeIntoUi5Input('7{enter}');
cy.findByTestId('payload').should('have.text', '["0","1","5"]');
cy.findByTestId('payloadRowsById').should('have.text', '{"0":true,"1":true,"5":true}');
cy.findByTestId('payloadAllRowsSelected').should('have.text', 'false');

cy.get('[data-row-index="0"][data-column-index="0"]').click();
cy.get('@onRowSelectSpy').should('have.callCount', 11);
cy.findByTestId('payload').should('have.text', '["0","1","5","7","17","20"]');
cy.findByTestId('payloadRowsById').should('have.text', '{"0":true,"1":true,"5":true,"7":true,"17":true,"20":true}');
cy.findByTestId('payloadAllRowsSelected').should('have.text', 'false');

cy.findByText('Name').click();
cy.get('[ui5-li-custom]').shadow().get('[ui5-input]').typeIntoUi5Input('{selectall}{backspace}{enter}');
cy.get('[data-row-index="0"][data-column-index="0"]').click();
cy.findByText('Name-17').click({ force: true });
cy.findByText('Name').click();
cy.get('[ui5-li-custom]').shadow().get('[ui5-input]').typeIntoUi5Input('7{enter}');
cy.findByTestId('payload').should(
'have.text',
'["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","18","19","20"]'
);
cy.findByTestId('payloadRowsById').should(
'have.text',
'{"0":true,"1":true,"2":true,"3":true,"4":true,"5":true,"6":true,"7":true,"8":true,"9":true,"10":true,"11":true,"12":true,"13":true,"14":true,"15":true,"16":true,"18":true,"19":true,"20":true}'
);
cy.findByTestId('payloadAllRowsSelected').should('have.text', 'false');
cy.findByText('Name-17').click();
cy.findByTestId('payload').should(
'have.text',
'["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"]'
);
cy.findByTestId('payloadRowsById').should(
'have.text',
'{"0":true,"1":true,"2":true,"3":true,"4":true,"5":true,"6":true,"7":true,"8":true,"9":true,"10":true,"11":true,"12":true,"13":true,"14":true,"15":true,"16":true,"17":true,"18":true,"19":true,"20":true}'
);
cy.findByTestId('payloadAllRowsSelected').should('have.text', 'true');

cy.findByText('Name').click();
cy.get('[ui5-li-custom]').shadow().get('[ui5-input]').typeIntoUi5Input('{selectall}{backspace}{enter}');

cy.findByText('Name-17').click({ force: true });
cy.findByTestId('input').type('7{enter}');
cy.findByTestId('payload').should(
'have.text',
'["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","18","19","20"]'
);
cy.findByTestId('payloadRowsById').should(
'have.text',
'{"0":true,"1":true,"2":true,"3":true,"4":true,"5":true,"6":true,"7":true,"8":true,"9":true,"10":true,"11":true,"12":true,"13":true,"14":true,"15":true,"16":true,"18":true,"19":true,"20":true}'
);
cy.findByTestId('payloadAllRowsSelected').should('have.text', 'false');
cy.findByText('Name-17').click();
cy.findByTestId('payload').should(
'have.text',
'["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"]'
);
cy.findByTestId('payloadRowsById').should(
'have.text',
'{"0":true,"1":true,"2":true,"3":true,"4":true,"5":true,"6":true,"7":true,"8":true,"9":true,"10":true,"11":true,"12":true,"13":true,"14":true,"15":true,"16":true,"17":true,"18":true,"19":true,"20":true}'
);
cy.findByTestId('payloadAllRowsSelected').should('have.text', 'true');
cy.get('@onRowSelectSpy').should('have.callCount', 16);
});

it('row & header height', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ const headerProps = (props, { instance }) => {
webComponentsReactProperties: { onRowSelect, selectionMode },
toggleAllRowsSelected,
isAllRowsSelected,
rowsById
rowsById,
dispatch,
state: { filters, globalFilter }
} = instance;
const style = { ...props.style, cursor: 'pointer', display: 'flex', justifyContent: 'center' };
if (
Expand All @@ -71,15 +73,20 @@ const headerProps = (props, { instance }) => {
) {
const onClick = (e) => {
toggleAllRowsSelected(!isAllRowsSelected);
const isFiltered = filters?.length > 0 || !!globalFilter;
if (typeof onRowSelect === 'function') {
onRowSelect(
// cannot use instance.selectedFlatRows here as it only returns all rows on the first level
enrichEventWithDetails(e, {
allRowsSelected: !isAllRowsSelected,
selectedFlatRows: !isAllRowsSelected ? flatRows : [],
selectedRowIds: !isAllRowsSelected ? getNextSelectedRowIds(rowsById) : {}
})
);
if (isFiltered) {
dispatch({ type: 'SELECT_ROW_CB', payload: { event: e, row: undefined, selectAll: true, fired: true } });
} else {
onRowSelect(
// cannot use instance.selectedFlatRows here as it only returns all rows on the first level
enrichEventWithDetails(e, {
allRowsSelected: !isAllRowsSelected,
selectedFlatRows: !isAllRowsSelected ? flatRows : [],
selectedRowIds: !isAllRowsSelected ? getNextSelectedRowIds(rowsById) : {}
})
);
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@ import { AnalyticalTableSelectionMode } from '../../../enums/index.js';

export const useSelectionChangeCallback = (hooks) => {
hooks.useControlledState.push((state, { instance }) => {
const { selectedRowPayload, selectedRowIds } = state;
const { rowsById, preFilteredRowsById, webComponentsReactProperties, dispatch, filters } = instance;
const { selectedRowPayload, selectedRowIds, filters, globalFilter } = state;
const { rowsById, preFilteredRowsById, webComponentsReactProperties, dispatch } = instance;
const isFiltered = filters?.length > 0 || !!globalFilter;

// eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(() => {
if (selectedRowPayload?.fired) {
const { event: e, row: selRow } = selectedRowPayload;
const { event: e, row: selRow, selectAll } = selectedRowPayload;
const row = rowsById[selRow?.id];

if (row) {
if (row || selectAll) {
const payload = {
row: row,
isSelected: row.isSelected,
selectedFlatRows: row.isSelected ? [row] : [],
isSelected: row?.isSelected,
selectedFlatRows: row?.isSelected ? [row] : [],
allRowsSelected: false,
selectedRowIds
};

if (webComponentsReactProperties.selectionMode === AnalyticalTableSelectionMode.MultiSelect) {
// when selecting a row on a filtered table, `preFilteredRowsById` has to be used, otherwise filtered out rows are undefined
const tempRowsById = filters?.length > 0 ? preFilteredRowsById : rowsById;
const tempRowsById = isFiltered ? preFilteredRowsById : rowsById;
const selectedRowIdsArrayMapped = Object.keys(selectedRowIds).reduce((acc, key) => {
if (selectedRowIds[key]) {
acc.push(tempRowsById[key]);
Expand All @@ -35,12 +37,23 @@ export const useSelectionChangeCallback = (hooks) => {
if (selectedRowIdsArrayMapped.length === Object.keys(tempRowsById).length) {
payload.allRowsSelected = true;
}
if (selectAll) {
dispatch({ type: 'SELECT_ROW_CB', payload: { event: e, row, selectAll: false, fired: false } });
webComponentsReactProperties?.onRowSelect(
enrichEventWithDetails(e, {
selectedFlatRows: payload.selectedFlatRows,
allRowsSelected: payload.allRowsSelected,
selectedRowIds: payload.selectedRowIds
})
);
return;
}
}
dispatch({ type: 'SELECT_ROW_CB', payload: { event: e, row, fired: false } });
webComponentsReactProperties?.onRowSelect(enrichEventWithDetails(e, payload));
}
}
}, [selectedRowPayload?.fired, rowsById, webComponentsReactProperties.selectionMode, selectedRowIds]);
}, [selectedRowPayload?.fired, rowsById, webComponentsReactProperties.selectionMode, selectedRowIds, isFiltered]);

return state;
});
Expand Down