ConceptPortal-public/rsconcept/frontend/src/components/select/SelectMultiGrammeme.tsx

38 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-12-08 19:24:08 +03:00
import { useEffect, useState } from 'react';
2025-01-28 23:23:42 +03:00
import { CProps } from '@/components/props';
import SelectMulti, { SelectMultiProps } from '@/components/ui/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
2024-05-27 20:42:34 +03:00
interface SelectMultiGrammemeProps
extends Omit<SelectMultiProps<IGrammemeOption>, 'value' | 'onChange'>,
CProps.Styling {
2023-12-28 14:04:44 +03:00
value: IGrammemeOption[];
onChange: (newValue: IGrammemeOption[]) => void;
2023-12-28 14:04:44 +03:00
placeholder?: string;
2023-12-08 19:24:08 +03:00
}
function SelectMultiGrammeme({ value, onChange, ...restProps }: SelectMultiGrammemeProps) {
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 => onChange([...newValue].sort(compareGrammemeOptions))}
2023-12-28 14:04:44 +03:00
{...restProps}
/>
);
2023-12-08 19:24:08 +03:00
}
2024-05-27 20:42:34 +03:00
export default SelectMultiGrammeme;