Skip to content
Draft
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 src/lib/AsyncTypeaheadInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface AsyncTypeaheadInputProps<T extends FieldValues> extends CommonTypeahea
defaultSelected?: TypeaheadOptions;
inputRef?: RefObject<AsyncTypeaheadInputRef | null>;
autocompleteProps?: AsyncTypeaheadAutocompleteProps;
preSelectSingleOption?: boolean;
}

const AsyncTypeaheadInput = <T extends FieldValues>(props: AsyncTypeaheadInputProps<T>) => {
Expand Down Expand Up @@ -75,10 +76,13 @@ const AsyncTypeaheadInput = <T extends FieldValues>(props: AsyncTypeaheadInputPr
fixedOptions,
withFixedOptionsInValue = true,
fitMenuContent,
preSelectSingleOption = false,
} = props;

const [options, setOptions] = useState<TypeaheadOptions>(defaultOptions);
const [value, setValue] = useState<TypeaheadOptions>(defaultSelected);
const [value, setValue] = useState<TypeaheadOptions>(
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);
Expand Down
8 changes: 7 additions & 1 deletion src/lib/StaticTypeaheadInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface StaticTypeaheadInputProps<T extends FieldValues> extends CommonTypeahe
options: TypeaheadOptions;
isLoading?: boolean;
autocompleteProps?: StaticTypeaheadAutocompleteProps;
preSelectSingleOption?: boolean;
}

const StaticTypeaheadInput = <T extends FieldValues>(props: StaticTypeaheadInputProps<T>) => {
Expand Down Expand Up @@ -66,6 +67,7 @@ const StaticTypeaheadInput = <T extends FieldValues>(props: StaticTypeaheadInput
withFixedOptionsInValue = true,
innerRef,
fitMenuContent,
preSelectSingleOption = false,
} = props;

const [page, setPage] = useState(1);
Expand All @@ -91,7 +93,11 @@ const StaticTypeaheadInput = <T extends FieldValues>(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
Expand Down
10 changes: 9 additions & 1 deletion src/lib/helpers/typeahead.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}

Expand Down
Loading