Skip to content

feat(AnalyticalTable): add selectedRowIds object to onRowSelect event & improve performance when selecting rows #4534

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 5 commits into from
May 3, 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
101 changes: 82 additions & 19 deletions packages/main/src/components/AnalyticalTable/AnalyticalTable.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface PropTypes {
row?: Record<string, unknown>;
isSelected?: boolean;
selectedFlatRows: Record<string, unknown>[];
selectedRowIds: Record<string | number, boolean>;
}>
) => void;
}
Expand Down Expand Up @@ -290,14 +291,15 @@ describe('AnalyticalTable', () => {
filterable
columns={columns}
onRowSelect={(e) => {
const { allRowsSelected, isSelected, row, selectedFlatRows } = e.detail;
const { allRowsSelected, isSelected, row, selectedFlatRows, selectedRowIds } = e.detail;
setRelevantPayload({
allRowsSelected,
isSelected,
row: row.id,
selectedFlatRows: selectedFlatRows.map((item) => ({
id: item?.id
}))
})),
selectedRowIds
});
props.onRowSelect(e);
}}
Expand All @@ -307,6 +309,7 @@ describe('AnalyticalTable', () => {
/>
<div data-testid="payloadHelper">
{JSON.stringify(relevantPayload?.selectedFlatRows?.filter(Boolean).length)}
{JSON.stringify(relevantPayload?.selectedRowIds)}
</div>
</>
);
Expand All @@ -329,12 +332,12 @@ describe('AnalyticalTable', () => {
cy.get('@onRowSelectSpy').should('have.been.calledWithMatch', {
detail: { isSelected: true }
});
cy.findByTestId('payloadHelper').should('have.text', '1');
cy.findByTestId('payloadHelper').should('have.text', '1{"0.2":true}');
cy.findByText('Judith Mathews').click();
cy.get('@onRowSelectSpy').should('have.been.calledWithMatch', {
detail: { isSelected: true }
});
cy.findByTestId('payloadHelper').should('have.text', '2');
cy.findByTestId('payloadHelper').should('have.text', '2{"0.2":true,"0.2.0":true}');

// global filter + select
cy.findByTestId('input').typeIntoUi5Input('Katy Bradshaw');
Expand All @@ -345,7 +348,7 @@ describe('AnalyticalTable', () => {
detail: { isSelected: true }
});
cy.get('@onRowSelectSpy').should('have.been.calledThrice');
cy.findByTestId('payloadHelper').should('have.text', '3');
cy.findByTestId('payloadHelper').should('have.text', '3{"1":true,"0.2":true,"0.2.0":true}');

cy.findByTestId('input').typeIntoUi5Input('{selectall}{backspace}');

Expand All @@ -360,14 +363,15 @@ describe('AnalyticalTable', () => {
detail: { isSelected: true }
});
cy.get('@onRowSelectSpy').should('have.callCount', 4);
cy.findByTestId('payloadHelper').should('have.text', '4');
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);
const TestComp = ({ onRowSelect }: PropTypes) => {
const [selectedRowIds, setSelectedRowIds] = useState({});
const [selectedFlatRows, setSelectedFlatRows] = useState([]);
const [selectedRowIdsCb, setSelectedRowIdsCb] = useState({});
return (
<>
<Button onClick={() => setSelectedRowIds({ 2: true, 3: false })}>Set selected rows</Button>
Expand All @@ -376,12 +380,18 @@ describe('AnalyticalTable', () => {
columns={columns}
onRowSelect={(e) => {
setSelectedFlatRows(e.detail.selectedFlatRows.map((item) => item.id));
setSelectedRowIdsCb(e.detail.selectedRowIds);
onRowSelect(e);
}}
selectionMode={AnalyticalTableSelectionMode.MultiSelect}
selectedRowIds={selectedRowIds}
/>
"event.detail.selectedFlatRows:"<div data-testid="payload">{JSON.stringify(selectedFlatRows)}</div>
<p>
"event.detail.selectedFlatRows:"<span data-testid="payload">{JSON.stringify(selectedFlatRows)}</span>
</p>
<p>
"e.detail.selectedRowIds:"<span data-testid="payloadRowsById">{JSON.stringify(selectedRowIdsCb)}</span>
</p>
</>
);
};
Expand All @@ -393,12 +403,14 @@ describe('AnalyticalTable', () => {
cy.findByText('Name-5').click();
cy.findByText('Name-5').click();
cy.findByTestId('payload').should('have.text', '["0","1"]');
cy.findByTestId('payloadRowsById').should('have.text', '{"0":true,"1":true}');
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.findByTestId('payload').should('have.text', '["1","2"]');
cy.findByTestId('payloadRowsById').should('have.text', '{"1":true,"2":true,"3":false}');
});

it('row & header height', () => {
Expand Down Expand Up @@ -451,16 +463,6 @@ describe('AnalyticalTable', () => {
});

it('GroupBy selection', () => {
interface PropTypes {
onRowSelect: (
e?: CustomEvent<{
allRowsSelected: boolean;
row?: Record<string, unknown>;
isSelected?: boolean;
selectedFlatRows: Record<string, unknown>[];
}>
) => void;
}
const GroupBySelectTable = (props: PropTypes) => {
const { onRowSelect } = props;
const [relevantPayload, setRelevantPayload] = useState<Record<string, any>>({});
Expand All @@ -483,14 +485,15 @@ describe('AnalyticalTable', () => {
columns={columns}
tableInstance={tableInstance}
onRowSelect={(e) => {
const { allRowsSelected, isSelected, row, selectedFlatRows } = e.detail;
const { allRowsSelected, isSelected, row, selectedFlatRows, selectedRowIds } = e.detail;
setRelevantPayload({
allRowsSelected,
isSelected,
row: row.id,
selectedFlatRows: selectedFlatRows.map((item) => ({
id: item?.id
}))
})),
selectedRowIds
});
onRowSelect(e);
}}
Expand All @@ -500,6 +503,7 @@ describe('AnalyticalTable', () => {
<div data-testid="selectedFlatRowsLength">
{JSON.stringify(relevantPayload?.selectedFlatRows?.filter(Boolean).length)}
</div>
<div data-testid="selectedRowIds">{JSON.stringify(relevantPayload?.selectedRowIds)}</div>
<div data-testid="isSelected">{`${relevantPayload.isSelected}`}</div>
</>
);
Expand All @@ -510,6 +514,7 @@ describe('AnalyticalTable', () => {
cy.findByText('QWE').click();
cy.get('@onRowSelectSpy').should('have.callCount', 1);
cy.findByTestId('selectedFlatRowsLength').should('have.text', '1');
cy.findByTestId('selectedRowIds').should('have.text', '{"2":true}');
cy.findByTestId('isSelected').should('have.text', 'true');

cy.findByText('Friend Name').click();
Expand All @@ -519,11 +524,13 @@ describe('AnalyticalTable', () => {
cy.findByText('25').click();
cy.get('@onRowSelectSpy').should('have.callCount', 2);
cy.findByTestId('selectedFlatRowsLength').should('have.text', '2');
cy.findByTestId('selectedRowIds').should('have.text', '{"2":true,"4":true}');
cy.findByTestId('isSelected').should('have.text', 'true');

cy.findByText('25').click();
cy.get('@onRowSelectSpy').should('have.callCount', 3);
cy.findByTestId('selectedFlatRowsLength').should('have.text', '1');
cy.findByTestId('selectedRowIds').should('have.text', '{"2":true}');
cy.findByTestId('isSelected').should('have.text', 'false');
});

Expand Down Expand Up @@ -1791,6 +1798,62 @@ describe('AnalyticalTable', () => {
cy.get('[data-column-index="2"][data-row-index="3"]').children().should('have.text', 'Y');
});

it('select-all', () => {
const select = cy.spy().as('selectSpy');
const TestComp = () => {
const [stringifiedPl, setStringifiedPl] = useState('');
const handleSelect = (e) => {
const { allRowsSelected, selectedFlatRows, selectedRowIds } = e.detail;
setStringifiedPl(
JSON.stringify({
selectedRowIds,
selectedFlatRows: selectedFlatRows.map((item) => ({
id: item?.id
})),
allRowsSelected
})
);
select(e);
};
return (
<>
<AnalyticalTable
columns={columns}
data={data}
selectionMode={AnalyticalTableSelectionMode.MultiSelect}
onRowSelect={handleSelect}
/>
<span data-testid="payload">{stringifiedPl}</span>
</>
);
};
cy.mount(<TestComp />);
cy.get('[data-visible-column-index="0"][data-visible-row-index="0"]').click();
cy.get('@selectSpy').should('have.been.calledOnce');
cy.findByTestId('payload').should(
'have.text',
'{"selectedRowIds":{"0":true,"1":true,"2":true,"3":true},"selectedFlatRows":[{"id":"0"},{"id":"1"},{"id":"2"},{"id":"3"}],"allRowsSelected":true}'
);
cy.findByText('X').click();
cy.get('@selectSpy').should('have.been.calledTwice');
cy.findByTestId('payload').should(
'have.text',
'{"selectedRowIds":{"0":true,"1":true,"3":true},"selectedFlatRows":[{"id":"0"},{"id":"1"},{"id":"3"}],"allRowsSelected":false}'
);
cy.get('[data-visible-column-index="0"][data-visible-row-index="0"]').click();
cy.get('@selectSpy').should('have.been.calledThrice');
cy.findByTestId('payload').should(
'have.text',
'{"selectedRowIds":{"0":true,"1":true,"2":true,"3":true},"selectedFlatRows":[{"id":"0"},{"id":"1"},{"id":"2"},{"id":"3"}],"allRowsSelected":true}'
);
cy.get('[data-visible-column-index="0"][data-visible-row-index="0"]').click();
cy.get('@selectSpy').should('have.callCount', 4);
cy.findByTestId('payload').should(
'have.text',
'{"selectedRowIds":{},"selectedFlatRows":[],"allRowsSelected":false}'
);
});

cypressPassThroughTestsFactory(AnalyticalTable, { data, columns });
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,21 @@ const Cell = ({ row, webComponentsReactProperties: { selectionMode } }) => {
);
};

/*
* TABLE HOOKS
*/

const headerProps = (
props,
{
instance: {
flatRows,
webComponentsReactProperties: { onRowSelect, selectionMode },
toggleAllRowsSelected,
isAllRowsSelected
}
}
) => {
function getNextSelectedRowIds(rowsById) {
return Object.keys(rowsById).reduce((acc, cur) => {
acc[cur] = true;
return acc;
}, {});
}

const headerProps = (props, { instance }) => {
const {
flatRows,
webComponentsReactProperties: { onRowSelect, selectionMode },
toggleAllRowsSelected,
isAllRowsSelected,
rowsById
} = instance;
const style = { ...props.style, cursor: 'pointer', display: 'flex', justifyContent: 'center' };
if (
props.key === 'header___ui5wcr__internal_selection_column' &&
Expand All @@ -93,7 +93,8 @@ const headerProps = (
// cannot use instance.selectedFlatRows here as it only returns all rows on the first level
enrichEventWithDetails(e, {
allRowsSelected: !isAllRowsSelected,
selectedFlatRows: !isAllRowsSelected ? flatRows : []
selectedFlatRows: !isAllRowsSelected ? flatRows : [],
selectedRowIds: !isAllRowsSelected ? getNextSelectedRowIds(rowsById) : {}
})
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ export const useSelectionChangeCallback = (hooks) => {
row: row,
isSelected: row.isSelected,
selectedFlatRows: row.isSelected ? [row] : [],
allRowsSelected: false
allRowsSelected: false,
selectedRowIds
};

if (webComponentsReactProperties.selectionMode === AnalyticalTableSelectionMode.MultiSelect) {
const selectedRowIdsArray = Object.entries(selectedRowIds).reduce((acc, [key, val]) => {
if (val) {
return [...acc, key];
// 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 selectedRowIdsArrayMapped = Object.keys(selectedRowIds).reduce((acc, key) => {
if (selectedRowIds[key]) {
acc.push(tempRowsById[key]);
}
return acc;
}, []);
// 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 selectedRowIdsArrayMapped = selectedRowIdsArray.map((item) => tempRowsById[item]);

payload.selectedFlatRows = selectedRowIdsArrayMapped;
if (selectedRowIdsArrayMapped.length === Object.keys(tempRowsById).length) {
payload.allRowsSelected = true;
Expand Down
1 change: 1 addition & 0 deletions packages/main/src/components/AnalyticalTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ export interface AnalyticalTablePropTypes extends Omit<CommonProps, 'title'> {
row?: Record<string, unknown>;
isSelected?: boolean;
selectedFlatRows: Record<string, unknown>[];
selectedRowIds: Record<string | number, boolean>;
}>
) => void;
/**
Expand Down