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 useList pagination total #6500

Merged
merged 3 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions packages/ra-core/src/controller/useList.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('<useList />', () => {
},
ids: [2],
error: undefined,
total: 1,
})
);
});
Expand Down Expand Up @@ -90,6 +91,7 @@ describe('<useList />', () => {
},
ids: [1, 3, 4],
error: undefined,
total: 3,
})
);
});
Expand Down Expand Up @@ -138,6 +140,7 @@ describe('<useList />', () => {
},
ids: [2, 1],
error: undefined,
total: 2,
})
);
});
Expand All @@ -155,6 +158,7 @@ describe('<useList />', () => {
},
ids: [1, 2],
error: undefined,
total: 2,
})
);
});
Expand Down Expand Up @@ -200,6 +204,7 @@ describe('<useList />', () => {
page: 2,
perPage: 5,
error: undefined,
total: 7,
})
);
});
Expand Down
9 changes: 7 additions & 2 deletions packages/ra-core/src/controller/useList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ export const useList = (props: UseListOptions): UseListValue => {
const [finalItems, setFinalItems] = useSafeSetState<{
data: RecordMap;
ids: Identifier[];
total: number;
}>(() => ({
data: indexById(data),
ids,
total: 0,
yksflip marked this conversation as resolved.
Show resolved Hide resolved
}));

// pagination logic
Expand Down Expand Up @@ -174,6 +176,9 @@ export const useList = (props: UseListOptions): UseListValue => {
return result;
})
);

const filteredLength = tempData.length;

// 2. sort
if (sort.field) {
tempData = tempData.sort((a, b) => {
Expand All @@ -192,10 +197,10 @@ export const useList = (props: UseListOptions): UseListValue => {
const finalIds = tempData
.filter(data => typeof data !== 'undefined')
.map(data => data.id);

setFinalItems({
data: finalData,
ids: finalIds,
total: filteredLength,
});
}, [
data,
Expand Down Expand Up @@ -241,7 +246,7 @@ export const useList = (props: UseListOptions): UseListValue => {
setPerPage,
setSort,
showFilter,
total: finalItems.ids.length,
total: finalItems.total,
};
};

Expand Down