Skip to content

Commit 9b73699

Browse files
committed
Fix AutocompleteInput optionValue with create raises a React warning
1 parent 2d21bf4 commit 9b73699

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

packages/ra-core/src/form/choices/useChoices.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export const useChoices = ({
8383
);
8484

8585
const getChoiceValue = useCallback(
86-
choice => get(choice, optionValue),
86+
choice => get(choice, optionValue, get(choice, 'id')),
8787
[optionValue]
8888
);
8989

packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,24 @@ describe('<AutocompleteInput />', () => {
14961496
).toEqual('true');
14971497
expect(screen.queryByText(/Create/)).toBeNull();
14981498
});
1499+
1500+
it('should allow the creation of a new choice when using optionValue', async () => {
1501+
render(<OnCreate optionValue="_id" />);
1502+
const input = (await screen.findByLabelText(
1503+
'Author'
1504+
)) as HTMLInputElement;
1505+
// Enter an unknown value and submit it with Enter
1506+
await userEvent.type(input, 'New Value{Enter}');
1507+
await screen.getByDisplayValue('New Value');
1508+
// Clear the input, otherwise the new value won't be shown in the dropdown as it is selected
1509+
fireEvent.change(input, {
1510+
target: { value: '' },
1511+
});
1512+
// Open the dropdown
1513+
fireEvent.mouseDown(input);
1514+
// Check the new value is in the dropdown
1515+
await screen.findByText('New Value');
1516+
});
14991517
});
15001518
describe('create', () => {
15011519
it('should allow the creation of a new choice', async () => {

packages/ra-ui-materialui/src/input/AutocompleteInput.stories.tsx

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,49 +260,55 @@ export const OptionTextElement = () => (
260260

261261
const choicesForCreationSupport: Partial<{
262262
id: number;
263+
_id: number;
263264
name: string;
264265
full_name: string;
265266
first_name: string;
266267
last_name: string;
267268
}>[] = [
268269
{
269270
id: 1,
271+
_id: 1,
270272
name: 'Leo Tolstoy',
271273
full_name: 'Leo Tolstoy',
272274
first_name: 'Leo',
273275
last_name: 'Tolstoy',
274276
},
275277
{
276278
id: 2,
279+
_id: 2,
277280
name: 'Victor Hugo',
278281
full_name: 'Victor Hugo',
279282
first_name: 'Victor',
280283
last_name: 'Hugo',
281284
},
282285
{
283286
id: 3,
287+
_id: 3,
284288
name: 'William Shakespeare',
285289
full_name: 'William Shakespeare',
286290
first_name: 'William',
287291
last_name: 'Shakespeare',
288292
},
289293
{
290294
id: 4,
295+
_id: 4,
291296
name: 'Charles Baudelaire',
292297
full_name: 'Charles Baudelaire',
293298
first_name: 'Charles',
294299
last_name: 'Baudelaire',
295300
},
296301
{
297302
id: 5,
303+
_id: 5,
298304
name: 'Marcel Proust',
299305
full_name: 'Marcel Proust',
300306
first_name: 'Marcel',
301307
last_name: 'Proust',
302308
},
303309
];
304310

305-
const OnCreateInput = () => {
311+
const OnCreateInput = (props: Partial<AutocompleteInputProps>) => {
306312
const [choices, setChoices] = useState(choicesForCreationSupport);
307313
return (
308314
<AutocompleteInput
@@ -313,6 +319,7 @@ const OnCreateInput = () => {
313319

314320
const newOption = {
315321
id: choices.length + 1,
322+
_id: choices.length + 1,
316323
name: filter,
317324
};
318325
setChoices(options => [...options, newOption]);
@@ -323,15 +330,29 @@ const OnCreateInput = () => {
323330
TextFieldProps={{
324331
placeholder: 'Start typing to create a new item',
325332
}}
333+
{...props}
326334
/>
327335
);
328336
};
329337

330-
export const OnCreate = () => (
338+
export const OnCreate = (props: Partial<AutocompleteInputProps>) => (
331339
<Wrapper>
332-
<OnCreateInput />
340+
<OnCreateInput {...props} />
333341
</Wrapper>
334342
);
343+
OnCreate.args = {
344+
optionValue: undefined,
345+
};
346+
OnCreate.argTypes = {
347+
optionValue: {
348+
options: ['default', '_id'],
349+
mapping: {
350+
default: undefined,
351+
_id: '_id',
352+
},
353+
control: { type: 'inline-radio' },
354+
},
355+
};
335356

336357
const AutocompleteWithCreateInReferenceInput = () => {
337358
const [create] = useCreate();

0 commit comments

Comments
 (0)