|
"content": "\"use client\"\n\nimport type { CollectionItem } from \"@chakra-ui/react\"\nimport { Select as ChakraSelect, Portal } from \"@chakra-ui/react\"\nimport { CloseButton } from \"./close-button\"\nimport * as React from \"react\"\n\ninterface SelectTriggerProps extends ChakraSelect.ControlProps {\n clearable?: boolean\n}\n\nexport const SelectTrigger = React.forwardRef<\n HTMLButtonElement,\n SelectTriggerProps\n>(function SelectTrigger(props, ref) {\n const { children, clearable, ...rest } = props\n return (\n <ChakraSelect.Control {...rest}>\n <ChakraSelect.Trigger ref={ref}>{children}</ChakraSelect.Trigger>\n <ChakraSelect.IndicatorGroup>\n {clearable && <SelectClearTrigger />}\n <ChakraSelect.Indicator />\n </ChakraSelect.IndicatorGroup>\n </ChakraSelect.Control>\n )\n})\n\nconst SelectClearTrigger = React.forwardRef<\n HTMLButtonElement,\n ChakraSelect.ClearTriggerProps\n>(function SelectClearTrigger(props, ref) {\n return (\n <ChakraSelect.ClearTrigger asChild {...props} ref={ref}>\n <CloseButton\n size=\"xs\"\n variant=\"plain\"\n focusVisibleRing=\"inside\"\n focusRingWidth=\"2px\"\n pointerEvents=\"auto\"\n />\n </ChakraSelect.ClearTrigger>\n )\n})\n\ninterface SelectContentProps extends ChakraSelect.ContentProps {\n portalled?: boolean\n portalRef?: React.RefObject<HTMLElement>\n}\n\nexport const SelectContent = React.forwardRef<\n HTMLDivElement,\n SelectContentProps\n>(function SelectContent(props, ref) {\n const { portalled = true, portalRef, ...rest } = props\n return (\n <Portal disabled={!portalled} container={portalRef}>\n <ChakraSelect.Positioner>\n <ChakraSelect.Content {...rest} ref={ref} />\n </ChakraSelect.Positioner>\n </Portal>\n )\n})\n\nexport const SelectItem = React.forwardRef<\n HTMLDivElement,\n ChakraSelect.ItemProps\n>(function SelectItem(props, ref) {\n const { item, children, ...rest } = props\n return (\n <ChakraSelect.Item key={item.value} item={item} {...rest} ref={ref}>\n {children}\n <ChakraSelect.ItemIndicator />\n </ChakraSelect.Item>\n )\n})\n\ninterface SelectValueTextProps\n extends Omit<ChakraSelect.ValueTextProps, \"children\"> {\n children?(items: CollectionItem[]): React.ReactNode\n}\n\nexport const SelectValueText = React.forwardRef<\n HTMLSpanElement,\n SelectValueTextProps\n>(function SelectValueText(props, ref) {\n const { children, ...rest } = props\n return (\n <ChakraSelect.ValueText {...rest} ref={ref}>\n <ChakraSelect.Context>\n {(select) => {\n const items = select.selectedItems\n if (items.length === 0) return props.placeholder\n if (children) return children(items)\n if (items.length === 1)\n return select.collection.stringifyItem(items[0])\n return `${items.length} selected`\n }}\n </ChakraSelect.Context>\n </ChakraSelect.ValueText>\n )\n})\n\nexport const SelectRoot = React.forwardRef<\n HTMLDivElement,\n ChakraSelect.RootProps\n>(function SelectRoot(props, ref) {\n return (\n <ChakraSelect.Root\n {...props}\n ref={ref}\n positioning={{ sameWidth: true, ...props.positioning }}\n >\n {props.asChild ? (\n props.children\n ) : (\n <>\n <ChakraSelect.HiddenSelect />\n {props.children}\n </>\n )}\n </ChakraSelect.Root>\n )\n}) as ChakraSelect.RootComponent\n\ninterface SelectItemGroupProps extends ChakraSelect.ItemGroupProps {\n label: React.ReactNode\n}\n\nexport const SelectItemGroup = React.forwardRef<\n HTMLDivElement,\n SelectItemGroupProps\n>(function SelectItemGroup(props, ref) {\n const { children, label, ...rest } = props\n return (\n <ChakraSelect.ItemGroup {...rest} ref={ref}>\n <ChakraSelect.ItemGroupLabel>{label}</ChakraSelect.ItemGroupLabel>\n {children}\n </ChakraSelect.ItemGroup>\n )\n})\n\nexport const SelectLabel = ChakraSelect.Label\nexport const SelectItemText = ChakraSelect.ItemText\n" |