ConceptPortal-public/rsconcept/frontend/src/components/Shared/SelectGrammeme.tsx

41 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-12-08 19:24:08 +03:00
import { useEffect, useState } from 'react';
import SelectMulti, { SelectMultiProps } from '@/components/Common/SelectMulti';
import { Grammeme } from '@/models/language';
import { getCompatibleGrams } from '@/models/languageAPI';
2023-12-27 19:34:39 +03:00
import { compareGrammemeOptions,IGrammemeOption, SelectorGrammemes } 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[]>>
className?: string
placeholder?: string
}
function SelectGrammeme({
value, setValue,
...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)
);
2023-12-27 19:34:39 +03:00
setOptions(SelectorGrammemes.filter(({value}) => compatible.includes(value as Grammeme)));
2023-12-08 19:24:08 +03:00
}, [value]);
return (
<SelectMulti
options={options}
value={value}
onChange={newValue => setValue([...newValue].sort(compareGrammemeOptions))}
{...restProps}
2023-12-08 19:24:08 +03:00
/>);
}
export default SelectGrammeme;