Fix chip input issue

This commit is contained in:
LASER-Yi 2022-10-11 23:49:52 +08:00
parent 559ec50c92
commit 1a612d12b8
2 changed files with 9 additions and 1 deletions

View file

@ -28,6 +28,7 @@ const ChipInput: FunctionComponent<ChipInputProps> = ({ ...props }) => {
onChange?.([...(value ?? []), query]);
return query;
}}
buildOption={(value) => value}
></MultiSelector>
);
};

View file

@ -109,6 +109,7 @@ export type MultiSelectorProps<T> = Override<
options: readonly SelectorOption<T>[];
onChange?: (value: T[]) => void;
getkey?: (value: T) => string;
buildOption?: (value: string) => T;
},
Omit<MultiSelectProps, "data">
>;
@ -119,11 +120,15 @@ export function MultiSelector<T>({
options,
onChange,
getkey = DefaultKeyBuilder,
buildOption,
...select
}: MultiSelectorProps<T>) {
const labelRef = useRef(getkey);
labelRef.current = getkey;
const buildRef = useRef(buildOption);
buildRef.current = buildOption;
const data = useMemo(
() =>
options.map<SelectItemWithPayload<T>>(({ value, ...option }) => ({
@ -150,6 +155,8 @@ export function MultiSelector<T>({
const payload = data.find((v) => v.value === value)?.payload;
if (payload) {
payloads.push(payload);
} else if (buildRef.current) {
payloads.push(buildRef.current(value));
}
}
onChange?.(payloads);
@ -159,10 +166,10 @@ export function MultiSelector<T>({
return (
<MultiSelect
{...select}
value={wrappedValue}
defaultValue={wrappedDefaultValue}
onChange={wrappedOnChange}
{...select}
data={data}
></MultiSelect>
);