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';
|
|
|
|
import { compareGrammemeOptions,IGrammemeOption, SelectorGrammems } from '@/utils/selectors';
|
2023-12-08 19:24:08 +03:00
|
|
|
|
|
|
|
interface SelectGrammemeProps extends
|
|
|
|
Omit<SelectMultiProps<IGrammemeOption>, 'value' | 'onChange'> {
|
|
|
|
value: IGrammemeOption[]
|
|
|
|
setValue: React.Dispatch<React.SetStateAction<IGrammemeOption[]>>
|
|
|
|
dimensions?: string
|
|
|
|
className?: string
|
|
|
|
placeholder?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
function SelectGrammeme({
|
|
|
|
value, setValue,
|
2023-12-18 12:25:39 +03:00
|
|
|
dimensions, className, placeholder,
|
|
|
|
...restProps
|
2023-12-08 19:24:08 +03:00
|
|
|
}: SelectGrammemeProps) {
|
|
|
|
const [options, setOptions] = useState<IGrammemeOption[]>([]);
|
|
|
|
|
|
|
|
useEffect(
|
|
|
|
() => {
|
|
|
|
const compatible = getCompatibleGrams(
|
|
|
|
value
|
|
|
|
.filter(data => Object.values(Grammeme).includes(data.value as Grammeme))
|
|
|
|
.map(data => data.value as Grammeme)
|
|
|
|
);
|
|
|
|
setOptions(SelectorGrammems.filter(({value}) => compatible.includes(value as Grammeme)));
|
|
|
|
}, [value]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SelectMulti
|
|
|
|
className={`${dimensions} ${className}`}
|
|
|
|
options={options}
|
|
|
|
placeholder={placeholder}
|
|
|
|
value={value}
|
|
|
|
onChange={newValue => setValue([...newValue].sort(compareGrammemeOptions))}
|
2023-12-18 12:25:39 +03:00
|
|
|
{...restProps}
|
2023-12-08 19:24:08 +03:00
|
|
|
/>);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SelectGrammeme;
|