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

Handle arrays in ReferenceArrayField filter #5887

Merged
merged 2 commits into from
Feb 19, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,86 @@ describe('<ReferenceArrayFieldController />', () => {
expect(dispatch.mock.calls[0][0].type).toBe('RA/CRUD_GET_MANY');
expect(dataProvider.getMany).toBeCalledTimes(1);
});

it('should filter string data based on the filter props', () => {
const children = jest.fn().mockReturnValue('child');
renderWithRedux(
<ReferenceArrayFieldController
record={{ id: 1, barIds: [1, 2] }}
filter={{ title: 'world' }}
resource="foo"
reference="bar"
source="barIds"
basePath=""
>
{children}
</ReferenceArrayFieldController>,
{
admin: {
resources: {
bar: {
data: {
1: { id: 1, title: 'hello' },
2: { id: 2, title: 'world' },
},
},
},
},
}
);
expect(children.mock.calls[0][0]).toMatchObject({
basePath: '',
currentSort: { field: 'id', order: 'ASC' },
loaded: true,
loading: true,
data: {
2: { id: 2, title: 'world' },
},
ids: [1, 2],
error: null,
});
});

it('should filter array data based on the filter props', () => {
const children = jest.fn().mockReturnValue('child');
renderWithRedux(
<ReferenceArrayFieldController
record={{ id: 1, barIds: [1, 2, 3, 4] }}
filter={{ items: ['two', 'four', 'five'] }}
resource="foo"
reference="bar"
source="barIds"
basePath=""
>
{children}
</ReferenceArrayFieldController>,
{
admin: {
resources: {
bar: {
data: {
1: { id: 1, items: ['one', 'two'] },
2: { id: 2, items: ['three'] },
3: { id: 3, items: 'four' },
4: { id: 4, items: ['five'] },
},
},
},
},
}
);
expect(children.mock.calls[0][0]).toMatchObject({
basePath: '',
currentSort: { field: 'id', order: 'ASC' },
loaded: true,
loading: true,
data: {
1: { id: 1, items: ['one', 'two'] },
3: { id: 3, items: 'four' },
4: { id: 4, items: ['five'] },
},
ids: [1, 2, 3, 4],
error: null,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,11 @@ const useReferenceArrayFieldController = (
if (!loaded) return;
// 1. filter
let tempData = data.filter(record =>
Object.entries(filterValues).every(
([filterName, filterValue]) =>
// eslint-disable-next-line eqeqeq
filterValue == get(record, filterName)
Object.entries(filterValues).every(([filterName, filterValue]) =>
Array.isArray(get(record, filterName))
? get(record, filterName).includes(filterValue)
: // eslint-disable-next-line eqeqeq
filterValue == get(record, filterName)
)
);
// 2. sort
Expand Down