import * as React from 'react'; import { TextControl, tailwind } from '@tesseract-design/web-base'; import { useFallbackId } from '@modal-sh/react-utils'; const { tw } = tailwind; const DropdownSelectDerivedElementComponent = 'select' as const; /** * Derived HTML element of the {@link DropdownSelect} component. */ export type DropdownSelectDerivedElement = HTMLElementTagNameMap[ typeof DropdownSelectDerivedElementComponent ]; /** * Props of the {@link DropdownSelect} component. */ export interface DropdownSelectProps extends Omit, 'size' | 'type' | 'label' | 'list' | 'multiple'> { /** * Short textual description indicating the nature of the component's value. */ label?: React.ReactNode, /** * Short textual description as guidelines for valid input values. */ hint?: React.ReactNode, /** * Size of the component. */ size?: TextControl.Size, /** * Should the component display a border? */ border?: boolean, /** * Should the component occupy the whole width of its parent? */ block?: boolean, /** * Style of the component. */ variant?: TextControl.Variant, /** * Is the label hidden? */ hiddenLabel?: boolean, } export const dropdownSelectPlugin: tailwind.PluginCreator = ({ addComponents }) => { addComponents({ '.dropdown-select': { '& optgroup': { 'color': 'rgb(var(--color-positive) / 50%)', 'text-transform': 'uppercase', 'font-size': '0.75em', 'margin-top': '0.5rem', 'user-select': 'none', }, '& optgroup > option': { 'color': 'rgb(var(--color-positive))', 'text-transform': 'none', 'font-size': '1.333333em', }, '& option': { 'user-select': 'none', }, }, }); }; // todo remove bg-negative /** * Component for selecting a single value from a dropdown. */ export const DropdownSelect = React.forwardRef(( { label, hint, size = 'medium' as const, border = false as const, block = false as const, variant = 'default' as const, hiddenLabel = false as const, className, children, id: idProp, style, ...etcProps }: DropdownSelectProps, forwardedRef, ) => { const labelId = React.useId(); const id = useFallbackId(idProp); return (
{label && ( <> {' '} )} {children} {hint && (
{hint}
)}
{border && ( )}
); }); DropdownSelect.displayName = 'DropdownSelect' as const; DropdownSelect.defaultProps = { label: undefined, hint: undefined, size: 'medium' as const, border: false as const, block: false as const, variant: 'default' as const, hiddenLabel: false as const, };