Skip to content

Commit 5663bfb

Browse files
authored
Merge pull request #6215 from marmelab/quick-create-support-2
Add Initial Support For Creation in Choices Inputs
2 parents dd0e793 + 9662b01 commit 5663bfb

33 files changed

+2416
-754
lines changed

docs/Inputs.md

+504
Large diffs are not rendered by default.

examples/simple/src/comments/CommentEdit.tsx

+69-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { Card, Typography } from '@material-ui/core';
1+
import {
2+
Card,
3+
Typography,
4+
Dialog,
5+
DialogContent,
6+
TextField as MuiTextField,
7+
DialogActions,
8+
Button,
9+
} from '@material-ui/core';
210
import { makeStyles } from '@material-ui/core/styles';
311
import * as React from 'react';
412
import {
@@ -14,6 +22,8 @@ import {
1422
Title,
1523
minLength,
1624
Record,
25+
useCreateSuggestionContext,
26+
useCreate,
1727
} from 'react-admin'; // eslint-disable-line import/no-unresolved
1828

1929
const LinkToRelatedPost = ({ record }: { record?: Record }) => (
@@ -34,13 +44,64 @@ const useEditStyles = makeStyles({
3444
},
3545
});
3646

37-
const OptionRenderer = ({ record }: { record?: Record }) => (
38-
<span>
39-
{record?.title} - {record?.id}
40-
</span>
41-
);
47+
const OptionRenderer = ({ record }: { record?: Record }) => {
48+
return record.id === '@@ra-create' ? (
49+
<span>{record.name}</span>
50+
) : (
51+
<span>
52+
{record?.title} - {record?.id}
53+
</span>
54+
);
55+
};
56+
57+
const inputText = record =>
58+
record.id === '@@ra-create'
59+
? record.name
60+
: `${record.title} - ${record.id}`;
4261

43-
const inputText = record => `${record.title} - ${record.id}`;
62+
const CreatePost = () => {
63+
const { filter, onCancel, onCreate } = useCreateSuggestionContext();
64+
const [value, setValue] = React.useState(filter || '');
65+
const [create] = useCreate('posts');
66+
const handleSubmit = (event: React.FormEvent) => {
67+
event.preventDefault();
68+
create(
69+
{
70+
payload: {
71+
data: {
72+
title: value,
73+
},
74+
},
75+
},
76+
{
77+
onSuccess: ({ data }) => {
78+
setValue('');
79+
const choice = data;
80+
onCreate(choice);
81+
},
82+
}
83+
);
84+
return false;
85+
};
86+
return (
87+
<Dialog open onClose={onCancel}>
88+
<form onSubmit={handleSubmit}>
89+
<DialogContent>
90+
<MuiTextField
91+
label="New post title"
92+
value={value}
93+
onChange={event => setValue(event.target.value)}
94+
autoFocus
95+
/>
96+
</DialogContent>
97+
<DialogActions>
98+
<Button type="submit">Save</Button>
99+
<Button onClick={onCancel}>Cancel</Button>
100+
</DialogActions>
101+
</form>
102+
</Dialog>
103+
);
104+
};
44105

45106
const CommentEdit = props => {
46107
const classes = useEditStyles();
@@ -86,6 +147,7 @@ const CommentEdit = props => {
86147
fullWidth
87148
>
88149
<AutocompleteInput
150+
create={<CreatePost />}
89151
matchSuggestion={(
90152
filterValue,
91153
suggestion

0 commit comments

Comments
 (0)