2023-12-08 19:24:08 +03:00
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
2023-12-13 14:32:57 +03:00
|
|
|
import SelectMulti, { SelectMultiProps } from '@/components/Common/SelectMulti';
|
|
|
|
import { Grammeme } from '@/models/language';
|
|
|
|
import { getCompatibleGrams } from '@/models/languageAPI';
|
2023-12-28 14:04:44 +03:00
|
|
|
import { compareGrammemeOptions, IGrammemeOption, SelectorGrammemes } from '@/utils/selectors';
|
2023-12-08 19:24:08 +03:00
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
interface SelectGrammemeProps extends Omit<SelectMultiProps<IGrammemeOption>, 'value' | 'onChange'> {
|
|
|
|
value: IGrammemeOption[];
|
|
|
|
setValue: React.Dispatch<React.SetStateAction<IGrammemeOption[]>>;
|
|
|
|
className?: string;
|
|
|
|
placeholder?: string;
|
2023-12-08 19:24:08 +03:00
|
|
|
}
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
function SelectGrammeme({ value, setValue, ...restProps }: SelectGrammemeProps) {
|
2023-12-08 19:24:08 +03:00
|
|
|
const [options, setOptions] = useState<IGrammemeOption[]>([]);
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
useEffect(() => {
|
2023-12-08 19:24:08 +03:00
|
|
|
const compatible = getCompatibleGrams(
|
2023-12-28 14:04:44 +03:00
|
|
|
value.filter(data => Object.values(Grammeme).includes(data.value as Grammeme)).map(data => data.value as Grammeme)
|
2023-12-08 19:24:08 +03:00
|
|
|
);
|
2023-12-28 14:04:44 +03:00
|
|
|
setOptions(SelectorGrammemes.filter(({ value }) => compatible.includes(value as Grammeme)));
|
2023-12-08 19:24:08 +03:00
|
|
|
}, [value]);
|
|
|
|
|
|
|
|
return (
|
2023-12-28 14:04:44 +03:00
|
|
|
<SelectMulti
|
|
|
|
options={options}
|
|
|
|
value={value}
|
|
|
|
onChange={newValue => setValue([...newValue].sort(compareGrammemeOptions))}
|
|
|
|
{...restProps}
|
|
|
|
/>
|
|
|
|
);
|
2023-12-08 19:24:08 +03:00
|
|
|
}
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
export default SelectGrammeme;
|