Autoresize InputDropdown

This commit is contained in:
David Corbitt
2023-08-15 00:27:12 -07:00
parent 6e7efee21e
commit 2bcb1d16a3
2 changed files with 6 additions and 16 deletions

View File

@@ -11,8 +11,6 @@ import {
Button, Button,
Text, Text,
useDisclosure, useDisclosure,
type PopoverContentProps,
type InputGroupProps,
} from "@chakra-ui/react"; } from "@chakra-ui/react";
import { FiChevronDown } from "react-icons/fi"; import { FiChevronDown } from "react-icons/fi";
@@ -22,27 +20,20 @@ type InputDropdownProps<T> = {
options: ReadonlyArray<T>; options: ReadonlyArray<T>;
selectedOption: T; selectedOption: T;
onSelect: (option: T) => void; onSelect: (option: T) => void;
inputGroupProps?: InputGroupProps;
popoverContentProps?: PopoverContentProps;
}; };
const InputDropdown = <T,>({ const InputDropdown = <T,>({ options, selectedOption, onSelect }: InputDropdownProps<T>) => {
options,
selectedOption,
onSelect,
inputGroupProps,
popoverContentProps,
}: InputDropdownProps<T>) => {
const popover = useDisclosure(); const popover = useDisclosure();
return ( return (
<Popover placement="bottom-start" {...popover}> <Popover placement="bottom-start" {...popover}>
<PopoverTrigger> <PopoverTrigger>
<InputGroup cursor="pointer" w={360} {...inputGroupProps}> <InputGroup cursor="pointer" w={(selectedOption as string).length * 14 + 180}>
<Input <Input
value={selectedOption as string} value={selectedOption as string}
// eslint-disable-next-line @typescript-eslint/no-empty-function -- controlled input requires onChange
onChange={() => {}}
cursor="pointer" cursor="pointer"
borderWidth={popover.isOpen ? 2 : undefined}
borderColor={popover.isOpen ? "blue.500" : undefined} borderColor={popover.isOpen ? "blue.500" : undefined}
_hover={popover.isOpen ? { borderColor: "blue.500" } : undefined} _hover={popover.isOpen ? { borderColor: "blue.500" } : undefined}
contentEditable={false} contentEditable={false}
@@ -56,7 +47,7 @@ const InputDropdown = <T,>({
</InputRightElement> </InputRightElement>
</InputGroup> </InputGroup>
</PopoverTrigger> </PopoverTrigger>
<PopoverContent boxShadow="0 0 40px 4px rgba(0, 0, 0, 0.1);" w={224} {...popoverContentProps}> <PopoverContent boxShadow="0 0 40px 4px rgba(0, 0, 0, 0.1);" minW={0} w="auto">
<VStack spacing={0}> <VStack spacing={0}>
{options?.map((option, index) => ( {options?.map((option, index) => (
<HStack <HStack
@@ -76,7 +67,7 @@ const InputDropdown = <T,>({
fontSize="sm" fontSize="sm"
borderBottomWidth={1} borderBottomWidth={1}
> >
<Text>{option as string}</Text> <Text mr={16}>{option as string}</Text>
{option === selectedOption && <Icon as={BiCheck} color="blue.500" boxSize={5} />} {option === selectedOption && <Icon as={BiCheck} color="blue.500" boxSize={5} />}
</HStack> </HStack>
))} ))}

View File

@@ -12,7 +12,6 @@ const SelectComparatorDropdown = ({ filter, index }: { filter: LogFilter; index:
options={comparators} options={comparators}
selectedOption={comparator} selectedOption={comparator}
onSelect={(option) => updateFilter(index, { ...filter, comparator: option })} onSelect={(option) => updateFilter(index, { ...filter, comparator: option })}
inputGroupProps={{ w: 300 }}
/> />
); );
}; };