Fix react-select scaling

This commit is contained in:
IRBorisov 2024-04-22 12:33:22 +03:00
parent 50e60010b0
commit 550a5e2892
3 changed files with 111 additions and 7 deletions

View File

@ -1,11 +1,45 @@
'use client'; 'use client';
import { useMemo } from 'react'; import { useMemo } from 'react';
import Select, { GroupBase, Props, StylesConfig } from 'react-select'; import Select, {
ClearIndicatorProps,
components,
DropdownIndicatorProps,
GroupBase,
Props,
StylesConfig
} from 'react-select';
import { useConceptOptions } from '@/context/OptionsContext'; import { useConceptOptions } from '@/context/OptionsContext';
import useWindowSize from '@/hooks/useWindowSize';
import { selectDarkT, selectLightT } from '@/styling/color'; import { selectDarkT, selectLightT } from '@/styling/color';
import { IconClose, IconDropArrow, IconDropArrowUp } from '../Icons';
function DropdownIndicator<Option, Group extends GroupBase<Option> = GroupBase<Option>>(
props: DropdownIndicatorProps<Option, true, Group>
) {
return (
components.DropdownIndicator && (
<components.DropdownIndicator {...props}>
{props.selectProps.menuIsOpen ? <IconDropArrow size='1.25rem' /> : <IconDropArrowUp size='1.25rem' />}
</components.DropdownIndicator>
)
);
}
function ClearIndicator<Option, Group extends GroupBase<Option> = GroupBase<Option>>(
props: ClearIndicatorProps<Option, true, Group>
) {
return (
components.ClearIndicator && (
<components.ClearIndicator {...props}>
<IconClose size='1.25rem' />
</components.ClearIndicator>
)
);
}
export interface SelectMultiProps<Option, Group extends GroupBase<Option> = GroupBase<Option>> export interface SelectMultiProps<Option, Group extends GroupBase<Option> = GroupBase<Option>>
extends Omit<Props<Option, true, Group>, 'theme' | 'menuPortalTarget'> { extends Omit<Props<Option, true, Group>, 'theme' | 'menuPortalTarget'> {
noPortal?: boolean; noPortal?: boolean;
@ -16,6 +50,7 @@ function SelectMulti<Option, Group extends GroupBase<Option> = GroupBase<Option>
...restProps ...restProps
}: SelectMultiProps<Option, Group>) { }: SelectMultiProps<Option, Group>) {
const { darkMode, colors } = useConceptOptions(); const { darkMode, colors } = useConceptOptions();
const size = useWindowSize();
const themeColors = useMemo(() => (!darkMode ? selectLightT : selectDarkT), [darkMode]); const themeColors = useMemo(() => (!darkMode ? selectLightT : selectDarkT), [darkMode]);
const adjustedStyles: StylesConfig<Option, true, Group> = useMemo( const adjustedStyles: StylesConfig<Option, true, Group> = useMemo(
@ -39,7 +74,7 @@ function SelectMulti<Option, Group extends GroupBase<Option> = GroupBase<Option>
}), }),
menuList: styles => ({ menuList: styles => ({
...styles, ...styles,
padding: '0px' padding: 0
}), }),
input: styles => ({ ...styles }), input: styles => ({ ...styles }),
placeholder: styles => ({ ...styles }), placeholder: styles => ({ ...styles }),
@ -47,6 +82,16 @@ function SelectMulti<Option, Group extends GroupBase<Option> = GroupBase<Option>
...styles, ...styles,
borderRadius: '0.5rem', borderRadius: '0.5rem',
backgroundColor: colors.bgSelected backgroundColor: colors.bgSelected
}),
dropdownIndicator: base => ({
...base,
paddingTop: 0,
paddingBottom: 0
}),
clearIndicator: base => ({
...base,
paddingTop: 0,
paddingBottom: 0
}) })
}), }),
[colors] [colors]
@ -56,9 +101,16 @@ function SelectMulti<Option, Group extends GroupBase<Option> = GroupBase<Option>
<Select <Select
isMulti isMulti
noOptionsMessage={() => 'Список пуст'} noOptionsMessage={() => 'Список пуст'}
components={{ DropdownIndicator, ClearIndicator }}
theme={theme => ({ theme={theme => ({
...theme, ...theme,
borderRadius: 0, borderRadius: 0,
spacing: {
...theme.spacing, // prettier: split-lines
baseUnit: size.isSmall ? 2 : 4,
menuGutter: size.isSmall ? 4 : 8,
controlHeight: size.isSmall ? 28 : 38
},
colors: { colors: {
...theme.colors, ...theme.colors,
...themeColors ...themeColors

View File

@ -1,12 +1,46 @@
'use client'; 'use client';
import { useMemo } from 'react'; import { useMemo } from 'react';
import Select, { GroupBase, Props, StylesConfig } from 'react-select'; import Select, {
ClearIndicatorProps,
components,
DropdownIndicatorProps,
GroupBase,
Props,
StylesConfig
} from 'react-select';
import { useConceptOptions } from '@/context/OptionsContext'; import { useConceptOptions } from '@/context/OptionsContext';
import useWindowSize from '@/hooks/useWindowSize';
import { selectDarkT, selectLightT } from '@/styling/color'; import { selectDarkT, selectLightT } from '@/styling/color';
interface SelectSingleProps<Option, Group extends GroupBase<Option> = GroupBase<Option>> import { IconClose, IconDropArrow, IconDropArrowUp } from '../Icons';
function DropdownIndicator<Option, Group extends GroupBase<Option> = GroupBase<Option>>(
props: DropdownIndicatorProps<Option, false, Group>
) {
return (
components.DropdownIndicator && (
<components.DropdownIndicator {...props}>
{props.selectProps.menuIsOpen ? <IconDropArrow size='1.25rem' /> : <IconDropArrowUp size='1.25rem' />}
</components.DropdownIndicator>
)
);
}
function ClearIndicator<Option, Group extends GroupBase<Option> = GroupBase<Option>>(
props: ClearIndicatorProps<Option, false, Group>
) {
return (
components.ClearIndicator && (
<components.ClearIndicator {...props}>
<IconClose size='1.25rem' />
</components.ClearIndicator>
)
);
}
export interface SelectSingleProps<Option, Group extends GroupBase<Option> = GroupBase<Option>>
extends Omit<Props<Option, false, Group>, 'theme' | 'menuPortalTarget'> { extends Omit<Props<Option, false, Group>, 'theme' | 'menuPortalTarget'> {
noPortal?: boolean; noPortal?: boolean;
noBorder?: boolean; noBorder?: boolean;
@ -18,6 +52,7 @@ function SelectSingle<Option, Group extends GroupBase<Option> = GroupBase<Option
...restProps ...restProps
}: SelectSingleProps<Option, Group>) { }: SelectSingleProps<Option, Group>) {
const { darkMode, colors } = useConceptOptions(); const { darkMode, colors } = useConceptOptions();
const size = useWindowSize();
const themeColors = useMemo(() => (!darkMode ? selectLightT : selectDarkT), [darkMode]); const themeColors = useMemo(() => (!darkMode ? selectLightT : selectDarkT), [darkMode]);
const adjustedStyles: StylesConfig<Option, false, Group> = useMemo( const adjustedStyles: StylesConfig<Option, false, Group> = useMemo(
@ -35,7 +70,7 @@ function SelectSingle<Option, Group extends GroupBase<Option> = GroupBase<Option
}), }),
menuList: defaultStyles => ({ menuList: defaultStyles => ({
...defaultStyles, ...defaultStyles,
padding: '0px' padding: 0
}), }),
option: (defaultStyles, { isSelected }) => ({ option: (defaultStyles, { isSelected }) => ({
...defaultStyles, ...defaultStyles,
@ -46,7 +81,17 @@ function SelectSingle<Option, Group extends GroupBase<Option> = GroupBase<Option
}), }),
input: defaultStyles => ({ ...defaultStyles }), input: defaultStyles => ({ ...defaultStyles }),
placeholder: defaultStyles => ({ ...defaultStyles }), placeholder: defaultStyles => ({ ...defaultStyles }),
singleValue: defaultStyles => ({ ...defaultStyles }) singleValue: defaultStyles => ({ ...defaultStyles }),
dropdownIndicator: base => ({
...base,
paddingTop: 0,
paddingBottom: 0
}),
clearIndicator: base => ({
...base,
paddingTop: 0,
paddingBottom: 0
})
}), }),
[colors, noBorder] [colors, noBorder]
); );
@ -54,9 +99,16 @@ function SelectSingle<Option, Group extends GroupBase<Option> = GroupBase<Option
return ( return (
<Select <Select
noOptionsMessage={() => 'Список пуст'} noOptionsMessage={() => 'Список пуст'}
components={{ DropdownIndicator, ClearIndicator }}
theme={theme => ({ theme={theme => ({
...theme, ...theme,
borderRadius: 0, borderRadius: 0,
spacing: {
...theme.spacing, // prettier: split-lines
baseUnit: size.isSmall ? 2 : 4,
menuGutter: size.isSmall ? 4 : 8,
controlHeight: size.isSmall ? 28 : 38
},
colors: { colors: {
...theme.colors, ...theme.colors,
...themeColors ...themeColors

View File

@ -51,7 +51,7 @@ function ViewHidden({ items, selected, toggleSelection, setFocus, schema, colori
} }
return ( return (
<div className='flex flex-col'> <div className='flex flex-col'>
<Overlay position='right-[calc(0.5rem+1px)] top-2'> <Overlay position='right-[calc(0.7rem-2px)] top-2'>
<MiniButton <MiniButton
noPadding noPadding
noHover noHover