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 erases input while typing #7173

Merged
merged 2 commits into from
Feb 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/ra-core/src/form/useChoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const useChoices = ({

return translateChoice
? translate(choiceName, { _: choiceName })
: choiceName;
: String(choiceName);
},
[optionText, translate, translateChoice]
);
Expand Down
6 changes: 3 additions & 3 deletions packages/ra-core/src/form/useSuggestions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, isValidElement } from 'react';
import { useCallback, isValidElement, ReactElement } from 'react';
import set from 'lodash/set';
import useChoices, { OptionText, UseChoicesOptions } from './useChoices';
import { useTranslate } from '../i18n';
Expand Down Expand Up @@ -138,7 +138,7 @@ const defaultMatchSuggestion = getChoiceText => (
: suggestionText &&
suggestionText.match(
// We must escape any RegExp reserved characters to avoid errors
// For example, the filter might contains * which must be escaped as \*
// For example, the filter might contain * which must be escaped as \*
new RegExp(exact ? `^${regex}$` : regex, 'i')
);
};
Expand Down Expand Up @@ -183,7 +183,7 @@ export const getSuggestionsFactory = ({
selectedItem,
suggestionLimit = 0,
}: UseSuggestionsOptions & {
getChoiceText: (choice: any) => string;
getChoiceText: (choice: any) => string | ReactElement;
getChoiceValue: (choice: any) => string;
}) => filter => {
let suggestions = [];
Expand Down
12 changes: 8 additions & 4 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ export const AutocompleteInput = (props: AutocompleteInputProps) => {
);

const classes = useStyles(props);
let inputEl = useRef<HTMLInputElement>();
let anchorEl = useRef<any>();
const inputEl = useRef<HTMLInputElement>();
const anchorEl = useRef<any>();
const translate = useTranslate();

const {
Expand Down Expand Up @@ -320,7 +320,9 @@ export const AutocompleteInput = (props: AutocompleteInputProps) => {
selectedItem === null
? ''
: inputText
? inputText(getChoiceText(selectedItem).props.record)
? /* @ts-ignore */ inputText(
getChoiceText(selectedItem).props.record
)
: getChoiceText(selectedItem)
);
}, [
Expand Down Expand Up @@ -377,7 +379,9 @@ export const AutocompleteInput = (props: AutocompleteInputProps) => {
setFilterValue(
input.value
? inputText
? inputText(getChoiceText(selectedItem).props.record)
? /* @ts-ignore */ inputText(
getChoiceText(selectedItem).props.record
)
: getChoiceText(selectedItem)
: ''
);
Expand Down