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

Feature: Blur input on suggestion create #6646

Merged
Merged
Show file tree
Hide file tree
Changes from 9 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
19 changes: 18 additions & 1 deletion packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ export const AutocompleteInput = (props: AutocompleteInputProps) => {
[input.value, getSuggestionFromValue]
);

const doesQueryMatchSuggestion = useMemo(() => {
if (typeof optionText !== 'string') {
return false;
}
andrico1234 marked this conversation as resolved.
Show resolved Hide resolved

return choices.some(choice => get(choice, optionText) === filterValue);
}, [choices, optionText, filterValue]);

const shouldAllowCreate = !doesQueryMatchSuggestion;

const { getChoiceText, getChoiceValue, getSuggestions } = useSuggestions({
allowEmpty,
choices,
Expand All @@ -247,6 +257,10 @@ export const AutocompleteInput = (props: AutocompleteInputProps) => {
[filterValue, getChoiceValue, input]
);

const onCreateNewItem = () => {
inputEl.current.blur();
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need the onCreateNewItem and we can just call inputEl.current.blur(); at the end of the effect which run when the input value changes at L283.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the change btw


const {
getCreateItem,
handleChange: handleChangeWithCreateSupport,
Expand All @@ -257,6 +271,7 @@ export const AutocompleteInput = (props: AutocompleteInputProps) => {
createItemLabel,
createValue,
handleChange,
onCreateNewItem,
filter: filterValue,
onCreate,
optionText,
Expand Down Expand Up @@ -494,7 +509,9 @@ export const AutocompleteInput = (props: AutocompleteInputProps) => {
});
const suggestions = [
...getSuggestions(filterValue),
...(onCreate || create ? [getCreateItem()] : []),
...((onCreate || create) && shouldAllowCreate
? [getCreateItem()]
: []),
];

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const useSupportCreateSuggestion = (
filter,
handleChange,
onCreate,
onCreateNewItem,
} = options;
const translate = useTranslate();
const [renderOnCreate, setRenderOnCreate] = useState(false);
Expand All @@ -47,6 +48,10 @@ export const useSupportCreateSuggestion = (
onCreate: item => {
setRenderOnCreate(false);
handleChange(undefined, item);

if (onCreateNewItem) {
onCreateNewItem(item);
}
},
};

Expand Down Expand Up @@ -117,6 +122,7 @@ export interface SupportCreateSuggestionOptions {
filter?: string;
handleChange: (value: any, newChoice: any) => void;
onCreate?: OnCreateHandler;
onCreateNewItem?: (value: any) => void;
optionText?: OptionText;
}

Expand Down