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 AutocompleteInput with create does not support nested optionText and fix enableGetChoices examples in doc #8556

Merged
merged 1 commit into from
Jan 6, 2023
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
2 changes: 1 addition & 1 deletion docs/ReferenceArrayInput.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ You can make the `getList()` call lazy by using the `enableGetChoices` prop. Thi
<ReferenceArrayInput
source="tags_ids"
reference="tags"
enableGetChoices={({ q }) => q.length >= 2}
enableGetChoices={({ q }) => q && q.length >= 2}
/>
```

Expand Down
2 changes: 1 addition & 1 deletion docs/ReferenceInput.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ You can make the `getList()` call lazy by using the `enableGetChoices` prop. Thi
<ReferenceInput
source="company_id"
reference="companies"
enableGetChoices={({ q }) => q.length >= 2}
enableGetChoices={({ q }) => q && q.length >= 2}
/>
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,49 @@ describe('<AutocompleteArrayInput />', () => {
expect(screen.getByText('Programming')).not.toBeNull();
});

it('should use optionText with a string value including "." as text identifier when a create element is passed', () => {
const choices = [
{ id: 't', foobar: { name: 'Technical' } },
{ id: 'p', foobar: { name: 'Programming' } },
];
const newChoice = {
id: 'js_fatigue',
foobar: { name: 'New Kid On The Block' },
};

const Create = () => {
const context = useCreateSuggestionContext();
const handleClick = () => {
choices.push(newChoice);
context.onCreate(newChoice);
};

return <button onClick={handleClick}>Get the kid</button>;
};

render(
<AdminContext dataProvider={testDataProvider()}>
<SimpleForm onSubmit={jest.fn()}>
<AutocompleteArrayInput
{...defaultProps}
create={<Create />}
optionText="foobar.name"
choices={choices}
/>
</SimpleForm>
</AdminContext>
);

userEvent.type(
screen.getByLabelText('resources.posts.fields.tags'),
'a'
);
expect(screen.queryAllByRole('option')).toHaveLength(3);
expect(screen.getByText('Technical')).not.toBeNull();
expect(screen.getByText('Programming')).not.toBeNull();
expect(screen.getByText('ra.action.create_item')).not.toBeNull();
});

it('should support creation of a new choice through the onCreate event when optionText is a function', async () => {
const choices = [
{ id: 'ang', name: 'Angular' },
Expand Down
10 changes: 7 additions & 3 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,17 @@ If you provided a React element for the optionText prop, you must also provide t
}

if (option?.id === createId) {
return option?.name;
return get(
option,
typeof optionText === 'string' ? optionText : 'name'
);
}

if (!isListItem && option[optionValue || 'id'] === emptyValue) {
return option[
return get(
option,
typeof optionText === 'string' ? optionText : 'name'
];
);
}

if (!isListItem && inputText !== undefined) {
Expand Down