Skip to content
Merged
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
23 changes: 12 additions & 11 deletions src/components/Selector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,14 @@ export const KeywordSelector = ({
onChange,
}: BaseSelectorMultiProps | BaseSelectorSingleProps) => {
const intl = useIntl();
const [defaultDataValue, setDefaultDataValue] = useState<
{ label: string; value: number }[] | null
const [selectedValue, setSelectedValue] = useState<
MultiValue<SingleVal> | SingleValue<SingleVal> | null
>(null);

useEffect(() => {
const loadDefaultKeywords = async (): Promise<void> => {
if (!defaultValue) {
setSelectedValue(null);
return;
}

Expand All @@ -317,16 +318,16 @@ export const KeywordSelector = ({
(keyword): keyword is Keyword => keyword !== null
);

setDefaultDataValue(
validKeywords.map((keyword) => ({
label: keyword.name,
value: keyword.id,
}))
);
const nextValue = validKeywords.map((keyword) => ({
label: keyword.name,
value: keyword.id,
}));

setSelectedValue(isMulti ? nextValue : (nextValue[0] ?? null));
};

loadDefaultKeywords();
}, [defaultValue]);
}, [defaultValue, isMulti]);

const loadKeywordOptions = async (inputValue: string) => {
const results = await axios.get<TmdbKeywordSearchResponse>(
Expand All @@ -346,7 +347,6 @@ export const KeywordSelector = ({

return (
<AsyncSelect
key={`keyword-select-${defaultDataValue}`}
inputId="data"
isMulti={isMulti}
isDisabled={isDisabled}
Expand All @@ -357,10 +357,11 @@ export const KeywordSelector = ({
? intl.formatMessage(messages.starttyping)
: intl.formatMessage(messages.nooptions)
}
defaultValue={defaultDataValue}
value={selectedValue}
loadOptions={loadKeywordOptions}
placeholder={intl.formatMessage(messages.searchKeywords)}
onChange={(value) => {
setSelectedValue(value);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onChange(value as any);
}}
Expand Down
Loading