Skip to content

Commit

Permalink
Merge pull request #5887 from srosset81/patch-2
Browse files Browse the repository at this point in the history
Handle arrays in ReferenceArrayField filter
  • Loading branch information
fzaninotto committed Feb 19, 2021
2 parents 4a63472 + 72a6215 commit 011d109
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
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,
});
});
});
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

0 comments on commit 011d109

Please sign in to comment.