1
0
Fork 1
mirror of https://github.com/thatmattlove/hyperglass.git synced 2026-04-17 21:38:27 +00:00

fix render error when rendering select element; change max device per group count to 9; closes #237

This commit is contained in:
thatmattlove 2024-03-20 17:47:53 -04:00
parent e8ff239fdb
commit cea421c939
3 changed files with 33 additions and 22 deletions

View file

@ -1,13 +1,13 @@
import { Flex, Stack, Wrap, chakra } from '@chakra-ui/react';
import { useMemo } from 'react'; import { useMemo } from 'react';
import { Wrap, Stack, Flex, chakra } from '@chakra-ui/react';
import { useFormContext } from 'react-hook-form'; import { useFormContext } from 'react-hook-form';
import { Select, LocationCard } from '~/components'; import { LocationCard, Select } from '~/components';
import { isMultiValue, isSingleValue } from '~/components/select';
import { useConfig } from '~/context'; import { useConfig } from '~/context';
import { useFormState } from '~/hooks'; import { useFormState } from '~/hooks';
import { isMultiValue, isSingleValue } from '~/components/select';
import type { DeviceGroup, SingleOption, OptionGroup, FormData, OnChangeArgs } from '~/types';
import type { SelectOnChange } from '~/components/select'; import type { SelectOnChange } from '~/components/select';
import type { DeviceGroup, FormData, OnChangeArgs, OptionGroup, SingleOption } from '~/types';
/** Location option type alias for future extensions. */ /** Location option type alias for future extensions. */
export type LocationOption = SingleOption; export type LocationOption = SingleOption;
@ -66,7 +66,7 @@ export const QueryLocation = (props: QueryLocationProps): JSX.Element => {
} }
const groups = options.length; const groups = options.length;
const maxOptionsPerGroup = Math.max(...options.map(opt => opt.options.length)); const maxOptionsPerGroup = Math.max(...options.map(opt => opt.options.length));
const showCards = groups < 5 && maxOptionsPerGroup < 6; const showCards = groups < 5 && maxOptionsPerGroup < 9;
return showCards ? 'cards' : 'select'; return showCards ? 'cards' : 'select';
}, [options, locationDisplayMode]); }, [options, locationDisplayMode]);

View file

@ -1,32 +1,33 @@
import { useDisclosure } from '@chakra-ui/react';
import { createContext, forwardRef, useContext } from 'react'; import { createContext, forwardRef, useContext } from 'react';
import ReactSelect from 'react-select'; import ReactSelect from 'react-select';
import { useDisclosure } from '@chakra-ui/react';
import { useColorMode } from '~/hooks'; import { useColorMode } from '~/hooks';
import { Option } from './option'; import { Option } from './option';
import { import {
useRSTheme, useContainerStyle,
useMenuStyle,
useMenuPortal,
useOptionStyle,
useControlStyle, useControlStyle,
useIndicatorSeparatorStyle,
useMenuListStyle, useMenuListStyle,
useMultiValueStyle, useMenuPortal,
usePlaceholderStyle, useMenuStyle,
useSingleValueStyle,
useMultiValueLabelStyle, useMultiValueLabelStyle,
useMultiValueRemoveStyle, useMultiValueRemoveStyle,
useIndicatorSeparatorStyle, useMultiValueStyle,
useOptionStyle,
usePlaceholderStyle,
useRSTheme,
useSingleValueStyle,
} from './styles'; } from './styles';
import { isSingleValue } from './types'; import { isSingleValue } from './types';
import type { import type {
Props as ReactSelectProps,
MultiValue, MultiValue,
OnChangeValue, OnChangeValue,
Props as ReactSelectProps,
SelectInstance, SelectInstance,
} from 'react-select'; } from 'react-select';
import type { SingleOption } from '~/types'; import type { SingleOption } from '~/types';
import type { SelectProps, SelectContextProps } from './types'; import type { SelectContextProps, SelectProps } from './types';
const SelectContext = createContext<SelectContextProps>({} as SelectContextProps); const SelectContext = createContext<SelectContextProps>({} as SelectContextProps);
export const useSelectContext = (): SelectContextProps => useContext(SelectContext); export const useSelectContext = (): SelectContextProps => useContext(SelectContext);
@ -51,6 +52,7 @@ export const Select = forwardRef(
} }
}; };
const container = useContainerStyle<Opt, IsMulti>({ colorMode });
const menu = useMenuStyle<Opt, IsMulti>({ colorMode }); const menu = useMenuStyle<Opt, IsMulti>({ colorMode });
const menuList = useMenuListStyle<Opt, IsMulti>({ colorMode }); const menuList = useMenuListStyle<Opt, IsMulti>({ colorMode });
const control = useControlStyle<Opt, IsMulti>({ colorMode }); const control = useControlStyle<Opt, IsMulti>({ colorMode });
@ -75,9 +77,10 @@ export const Select = forwardRef(
isMulti={isMulti} isMulti={isMulti}
theme={rsTheme} theme={rsTheme}
components={{ Option, ...components }} components={{ Option, ...components }}
menuPortalTarget={document?.body ?? undefined} menuPortalTarget={typeof document !== 'undefined' ? document.body : undefined}
ref={ref} ref={ref}
styles={{ styles={{
container,
menu, menu,
option, option,
control, control,

View file

@ -1,11 +1,11 @@
/* eslint-disable react-hooks/exhaustive-deps */
import { useCallback } from 'react';
import { useToken } from '@chakra-ui/react'; import { useToken } from '@chakra-ui/react';
import { mergeWith } from '@chakra-ui/utils'; import { mergeWith } from '@chakra-ui/utils';
/* eslint-disable react-hooks/exhaustive-deps */
import { useCallback } from 'react';
import { import {
useMobile,
useColorValue,
useColorToken, useColorToken,
useColorValue,
useMobile,
useOpposingColor, useOpposingColor,
useOpposingColorCallback, useOpposingColorCallback,
} from '~/hooks'; } from '~/hooks';
@ -13,7 +13,15 @@ import { useSelectContext } from './select';
import * as ReactSelect from 'react-select'; import * as ReactSelect from 'react-select';
import type { SingleOption } from '~/types'; import type { SingleOption } from '~/types';
import type { RSStyleCallbackProps, RSThemeFunction, RSStyleFunction } from './types'; import type { RSStyleCallbackProps, RSStyleFunction, RSThemeFunction } from './types';
export const useContainerStyle = <Opt extends SingleOption, IsMulti extends boolean>(
props: RSStyleCallbackProps,
): RSStyleFunction<'container', Opt, IsMulti> => {
return useCallback((base, state) => {
return { width: '100%' };
}, []);
};
export const useControlStyle = <Opt extends SingleOption, IsMulti extends boolean>( export const useControlStyle = <Opt extends SingleOption, IsMulti extends boolean>(
props: RSStyleCallbackProps, props: RSStyleCallbackProps,