Skip to content

Commit 77fc18c

Browse files
authored
Merge pull request #11364 from lprnmns/codex/fix-autocomplete-oncreate-autoselect
Fix AutocompleteInput clearing mouse selections on blur
2 parents c3f33a2 + dcd5c1a commit 77fc18c

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
InsideReferenceInputOnChange,
2727
WithInputProps,
2828
OnCreate,
29+
OnCreateWithAutoSelect,
2930
OnCreateSlow,
3031
CreateLabel,
3132
CreateItemLabel,
@@ -1212,6 +1213,18 @@ describe('<AutocompleteInput />', () => {
12121213
});
12131214

12141215
describe('onCreate', () => {
1216+
it('should keep a clicked choice on blur when autoSelect is enabled', async () => {
1217+
render(<OnCreateWithAutoSelect />);
1218+
const input = (await screen.findByLabelText(
1219+
'Author'
1220+
)) as HTMLInputElement;
1221+
1222+
fireEvent.focus(input);
1223+
fireEvent.click(await screen.findByText('Victor Hugo'));
1224+
fireEvent.blur(input);
1225+
1226+
expect(input.value).toEqual('Victor Hugo');
1227+
});
12151228
it("shouldn't include an option with the create label when the input is empty", async () => {
12161229
render(<OnCreate />);
12171230
const input = (await screen.findByLabelText(

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,12 @@ OnCreate.argTypes = {
375375
},
376376
};
377377

378+
export const OnCreateWithAutoSelect = () => (
379+
<Wrapper>
380+
<OnCreateInput autoSelect autoHighlight />
381+
</Wrapper>
382+
);
383+
378384
const AutocompleteWithCreateInReferenceInput = () => {
379385
const [create] = useCreate();
380386
const handleCreateAuthor = async (authorName?: string) => {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,13 @@ If you provided a React element for the optionText prop, you must also provide t
619619
(event: any, newValue: any, reason: AutocompleteChangeReason) => {
620620
// This prevents auto-submitting a form inside a dialog passed to the `create` prop
621621
event.preventDefault();
622+
if (
623+
reason === 'blur' &&
624+
typeof newValue === 'string' &&
625+
doesQueryMatchSelection(newValue)
626+
) {
627+
return;
628+
}
622629
if (reason === 'createOption') {
623630
// When users press the enter key after typing a new value, we can handle it as if they clicked on the create option
624631
handleChangeWithCreateSupport(
@@ -634,7 +641,12 @@ If you provided a React element for the optionText prop, you must also provide t
634641
newValue != null ? newValue : emptyValue
635642
);
636643
},
637-
[emptyValue, getCreateItem, handleChangeWithCreateSupport]
644+
[
645+
doesQueryMatchSelection,
646+
emptyValue,
647+
getCreateItem,
648+
handleChangeWithCreateSupport,
649+
]
638650
);
639651

640652
const oneSecondHasPassed = useTimeout(1000, filterValue);

0 commit comments

Comments
 (0)