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 useReferenceInputController sorting #5527

Merged
merged 3 commits into from
Nov 13, 2020
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
29 changes: 5 additions & 24 deletions examples/simple/src/comments/PostReferenceInput.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react';
import { Fragment, useState, useCallback, useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { Fragment, useState, useCallback } from 'react';
import { FormSpy, useForm } from 'react-final-form';

import { makeStyles } from '@material-ui/core/styles';
Expand All @@ -10,12 +9,7 @@ import DialogTitle from '@material-ui/core/DialogTitle';
import DialogContent from '@material-ui/core/DialogContent';
import DialogActions from '@material-ui/core/DialogActions';

import {
crudGetMatching,
ReferenceInput,
SelectInput,
useTranslate,
} from 'react-admin'; // eslint-disable-line import/no-unresolved
import { ReferenceInput, SelectInput, useTranslate } from 'react-admin'; // eslint-disable-line import/no-unresolved

import PostQuickCreate from './PostQuickCreate';
import PostPreview from './PostPreview';
Expand All @@ -30,26 +24,12 @@ const useStyles = makeStyles({
const PostReferenceInput = props => {
const translate = useTranslate();
const classes = useStyles();
const dispatch = useDispatch();
const { change } = useForm();

const [showCreateDialog, setShowCreateDialog] = useState(false);
const [showPreviewDialog, setShowPreviewDialog] = useState(false);
const [newPostId, setNewPostId] = useState('');

useEffect(() => {
//Refresh the choices of the ReferenceInput to ensure our newly created post
// always appear, even after selecting another post
dispatch(
crudGetMatching(
'posts',
'comments@post_id',
{ page: 1, perPage: 25 },
{ field: 'id', order: 'DESC' },
{}
)
);
}, [dispatch, newPostId]);
const [version, setVersion] = useState(0);

const handleNewClick = useCallback(
event => {
Expand Down Expand Up @@ -79,14 +59,15 @@ const PostReferenceInput = props => {
post => {
setShowCreateDialog(false);
setNewPostId(post.id);
setVersion(previous => previous + 1);
change('post_id', post.id);
},
[setShowCreateDialog, setNewPostId, change]
);

return (
<Fragment>
<ReferenceInput {...props} defaultValue={newPostId}>
<ReferenceInput key={version} {...props} defaultValue={newPostId}>
<SelectInput optionText="title" />
</ReferenceInput>
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export const useReferenceInputController = (
loading: dataStatus.waiting,
warning: dataStatus.warning,
},
choices: Object.keys(finalData).map(id => finalData[id]),
choices: finalIds.map(id => finalData[id]),
// kept for backwards compatibility
// @deprecated to be removed in 4.0
error: dataStatus.error,
Expand Down