ConceptPortal-public/rsconcept/frontend/src/components/Common/SelectSingle.tsx

68 lines
1.8 KiB
TypeScript
Raw Normal View History

import { useMemo } from 'react';
import Select, { GroupBase, Props, StylesConfig } from 'react-select';
import { useConceptTheme } from '../../context/ThemeContext';
import { selectDarkT, selectLightT } from '../../utils/color';
2023-09-14 16:53:38 +03:00
interface SelectSingleProps<
Option,
Group extends GroupBase<Option> = GroupBase<Option>
>
2023-11-29 13:07:55 +03:00
extends Omit<Props<Option, false, Group>, 'theme' | 'menuPortalTarget'> {
noPortal?: boolean
}
2023-11-29 13:07:55 +03:00
function SelectSingle<Option, Group extends GroupBase<Option> = GroupBase<Option>> ({
noPortal, ...restProps
}: SelectSingleProps<Option, Group>) {
const { darkMode, colors } = useConceptTheme();
const themeColors = useMemo(
() => !darkMode ? selectLightT : selectDarkT
, [darkMode]);
const adjustedStyles: StylesConfig<Option, false, Group> = useMemo(
() => ({
control: (styles, { isDisabled }) => ({
...styles,
borderRadius: '0.25rem',
cursor: isDisabled ? 'not-allowed' : 'pointer'
}),
2023-11-29 13:07:55 +03:00
menuPortal: (styles) => ({
...styles,
zIndex: 9999
}),
2023-10-14 17:14:40 +03:00
menuList: (styles) => ({
...styles,
padding: '0px'
}),
option: (styles, { isSelected }) => ({
...styles,
backgroundColor: isSelected ? colors.bgSelected : styles.backgroundColor,
color: isSelected ? colors.fgSelected : styles.color,
borderWidth: '1px',
borderColor: colors.border
}),
input: (styles) => ({...styles}),
placeholder: (styles) => ({...styles}),
singleValue: (styles) => ({...styles}),
}), [colors]);
return (
<Select
noOptionsMessage={() => 'Список пуст'}
theme={theme => ({
...theme,
borderRadius: 0,
colors: {
...theme.colors,
...themeColors
},
})}
menuPortalTarget={!noPortal ? document.body : null}
styles={adjustedStyles}
{...restProps}
/>);
}
2023-09-14 16:53:38 +03:00
export default SelectSingle;