|
1 | | -export function Select() { |
2 | | - return <></> |
| 1 | +'use client' |
| 2 | + |
| 3 | +import { Box, css, Flex, VStack } from '@devup-ui/react' |
| 4 | +import clsx from 'clsx' |
| 5 | +import { ComponentProps, createContext, useContext, useState } from 'react' |
| 6 | + |
| 7 | +import { Button } from '../Button' |
| 8 | + |
| 9 | +interface SelectProps { |
| 10 | + open?: boolean |
| 11 | + onOpenChange?: (open: boolean) => void |
| 12 | + children: React.ReactNode |
| 13 | +} |
| 14 | + |
| 15 | +const SelectContext = createContext<{ |
| 16 | + open: boolean |
| 17 | + setOpen: (open: boolean) => void |
| 18 | +} | null>(null) |
| 19 | + |
| 20 | +export const useSelect = () => { |
| 21 | + const context = useContext(SelectContext) |
| 22 | + if (!context) { |
| 23 | + throw new Error('useSelect must be used within a Select') |
| 24 | + } |
| 25 | + return context |
| 26 | +} |
| 27 | + |
| 28 | +export function Select({ |
| 29 | + children, |
| 30 | + open: openProp, |
| 31 | + onOpenChange, |
| 32 | +}: SelectProps) { |
| 33 | + const [open, setOpen] = useState(openProp ?? false) |
| 34 | + const handleOpenChange = (open: boolean) => { |
| 35 | + setOpen(open) |
| 36 | + onOpenChange?.(open) |
| 37 | + } |
| 38 | + return ( |
| 39 | + <SelectContext.Provider value={{ open, setOpen: handleOpenChange }}> |
| 40 | + <Box display="inline-block" pos="relative"> |
| 41 | + {children} |
| 42 | + </Box> |
| 43 | + </SelectContext.Provider> |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +export function SelectTrigger({ |
| 48 | + className, |
| 49 | + children, |
| 50 | + ...props |
| 51 | +}: ComponentProps<typeof Button>) { |
| 52 | + const { open, setOpen } = useSelect() |
| 53 | + const handleClick = () => { |
| 54 | + setOpen(!open) |
| 55 | + } |
| 56 | + |
| 57 | + return ( |
| 58 | + <Button |
| 59 | + className={clsx( |
| 60 | + css({ |
| 61 | + pos: 'relative', |
| 62 | + borderRadius: '8px', |
| 63 | + }), |
| 64 | + className, |
| 65 | + )} |
| 66 | + onClick={handleClick} |
| 67 | + {...props} |
| 68 | + > |
| 69 | + {children} |
| 70 | + </Button> |
| 71 | + ) |
| 72 | +} |
| 73 | + |
| 74 | +export function SelectContainer({ children, ...props }: ComponentProps<'div'>) { |
| 75 | + const { open } = useSelect() |
| 76 | + if (!open) return null |
| 77 | + return ( |
| 78 | + <VStack |
| 79 | + bg="$inputBg" |
| 80 | + border="1px solid $border" |
| 81 | + borderRadius="8px" |
| 82 | + bottom="-4px" |
| 83 | + boxShadow="0 2px 2px 0 $base10" |
| 84 | + gap="6px" |
| 85 | + p="10px" |
| 86 | + pos="absolute" |
| 87 | + styleOrder={1} |
| 88 | + transform="translateY(100%)" |
| 89 | + userSelect="none" |
| 90 | + w="232px" |
| 91 | + {...props} |
| 92 | + > |
| 93 | + {children} |
| 94 | + </VStack> |
| 95 | + ) |
| 96 | +} |
| 97 | + |
| 98 | +interface SelectOptionProps extends ComponentProps<'div'> { |
| 99 | + disabled?: boolean |
| 100 | +} |
| 101 | + |
| 102 | +export function SelectOption({ |
| 103 | + disabled, |
| 104 | + onClick, |
| 105 | + children, |
| 106 | + ...props |
| 107 | +}: SelectOptionProps) { |
| 108 | + const { setOpen } = useSelect() |
| 109 | + const handleClick = (e: React.MouseEvent<HTMLDivElement>) => { |
| 110 | + if (onClick) { |
| 111 | + onClick(e) |
| 112 | + return |
| 113 | + } |
| 114 | + setOpen(false) |
| 115 | + } |
| 116 | + |
| 117 | + return ( |
| 118 | + <Flex |
| 119 | + _hover={ |
| 120 | + !disabled && { |
| 121 | + bg: '$primaryBg', |
| 122 | + } |
| 123 | + } |
| 124 | + alignItems="center" |
| 125 | + borderRadius="8px" |
| 126 | + color={disabled ? '$selectDisabled' : '$title'} |
| 127 | + cursor={disabled ? 'default' : 'pointer'} |
| 128 | + h="40px" |
| 129 | + onClick={disabled ? undefined : handleClick} |
| 130 | + px="10px" |
| 131 | + styleOrder={1} |
| 132 | + transition="background-color 0.1s ease-in-out" |
| 133 | + typography="inputText" |
| 134 | + {...props} |
| 135 | + > |
| 136 | + {children} |
| 137 | + </Flex> |
| 138 | + ) |
| 139 | +} |
| 140 | + |
| 141 | +export function SelectDivider({ ...props }: ComponentProps<'div'>) { |
| 142 | + return <Box bg="$border" h="1px" styleOrder={1} w="100%" {...props} /> |
3 | 143 | } |
0 commit comments