|
1 |
| -import { Select, SelectProps } from "@quassel/ui"; |
| 1 | +import { Combobox, TextInput, TextInputProps, useCombobox } from "@quassel/ui"; |
| 2 | +import { useEffect, useState } from "react"; |
| 3 | +import { i18n } from "../stores/i18n"; |
| 4 | +import { useStore } from "@nanostores/react"; |
| 5 | +import { params } from "@nanostores/i18n"; |
2 | 6 |
|
3 |
| -export type EntitySelectProps = Omit<SelectProps, "value" | "onChange"> & { |
| 7 | +export type EntitySelectProps = Omit<TextInputProps, "value" | "onChange"> & { |
4 | 8 | value?: number;
|
5 |
| - onChange?: (id?: number) => void; |
| 9 | + onChange?: (value?: number) => void; |
| 10 | + onAddNew?: (value: string) => Promise<number>; |
6 | 11 | };
|
7 | 12 |
|
| 13 | +type StringKeys<T> = { [K in keyof T]-?: T[K] extends string ? K : never }[keyof T]; |
| 14 | + |
8 | 15 | type Props<T extends { id: number }> = Omit<EntitySelectProps, "data"> & {
|
9 | 16 | data?: T[];
|
10 |
| - buildLabel: (value: T) => string; |
| 17 | + onAddNew?: (value: string) => void; |
| 18 | + labelKey: StringKeys<T>; |
11 | 19 | };
|
12 | 20 |
|
13 |
| -export function EntitySelect<T extends { id: number }>({ value, onChange, data, buildLabel, ...rest }: Props<T>) { |
| 21 | +const customValueKey = "CUSTOM_VALUE"; |
| 22 | + |
| 23 | +const messages = i18n("entitySelect", { |
| 24 | + actionCreateNew: params('Create new "{value}"'), |
| 25 | +}); |
| 26 | + |
| 27 | +export function EntitySelect<T extends { id: number }>({ value, onChange, data, labelKey, onAddNew, ...rest }: Props<T>) { |
| 28 | + const t = useStore(messages); |
| 29 | + |
| 30 | + const combobox = useCombobox({ |
| 31 | + onDropdownClose: () => combobox.resetSelectedOption(), |
| 32 | + }); |
| 33 | + |
| 34 | + const [searchValue, setSearchValue] = useState(""); |
| 35 | + |
| 36 | + const shouldFilterOptions = !data?.some((item) => item[labelKey] === searchValue); |
| 37 | + const filteredOptions = |
| 38 | + (shouldFilterOptions |
| 39 | + ? data?.filter((item) => (item[labelKey] as string)?.toLowerCase().includes(searchValue.toLowerCase().trim())) |
| 40 | + : data) ?? []; |
| 41 | + |
| 42 | + const options = filteredOptions?.map((item) => ( |
| 43 | + <Combobox.Option key={item.id} value={item.id.toString()}> |
| 44 | + {item[labelKey] as string} |
| 45 | + </Combobox.Option> |
| 46 | + )); |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + if (value && data) { |
| 50 | + const index = data.findIndex((item) => item.id === value); |
| 51 | + if (index !== -1) { |
| 52 | + setSearchValue(data[index][labelKey] as string); |
| 53 | + combobox.selectOption(index); |
| 54 | + } |
| 55 | + } |
| 56 | + }, [value, data]); |
| 57 | + |
| 58 | + useEffect(() => { |
| 59 | + if (shouldFilterOptions) combobox.selectFirstOption(); |
| 60 | + }, [searchValue]); |
| 61 | + |
14 | 62 | return (
|
15 |
| - <Select |
16 |
| - value={value?.toString()} |
17 |
| - onChange={(value) => onChange?.(value ? parseInt(value) : undefined)} |
18 |
| - data={data?.map((entity) => ({ value: entity.id.toString(), label: buildLabel(entity) })) ?? []} |
19 |
| - {...rest} |
20 |
| - /> |
| 63 | + <Combobox |
| 64 | + store={combobox} |
| 65 | + onOptionSubmit={async (value) => { |
| 66 | + let id: number; |
| 67 | + if (value === customValueKey) { |
| 68 | + id = await onAddNew!(searchValue); |
| 69 | + } else { |
| 70 | + id = +value; |
| 71 | + } |
| 72 | + onChange?.(id); |
| 73 | + combobox.closeDropdown(); |
| 74 | + }} |
| 75 | + > |
| 76 | + <Combobox.Target> |
| 77 | + <TextInput |
| 78 | + value={searchValue} |
| 79 | + onChange={({ target: { value } }) => { |
| 80 | + setSearchValue(value); |
| 81 | + }} |
| 82 | + onClick={() => combobox.openDropdown()} |
| 83 | + onFocus={() => combobox.openDropdown()} |
| 84 | + onBlur={() => combobox.closeDropdown()} |
| 85 | + {...rest} |
| 86 | + /> |
| 87 | + </Combobox.Target> |
| 88 | + |
| 89 | + <Combobox.Dropdown> |
| 90 | + <Combobox.Options> |
| 91 | + {onAddNew && !options?.length && ( |
| 92 | + <Combobox.Option value={customValueKey}>{t.actionCreateNew({ value: searchValue })}</Combobox.Option> |
| 93 | + )} |
| 94 | + {options} |
| 95 | + </Combobox.Options> |
| 96 | + </Combobox.Dropdown> |
| 97 | + </Combobox> |
21 | 98 | );
|
22 | 99 | }
|
0 commit comments