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

Add a custom filter property in useList #8116

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
18 changes: 18 additions & 0 deletions docs/useList.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,21 @@ const {
refetch, // a function that throws an error, as refetch doesn't make sense for local data
} = getGetList({ data });
```
## `filterCallback`

Property for custom filter definition. Being able to apply more complex filters using operators

```jsx
const { data } = useList({
data: [
{ id: 1, name: 'Arnold' },
{ id: 2, name: 'Sylvester' },
{ id: 3, name: 'Jean-Claude' },
],
sort: { field: 'name', order: 'ASC' },
filterCallback: (record) => record.id > 1 && record.name !== 'Jean-Claude'
});
// data will be
// [
// { id: 2, name: 'Sylvester' },
// ]
35 changes: 35 additions & 0 deletions packages/ra-core/src/controller/list/useList.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,39 @@ describe('<useList />', () => {
})
);
});

it('should filter array data based on the custom filter', async () => {
const callback = jest.fn();
const data = [
{ id: 1, items: ['one', 'two'] },
{ id: 2, items: ['three'] },
{ id: 3, items: 'four' },
{ id: 4, items: ['five'] },
];

render(
<UseList
data={data}
sort={{ field: 'id', order: 'ASC' }}
filterCallback={record => record.id > 2}
callback={callback}
/>
);

await waitFor(() => {
expect(callback).toHaveBeenCalledWith(
expect.objectContaining({
sort: { field: 'id', order: 'ASC' },
isFetching: false,
isLoading: false,
data: [
{ id: 3, items: 'four' },
{ id: 4, items: ['five'] },
],
error: undefined,
total: 2,
})
);
});
});
});
37 changes: 21 additions & 16 deletions packages/ra-core/src/controller/list/useList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const refetch = () => {
* @param {Number} props.page: Optional. The initial page index
* @param {Number} props.perPage: Optional. The initial page size
* @param {SortPayload} props.sort: Optional. The initial sort (field and order)
* @param {filterCallback} prop.filterCallback Optional. A function that allows you to make a custom filter
*/
export const useList = <RecordType extends RaRecord = any>(
props: UseListOptions<RecordType>
Expand All @@ -61,6 +62,7 @@ export const useList = <RecordType extends RaRecord = any>(
page: initialPage = 1,
perPage: initialPerPage = 1000,
sort: initialSort,
filterCallback = (record: RecordType) => Boolean(record),
} = props;
const resource = useResourceContext(props);

Expand Down Expand Up @@ -161,23 +163,25 @@ export const useList = <RecordType extends RaRecord = any>(

// 1. filter
if (filterValues) {
tempData = data.filter(record =>
Object.entries(filterValues).every(
([filterName, filterValue]) => {
const recordValue = get(record, filterName);
const result = Array.isArray(recordValue)
? Array.isArray(filterValue)
? recordValue.some(item =>
filterValue.includes(item)
)
: recordValue.includes(filterValue)
: Array.isArray(filterValue)
? filterValue.includes(recordValue)
: filterValue == recordValue; // eslint-disable-line eqeqeq
return result;
}
tempData = data
.filter(record =>
Object.entries(filterValues).every(
([filterName, filterValue]) => {
const recordValue = get(record, filterName);
const result = Array.isArray(recordValue)
? Array.isArray(filterValue)
? recordValue.some(item =>
filterValue.includes(item)
)
: recordValue.includes(filterValue)
: Array.isArray(filterValue)
? filterValue.includes(recordValue)
: filterValue == recordValue; // eslint-disable-line eqeqeq
return result;
}
)
)
);
.filter(filterCallback);
}
const filteredLength = tempData.length;

Expand Down Expand Up @@ -269,6 +273,7 @@ export interface UseListOptions<RecordType extends RaRecord = any> {
perPage?: number;
sort?: SortPayload;
resource?: string;
filterCallback?: (record: RecordType) => boolean;
}

export type UseListValue<
Expand Down