diff --git a/src/lib/AsyncTypeaheadInput.tsx b/src/lib/AsyncTypeaheadInput.tsx index 88bf76b..a20edf2 100644 --- a/src/lib/AsyncTypeaheadInput.tsx +++ b/src/lib/AsyncTypeaheadInput.tsx @@ -33,6 +33,7 @@ interface AsyncTypeaheadInputProps extends CommonTypeahea defaultSelected?: TypeaheadOptions; inputRef?: RefObject; autocompleteProps?: AsyncTypeaheadAutocompleteProps; + preSelectSingleOption?: boolean; } const AsyncTypeaheadInput = (props: AsyncTypeaheadInputProps) => { @@ -75,10 +76,13 @@ const AsyncTypeaheadInput = (props: AsyncTypeaheadInputPr fixedOptions, withFixedOptionsInValue = true, fitMenuContent, + preSelectSingleOption = false, } = props; const [options, setOptions] = useState(defaultOptions); - const [value, setValue] = useState(defaultSelected); + const [value, setValue] = useState( + defaultSelected.length > 0 ? defaultSelected : preSelectSingleOption && defaultOptions.length === 1 ? defaultOptions : [], + ); const [page, setPage] = useState(1); const [loadMoreOptions, setLoadMoreOptions] = useState(limitResults !== undefined && limitResults < defaultOptions.length); const { name, id } = useSafeNameId(props.name ?? "", props.id); diff --git a/src/lib/StaticTypeaheadInput.tsx b/src/lib/StaticTypeaheadInput.tsx index 56f4e15..212819e 100644 --- a/src/lib/StaticTypeaheadInput.tsx +++ b/src/lib/StaticTypeaheadInput.tsx @@ -27,6 +27,7 @@ interface StaticTypeaheadInputProps extends CommonTypeahe options: TypeaheadOptions; isLoading?: boolean; autocompleteProps?: StaticTypeaheadAutocompleteProps; + preSelectSingleOption?: boolean; } const StaticTypeaheadInput = (props: StaticTypeaheadInputProps) => { @@ -66,6 +67,7 @@ const StaticTypeaheadInput = (props: StaticTypeaheadInput withFixedOptionsInValue = true, innerRef, fitMenuContent, + preSelectSingleOption = false, } = props; const [page, setPage] = useState(1); @@ -91,7 +93,11 @@ const StaticTypeaheadInput = (props: StaticTypeaheadInput [limitResults, page, options], ); - const fieldValue = watch(name) as string | number | string[] | number[] | undefined; + const defaultValue = useMemo( + () => (preSelectSingleOption && options.length === 1 ? (typeof options[0] === "string" ? options[0] : options[0]?.value) : undefined), + [preSelectSingleOption, options], + ); + const fieldValue = watch(name, defaultValue) as string | number | string[] | number[] | undefined; const value = useMemo( () => multiple diff --git a/src/lib/helpers/typeahead.tsx b/src/lib/helpers/typeahead.tsx index 82c0c82..a310f73 100644 --- a/src/lib/helpers/typeahead.tsx +++ b/src/lib/helpers/typeahead.tsx @@ -21,8 +21,16 @@ const convertAutoCompleteOptionsToStringArray = (options: TypeaheadOptions | und return (options as LabelValueOption[]).map((option) => option.value) as string[]; }; -const getSingleAutoCompleteValue = (options: TypeaheadOptions, fieldValue: string | number | undefined): TypeaheadOptions => { +const getSingleAutoCompleteValue = ( + options: TypeaheadOptions, + fieldValue: string | number | undefined, + defaultOption: TypeaheadOptions = [], +): TypeaheadOptions => { if (fieldValue === undefined) { + if (defaultOption.length > 0) { + return defaultOption; + } + return []; }