Skip to content

Commit ca98b00

Browse files
authored
Merge pull request #11371 from Sophran-fbj/fix-prevnextbuttons-store-key-false
Fix `<PrevNextButtons storeKey={false}>` being ignored
2 parents 6f5a30e + 9103134 commit ca98b00

4 files changed

Lines changed: 122 additions & 18 deletions

File tree

‎docs/PrevNextButtons.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,9 @@ export const MyAdmin = () => (
301301
`<PrevNextButtons>` can get the current list parameters (sort and filters) from the store.
302302
This prop is useful if you specified a custom `storeKey` for a `<List>` and you want `<PrevNextButtons>` to use the same stored parameters.
303303

304-
See [`storeKey` in `<List>`](./List.md#storekey) for more information.
304+
If you don't want `<PrevNextButtons>` to read the parameters stored by the `<List>` (for instance, to let users browse a thread of messages from a `<Show>` view regardless of how they last filtered the messages list), pass `false`: the pager will then rely only on its own `sort`, `filter` and `filterDefaultValues` props.
305+
306+
See [`storeKey` in `<List>`](./List.md#storekey) for more information.
305307

306308
```tsx
307309
export const MyAdmin = () => (

‎packages/ra-core/src/controller/usePrevNextController.ts‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,26 +130,31 @@ export const usePrevNextController = <RecordType extends RaRecord = any>(
130130
);
131131
}
132132

133+
const defaultParams: ListParams = {
134+
filter: filterDefaultValues,
135+
order: initialSort.order,
136+
sort: initialSort.field,
137+
page: 1,
138+
perPage: 10,
139+
displayedFilters: {},
140+
};
141+
// As we can't conditionally call a hook, if the storeKey is false,
142+
// we'll ignore the storedParams variable later on and use the
143+
// props-derived defaults instead.
133144
const [storedParams] = useStore<ListParams>(
134145
storeKey || `${resource}.listParams`,
135-
{
136-
filter: filterDefaultValues,
137-
order: initialSort.order,
138-
sort: initialSort.field,
139-
page: 1,
140-
perPage: 10,
141-
displayedFilters: {},
142-
}
146+
defaultParams
143147
);
148+
const listParams = storeKey === false ? defaultParams : storedParams;
144149

145150
const dataProvider = useDataProvider();
146151
const queryClient = useQueryClient();
147152
const pagination = { page: 1, perPage: limit };
148153
const sort = {
149-
field: storedParams.sort,
150-
order: storedParams.order,
154+
field: listParams.sort,
155+
order: listParams.order,
151156
};
152-
const filter = { ...storedParams.filter, ...permanentFilter };
157+
const filter = { ...listParams.filter, ...permanentFilter };
153158
const { meta, ...otherQueryOptions } = queryOptions;
154159
const params = { pagination, sort, filter, meta };
155160

@@ -163,20 +168,20 @@ export const usePrevNextController = <RecordType extends RaRecord = any>(
163168
{
164169
...params,
165170
pagination: {
166-
page: storedParams.page,
167-
perPage: storedParams.perPage,
171+
page: listParams.page,
172+
perPage: listParams.perPage,
168173
},
169174
},
170175
]);
171176
const recordIndexInQueryData = queryData?.data?.findIndex(
172177
r => r.id === record?.id
173178
);
174179
const isRecordIndexFirstInNonFirstPage =
175-
recordIndexInQueryData === 0 && storedParams.page > 1;
180+
recordIndexInQueryData === 0 && listParams.page > 1;
176181
const isRecordIndexLastInNonLastPage =
177182
queryData?.data && queryData?.total
178183
? recordIndexInQueryData === queryData?.data?.length - 1 &&
179-
storedParams.page < queryData?.total / storedParams.perPage
184+
listParams.page < queryData?.total / listParams.perPage
180185
: undefined;
181186
const canUseCacheData =
182187
record &&
@@ -249,8 +254,7 @@ export const usePrevNextController = <RecordType extends RaRecord = any>(
249254
? undefined
250255
: index +
251256
(canUseCacheData
252-
? (storedParams.perPage ?? 0) *
253-
((storedParams.page ?? 1) - 1)
257+
? (listParams.perPage ?? 0) * ((listParams.page ?? 1) - 1)
254258
: 0),
255259
total: canUseCacheData ? queryData?.total : data?.total,
256260
error,

‎packages/ra-ui-materialui/src/button/PrevNextButtons.spec.tsx‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
WithFilter,
1010
WithLimit,
1111
WithQueryFilter,
12+
WithoutStoreKey,
1213
} from './PrevNextButtons.stories';
1314

1415
describe('<PrevNextButtons />', () => {
@@ -109,6 +110,43 @@ describe('<PrevNextButtons />', () => {
109110
});
110111
});
111112

113+
describe('storeKey', () => {
114+
it('should ignore the stored list params when storeKey is false', async () => {
115+
const data = {
116+
customers: Array.from(Array(900).keys()).map(id => {
117+
const first_name = `first_name_${id}`;
118+
const last_name = `last_name_${id}`;
119+
const email = `first_name_${id}.last_name_${id}@example.com`;
120+
121+
return {
122+
id,
123+
first_name,
124+
last_name,
125+
email,
126+
city: `city_${Math.floor(id / 50)}`,
127+
};
128+
}),
129+
};
130+
const dataProvider = fakeRestDataProvider(data);
131+
const spy = jest.spyOn(dataProvider, 'getList');
132+
render(<WithoutStoreKey customDataProvider={dataProvider} />);
133+
const input = await screen.findByLabelText('Search');
134+
fireEvent.change(input, { target: { value: 'city_0' } });
135+
await screen.findByText('1-10 of 50');
136+
const item = await screen.findByText('first_name_9');
137+
fireEvent.click(item);
138+
await screen.findByRole('navigation');
139+
await screen.findByText('11 / 900');
140+
expect(spy).toHaveBeenCalledWith('customers', {
141+
pagination: { page: 1, perPage: 1000 },
142+
sort: { field: 'first_name', order: 'DESC' },
143+
filter: {},
144+
meta: undefined,
145+
signal: undefined,
146+
});
147+
});
148+
});
149+
112150
describe('limit', () => {
113151
it('should render the total number of items, even with a limit', async () => {
114152
render(<WithLimit />);

‎packages/ra-ui-materialui/src/button/PrevNextButtons.stories.tsx‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,66 @@ export const WithStoreKey = () => (
156156
</TestMemoryRouter>
157157
);
158158

159+
export const WithoutStoreKey = ({ customDataProvider = dataProvider }: any) => (
160+
<TestMemoryRouter>
161+
<AdminContext
162+
dataProvider={customDataProvider}
163+
i18nProvider={i18nProvider}
164+
>
165+
<AdminUI>
166+
<Resource
167+
name="customers"
168+
list={
169+
<ListGuesser
170+
filters={[
171+
<TextInput
172+
label="Search"
173+
source="q"
174+
alwaysOn
175+
key="q"
176+
/>,
177+
]}
178+
/>
179+
}
180+
edit={
181+
<CustomerEdit
182+
actions={
183+
<MyTopToolbar>
184+
<PrevNextButtons
185+
storeKey={false}
186+
sort={{
187+
field: 'first_name',
188+
order: 'DESC',
189+
}}
190+
/>
191+
<ShowButton />
192+
</MyTopToolbar>
193+
}
194+
/>
195+
}
196+
show={
197+
<CustomerShow
198+
actions={
199+
<MyTopToolbar>
200+
<PrevNextButtons
201+
linkType="show"
202+
storeKey={false}
203+
sort={{
204+
field: 'first_name',
205+
order: 'DESC',
206+
}}
207+
/>
208+
<EditButton />
209+
</MyTopToolbar>
210+
}
211+
/>
212+
}
213+
/>
214+
</AdminUI>
215+
</AdminContext>
216+
</TestMemoryRouter>
217+
);
218+
159219
export const WithFilter = () => (
160220
<TestMemoryRouter>
161221
<AdminContext dataProvider={dataProvider} i18nProvider={i18nProvider}>

0 commit comments

Comments
 (0)