From ade04f9490173039a57735df3bf7ef2a2dc1ea4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20K=C3=BCng?= Date: Wed, 12 Aug 2026 13:45:40 +0200 Subject: [PATCH 1/5] Added preSelectSingleOption --- src/lib/AsyncTypeaheadInput.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib/AsyncTypeaheadInput.tsx b/src/lib/AsyncTypeaheadInput.tsx index 88bf76b..3e7f289 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?: false; } 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); From 776e6ac4499f69ff9238138ef7b3e8862be733e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20K=C3=BCng?= Date: Wed, 12 Aug 2026 14:08:56 +0200 Subject: [PATCH 2/5] Fix type --- src/lib/AsyncTypeaheadInput.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/AsyncTypeaheadInput.tsx b/src/lib/AsyncTypeaheadInput.tsx index 3e7f289..a20edf2 100644 --- a/src/lib/AsyncTypeaheadInput.tsx +++ b/src/lib/AsyncTypeaheadInput.tsx @@ -33,7 +33,7 @@ interface AsyncTypeaheadInputProps extends CommonTypeahea defaultSelected?: TypeaheadOptions; inputRef?: RefObject; autocompleteProps?: AsyncTypeaheadAutocompleteProps; - preSelectSingleOption?: false; + preSelectSingleOption?: boolean; } const AsyncTypeaheadInput = (props: AsyncTypeaheadInputProps) => { From bcec292c4ab30560ef547adb51e8cfb11c12df86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20K=C3=BCng?= Date: Wed, 12 Aug 2026 14:57:39 +0200 Subject: [PATCH 3/5] added defualt logic --- src/lib/StaticTypeaheadInput.tsx | 15 ++++++++++++--- src/lib/helpers/typeahead.tsx | 20 ++++++++++++++++++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/lib/StaticTypeaheadInput.tsx b/src/lib/StaticTypeaheadInput.tsx index 56f4e15..12b3c67 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,13 +93,19 @@ const StaticTypeaheadInput = (props: StaticTypeaheadInput [limitResults, page, options], ); + const [defaultSelected, setDefaultSelected] = useState(preSelectSingleOption && options.length === 1 ? options : []); + const fieldValue = watch(name) as string | number | string[] | number[] | undefined; const value = useMemo( () => multiple - ? getMultipleAutoCompleteValue(combineOptions(options, fixedOptions), fieldValue as string[] | number[] | undefined) - : getSingleAutoCompleteValue(options, fieldValue as string | number | undefined), - [fieldValue, multiple, options, fixedOptions], + ? getMultipleAutoCompleteValue( + combineOptions(options, fixedOptions), + fieldValue as string[] | number[] | undefined, + defaultSelected, + ) + : getSingleAutoCompleteValue(options, fieldValue as string | number | undefined, defaultSelected), + [fieldValue, multiple, options, fixedOptions, defaultSelected], ); validateFixedOptions(fixedOptions, multiple, autocompleteProps, withFixedOptionsInValue, value); @@ -164,6 +172,7 @@ const StaticTypeaheadInput = (props: StaticTypeaheadInput const optionsArray = getOptionsFromValue(value, fixedOptions, withFixedOptionsInValue); const values = convertAutoCompleteOptionsToStringArray(optionsArray); const finalValue = multiple ? values : values[0]; + setDefaultSelected([]); clearErrors(field.name); if (onChange) { const finalOption = multiple ? optionsArray : optionsArray?.at(0); diff --git a/src/lib/helpers/typeahead.tsx b/src/lib/helpers/typeahead.tsx index 82c0c82..402e8ec 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 []; } @@ -32,8 +40,16 @@ const getSingleAutoCompleteValue = (options: TypeaheadOptions, fieldValue: strin ) as TypeaheadOptions; }; -const getMultipleAutoCompleteValue = (options: TypeaheadOptions, fieldValue: (string | number)[] | undefined): TypeaheadOptions => { +const getMultipleAutoCompleteValue = ( + options: TypeaheadOptions, + fieldValue: (string | number)[] | undefined, + defaultOption: TypeaheadOptions = [], +): TypeaheadOptions => { if (fieldValue === undefined) { + if (defaultOption.length > 0) { + return defaultOption; + } + return []; } return (options as TypeaheadOption[]).filter((x) => From 1748c7b8f2caa84de817a37641b9d5f5bf2ce189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20K=C3=BCng?= Date: Thu, 13 Aug 2026 10:04:57 +0200 Subject: [PATCH 4/5] up --- src/lib/StaticTypeaheadInput.tsx | 20 +++++++++----------- src/lib/helpers/typeahead.tsx | 10 +--------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/lib/StaticTypeaheadInput.tsx b/src/lib/StaticTypeaheadInput.tsx index 12b3c67..00a451f 100644 --- a/src/lib/StaticTypeaheadInput.tsx +++ b/src/lib/StaticTypeaheadInput.tsx @@ -93,19 +93,18 @@ const StaticTypeaheadInput = (props: StaticTypeaheadInput [limitResults, page, options], ); - const [defaultSelected, setDefaultSelected] = useState(preSelectSingleOption && options.length === 1 ? options : []); - - const fieldValue = watch(name) as string | number | string[] | number[] | undefined; + const fieldValue = watch(name, preSelectSingleOption && options.length === 1 ? options : []) as + | string + | number + | string[] + | number[] + | undefined; const value = useMemo( () => multiple - ? getMultipleAutoCompleteValue( - combineOptions(options, fixedOptions), - fieldValue as string[] | number[] | undefined, - defaultSelected, - ) - : getSingleAutoCompleteValue(options, fieldValue as string | number | undefined, defaultSelected), - [fieldValue, multiple, options, fixedOptions, defaultSelected], + ? getMultipleAutoCompleteValue(combineOptions(options, fixedOptions), fieldValue as string[] | number[] | undefined) + : getSingleAutoCompleteValue(options, fieldValue as string | number | undefined), + [fieldValue, multiple, options, fixedOptions], ); validateFixedOptions(fixedOptions, multiple, autocompleteProps, withFixedOptionsInValue, value); @@ -172,7 +171,6 @@ const StaticTypeaheadInput = (props: StaticTypeaheadInput const optionsArray = getOptionsFromValue(value, fixedOptions, withFixedOptionsInValue); const values = convertAutoCompleteOptionsToStringArray(optionsArray); const finalValue = multiple ? values : values[0]; - setDefaultSelected([]); clearErrors(field.name); if (onChange) { const finalOption = multiple ? optionsArray : optionsArray?.at(0); diff --git a/src/lib/helpers/typeahead.tsx b/src/lib/helpers/typeahead.tsx index 402e8ec..a310f73 100644 --- a/src/lib/helpers/typeahead.tsx +++ b/src/lib/helpers/typeahead.tsx @@ -40,16 +40,8 @@ const getSingleAutoCompleteValue = ( ) as TypeaheadOptions; }; -const getMultipleAutoCompleteValue = ( - options: TypeaheadOptions, - fieldValue: (string | number)[] | undefined, - defaultOption: TypeaheadOptions = [], -): TypeaheadOptions => { +const getMultipleAutoCompleteValue = (options: TypeaheadOptions, fieldValue: (string | number)[] | undefined): TypeaheadOptions => { if (fieldValue === undefined) { - if (defaultOption.length > 0) { - return defaultOption; - } - return []; } return (options as TypeaheadOption[]).filter((x) => From fc978e696c787139494951625e37a5ff77338dfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20K=C3=BCng?= Date: Thu, 13 Aug 2026 10:21:17 +0200 Subject: [PATCH 5/5] up --- src/lib/StaticTypeaheadInput.tsx | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/lib/StaticTypeaheadInput.tsx b/src/lib/StaticTypeaheadInput.tsx index 00a451f..212819e 100644 --- a/src/lib/StaticTypeaheadInput.tsx +++ b/src/lib/StaticTypeaheadInput.tsx @@ -93,12 +93,11 @@ const StaticTypeaheadInput = (props: StaticTypeaheadInput [limitResults, page, options], ); - const fieldValue = watch(name, preSelectSingleOption && options.length === 1 ? options : []) 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