"use client"
import type { CollectionItem } from "@chakra-ui/react"
import { Select as ChakraSelect, Portal } from "@chakra-ui/react"
import { CloseButton } from "compositions/ui/close-button"
import * as React from "react"
interface SelectTriggerProps extends ChakraSelect.ControlProps {
clearable?: boolean
}
export const SelectTrigger = React.forwardRef<
HTMLButtonElement,
SelectTriggerProps
>(function SelectTrigger(props, ref) {
const { children, clearable, ...rest } = props
return (
{children}
{clearable && }
)
})
const SelectClearTrigger = React.forwardRef<
HTMLButtonElement,
ChakraSelect.ClearTriggerProps
>(function SelectClearTrigger(props, ref) {
return (
)
})
interface SelectContentProps extends ChakraSelect.ContentProps {
portalled?: boolean
portalRef?: React.RefObject
}
export const SelectContent = React.forwardRef<
HTMLDivElement,
SelectContentProps
>(function SelectContent(props, ref) {
const { portalled = true, portalRef, ...rest } = props
return (
)
})
export const SelectItem = React.forwardRef<
HTMLDivElement,
ChakraSelect.ItemProps
>(function SelectItem(props, ref) {
const { item, children, ...rest } = props
return (
{children}
)
})
interface SelectValueTextProps
extends Omit {
children?(items: CollectionItem[]): React.ReactNode
}
export const SelectValueText = React.forwardRef<
HTMLSpanElement,
SelectValueTextProps
>(function SelectValueText(props, ref) {
const { children, ...rest } = props
return (
{(select) => {
const items = select.selectedItems
if (items.length === 0) return props.placeholder
if (children) return children(items)
if (items.length === 1)
return select.collection.stringifyItem(items[0])
return `${items.length} selected`
}}
)
})
export const SelectRoot = React.forwardRef<
HTMLDivElement,
ChakraSelect.RootProps
>(function SelectRoot(props, ref) {
return (
{props.asChild ? (
props.children
) : (
<>
{props.children}
>
)}
)
}) as ChakraSelect.RootComponent
interface SelectItemGroupProps extends ChakraSelect.ItemGroupProps {
label: React.ReactNode
}
export const SelectItemGroup = React.forwardRef<
HTMLDivElement,
SelectItemGroupProps
>(function SelectItemGroup(props, ref) {
const { children, label, ...rest } = props
return (
{label}
{children}
)
})
export const SelectLabel = ChakraSelect.Label
export const SelectItemText = ChakraSelect.ItemText