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

fix(table): +reset page on predicate change, +unit tests #3538

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
14 changes: 10 additions & 4 deletions packages/mantine/src/components/table/TablePredicate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const useStyles = createStyles((theme) => ({
root: {},
wrapper: {},
label: {},
select: {}
select: {},
}));

type TablePredicateStylesNames = Selectors<typeof useStyles>;
Expand Down Expand Up @@ -40,10 +40,16 @@ export const TablePredicate: FunctionComponent<TablePredicateProps> = ({
...others
}) => {
const {classes} = useStyles(null, {name: 'TablePredicate', classNames, styles, unstyled});
const {form} = useTable();
const {form, setState} = useTable();

const onUpdate = (newValue: string) => {
const handleChange = (newValue: string) => {
form.setFieldValue('predicates', {...form.values.predicates, [id]: newValue});
setState((prevState) => ({
...prevState,
pagination: prevState.pagination
? {pageIndex: 0, pageSize: prevState.pagination.pageSize}
: prevState.pagination,
}));
};

return (
Expand All @@ -53,7 +59,7 @@ export const TablePredicate: FunctionComponent<TablePredicateProps> = ({
<Select
withinPortal
value={form.values.predicates[id]}
onChange={onUpdate}
onChange={handleChange}
data={data}
aria-label={label ?? id}
searchable={data.length > 7}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,35 @@ describe('Table.Predicate', () => {
});
expect(onChange).toHaveBeenCalledWith(expect.objectContaining({predicates: {rank: 'first'}}));
});

it('goes back to the first page when changing the predicate', async () => {
const user = userEvent.setup();
const onChange = vi.fn();
render(
<Table data={[{name: 'fruit'}, {name: 'vegetable'}]} columns={columns} onChange={onChange}>
<Table.Header>
<Table.Predicate
id="rank"
data={[
{value: 'first', label: 'First'},
{value: 'second', label: 'Second'},
]}
/>
</Table.Header>
<Table.Footer>
<Table.PerPage />
<Table.Pagination totalPages={2} />
</Table.Footer>
</Table>,
);
await user.click(screen.getByRole('searchbox', {name: 'rank'}));
await user.click(screen.getByRole('option', {name: 'First'}));
expect(screen.getByRole('searchbox', {name: 'rank'})).toHaveValue('First');
await waitFor(() => {
expect(onChange).toHaveBeenCalledTimes(1);
});
expect(onChange).toHaveBeenCalledWith(
expect.objectContaining({predicates: {rank: 'first'}, pagination: {pageIndex: 0, pageSize: 50}}),
);
});
});