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 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
6 changes: 5 additions & 1 deletion cypress/integration/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,13 @@ describe('Edit Page', () => {
);
});

it('should allow to select an item from the AutocompleteInput without showing the choices again after', () => {
it.only('should allow to select an item from the AutocompleteInput without showing the choices again after', () => {
EditCommentPage.navigate();
cy.get(
'[value="Accusantium qui nihil voluptatum quia voluptas maxime ab similique - 1"]'
);
cy.get(EditCommentPage.elements.input('post_id'))
.type('{selectall}')
.clear()
.type('Sed quo');
cy.get('[role="tooltip"]').within(() => {
Expand Down
6 changes: 5 additions & 1 deletion examples/simple/src/comments/CommentEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ const CommentEdit = props => {
matchSuggestion={(
filterValue,
suggestion
) => true}
) => {
const title = `${suggestion.title} - ${suggestion.id}`;

return title.includes(filterValue);
}}
optionText={<OptionRenderer />}
inputText={inputText}
options={{
Expand Down
1 change: 1 addition & 0 deletions packages/ra-core/src/form/useSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ export const getSuggestionsFactory = ({
}
} else {
suggestions = choices.filter(choice => matchSuggestion(filter, choice));

if (!allowDuplicates) {
suggestions = removeAlreadySelectedSuggestions(
suggestions,
Expand Down
35 changes: 34 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,35 @@ export const AutocompleteInput = (props: AutocompleteInputProps) => {
[input.value, getSuggestionFromValue]
);

const doesQueryMatchSuggestion = useMemo(() => {
if (isValidElement(optionText)) {
return choices.some(choice => matchSuggestion(filterValue, choice));
}

const isFunction = typeof optionText === 'function';

if (isFunction) {
const hasOption = choices.some(choice => {
const text = optionText(choice) as string;

return get(choice, text) === filterValue;
});

const selectedItemText = optionText(selectedItem);

return hasOption || selectedItemText === filterValue;
}

const selectedItemText = get(selectedItem, optionText);
const hasOption = choices.some(choice => {
return get(choice, optionText) === filterValue;
});

return selectedItemText === filterValue || hasOption;
}, [choices, optionText, filterValue, matchSuggestion, selectedItem]);

const shouldAllowCreate = !doesQueryMatchSuggestion;

const { getChoiceText, getChoiceValue, getSuggestions } = useSuggestions({
allowEmpty,
choices,
Expand Down Expand Up @@ -294,6 +323,8 @@ export const AutocompleteInput = (props: AutocompleteInputProps) => {
? inputText(getChoiceText(selectedItem).props.record)
: getChoiceText(selectedItem)
);

inputEl.current.blur();
}, [
input.value,
handleFilterChange,
Expand Down Expand Up @@ -494,7 +525,9 @@ export const AutocompleteInput = (props: AutocompleteInputProps) => {
});
const suggestions = [
...getSuggestions(filterValue),
...(onCreate || create ? [getCreateItem()] : []),
...((onCreate || create) && shouldAllowCreate
? [getCreateItem()]
: []),
];

return (
Expand Down