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

[RFR] Fix ReferenceInput Ignore its sort prop #3783

Merged
merged 2 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions examples/simple/src/comments/CommentCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
import PostReferenceInput from './PostReferenceInput';

const defaultValue = { created_at: new Date() };
const defaultSort = { field: 'title', order: 'ASC' };

const CommentCreate = props => (
<Create {...props}>
<SimpleForm redirect={false} defaultValue={defaultValue}>
Expand All @@ -20,6 +22,7 @@ const CommentCreate = props => (
allowEmpty
validate={required()}
perPage={10000}
sort={defaultSort}
/>
<TextInput
source="author.name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import expect from 'expect';
import renderWithRedux from '../../util/renderWithRedux';
import ReferenceInputController from './ReferenceInputController';
import { DataProviderContext } from '../../dataProvider';
import { crudGetMatching } from '../../actions';

describe('<ReferenceInputController />', () => {
const defaultProps = {
Expand Down Expand Up @@ -34,6 +35,7 @@ describe('<ReferenceInputController />', () => {
...defaultProps,
input: { value: 1 } as any,
loading: true,
sort: { field: 'title', order: 'ASC' },
}}
>
{children}
Expand Down Expand Up @@ -62,14 +64,23 @@ describe('<ReferenceInputController />', () => {
filter: { q: '' },
loading: false,
pagination: { page: 1, perPage: 25 },
sort: { field: 'id', order: 'DESC' },
sort: { field: 'title', order: 'ASC' },
warning: null,
});
await new Promise(resolve => setTimeout(resolve));
await new Promise(resolve => setTimeout(resolve, 100));
expect(dispatch).toBeCalledTimes(6);
expect(dispatch.mock.calls[0][0].type).toBe(
'RA/CRUD_GET_MATCHING_ACCUMULATE'
);
expect(dispatch.mock.calls[0][0].meta.accumulate()).toEqual(
crudGetMatching(
'posts',
'comments@post_id',
{ page: 1, perPage: 25 },
{ field: 'title', order: 'ASC' },
{ q: '' }
)
);
expect(dispatch.mock.calls[1][0].type).toBe('RA/CRUD_GET_MANY');
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { useEffect } from 'react';
import isEqual from 'lodash/isEqual';

import { getStatusForInput as getDataStatus } from './referenceDataStatus';
import useTranslate from '../../i18n/useTranslate';
import { Sort, Record, Pagination } from '../../types';
Expand Down Expand Up @@ -84,12 +87,23 @@ const useReferenceInputController = ({
filterToQuery,
referenceSource = defaultReferenceSource,
resource,
sort: sortOverride,
source,
}: Option): ReferenceInputValue => {
const translate = useTranslate();

const { pagination, setPagination } = usePaginationState({ perPage });
const { sort, setSort } = useSortState();
const { sort, setSort } = useSortState(sortOverride);

// We need to keep the current sort synchronized with the sort prop
// because the inner component may call the setSort too
useEffect(() => {
if (!isEqual(sort, sortOverride)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the !? I'd expect that the effect does nothing if the props are equal, and sets the sort if the props are different. That makes me think that you are probably missing a unit test.

return;
}
setSort(sortOverride);
}, [setSort, sort, sortOverride]);

const { filter: filterValue, setFilter } = useFilterState({
permanentFilter: filter,
filterToQuery,
Expand Down