Skip to content

Commit e876fda

Browse files
committed
Fix AutocompleteInput displays 'Create' option for choices that already exist when createLabel is provided
1 parent f5294e7 commit e876fda

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

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

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ describe('<AutocompleteInput />', () => {
11681168
});
11691169

11701170
describe('onCreate', () => {
1171-
it("shouldn't include an option with the createLabel when the input is empty", async () => {
1171+
it("shouldn't include an option with the create label when the input is empty", async () => {
11721172
const choices = [
11731173
{ id: 'ang', name: 'Angular' },
11741174
{ id: 'rea', name: 'React' },
@@ -1259,6 +1259,67 @@ describe('<AutocompleteInput />', () => {
12591259
expect(screen.queryByText('ra.action.create')).toBeNull();
12601260
expect(screen.queryByText('ra.action.create_item')).toBeNull();
12611261
});
1262+
it('should not show the create option when a choice is selected when using a custom createLabel', async () => {
1263+
const choices = [
1264+
{ id: 'ang', name: 'Angular' },
1265+
{ id: 'rea', name: 'React' },
1266+
];
1267+
const handleCreate = filter => {
1268+
const newChoice = {
1269+
id: 'js_fatigue',
1270+
name: filter,
1271+
};
1272+
choices.push(newChoice);
1273+
return newChoice;
1274+
};
1275+
1276+
render(
1277+
<AdminContext dataProvider={testDataProvider()}>
1278+
<ResourceContextProvider value="posts">
1279+
<SimpleForm
1280+
mode="onBlur"
1281+
onSubmit={jest.fn()}
1282+
defaultValues={{ language: 'ang' }}
1283+
>
1284+
<AutocompleteInput
1285+
source="language"
1286+
choices={choices}
1287+
onCreate={handleCreate}
1288+
createLabel="Start typing to create a new item"
1289+
/>
1290+
</SimpleForm>
1291+
</ResourceContextProvider>
1292+
</AdminContext>
1293+
);
1294+
1295+
const input = screen.getByLabelText(
1296+
'resources.posts.fields.language'
1297+
) as HTMLInputElement;
1298+
input.focus();
1299+
1300+
// First, clear the input
1301+
fireEvent.change(input, {
1302+
target: { value: '' },
1303+
});
1304+
// We expect only the 'Start typing to create a new item' option
1305+
await screen.findByText('React');
1306+
expect(
1307+
screen.queryByText('Start typing to create a new item')
1308+
).not.toBeNull();
1309+
expect(screen.queryByText('ra.action.create_item')).toBeNull();
1310+
expect(screen.queryByText('ra.action.create')).toBeNull();
1311+
1312+
// Then, change the input to an existing value
1313+
fireEvent.click(screen.getByText('Angular'));
1314+
fireEvent.focus(input);
1315+
// We expect all create labels not to render
1316+
await screen.findByText('React');
1317+
expect(
1318+
screen.queryByText('Start typing to create a new item')
1319+
).toBeNull();
1320+
expect(screen.queryByText('ra.action.create_item')).toBeNull();
1321+
expect(screen.queryByText('ra.action.create')).toBeNull();
1322+
});
12621323
it('should include an option with the createItemLabel when the input not empty', async () => {
12631324
const choices = [
12641325
{ id: 'ang', name: 'Angular' },

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ If you provided a React element for the optionText prop, you must also provide t
519519
// add create option if necessary
520520
const { inputValue } = params;
521521
if (onCreate || create) {
522-
if (inputValue === '' && createLabel) {
522+
if (inputValue === '' && filterValue === '' && createLabel) {
523523
// create option with createLabel
524524
filteredOptions = filteredOptions.concat(getCreateItem(''));
525525
} else if (!doesQueryMatchSuggestion(filterValue)) {

0 commit comments

Comments
 (0)