|
|
@ -20,11 +20,13 @@ type Props<T> = Readonly<{
|
|
|
|
borderStyle?: SelectBorderStyle;
|
|
|
|
borderStyle?: SelectBorderStyle;
|
|
|
|
defaultValue?: T;
|
|
|
|
defaultValue?: T;
|
|
|
|
display?: SelectDisplay;
|
|
|
|
display?: SelectDisplay;
|
|
|
|
|
|
|
|
errorMessage?: string;
|
|
|
|
isLabelHidden?: boolean;
|
|
|
|
isLabelHidden?: boolean;
|
|
|
|
label: string;
|
|
|
|
label: string;
|
|
|
|
name?: string;
|
|
|
|
name?: string;
|
|
|
|
onChange?: (value: string) => void;
|
|
|
|
onChange?: (value: string) => void;
|
|
|
|
options: ReadonlyArray<SelectItem<T>>;
|
|
|
|
options: ReadonlyArray<SelectItem<T>>;
|
|
|
|
|
|
|
|
placeholder?: string;
|
|
|
|
value?: T;
|
|
|
|
value?: T;
|
|
|
|
}> &
|
|
|
|
}> &
|
|
|
|
Readonly<Attributes>;
|
|
|
|
Readonly<Attributes>;
|
|
|
@ -34,15 +36,25 @@ const borderClasses: Record<SelectBorderStyle, string> = {
|
|
|
|
borderless: 'border-transparent bg-transparent',
|
|
|
|
borderless: 'border-transparent bg-transparent',
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type State = 'error' | 'normal';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const stateClasses: Record<State, string> = {
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
|
|
|
'border-danger-300 text-danger-900 placeholder-danger-300 focus:outline-none focus:ring-danger-500 focus:border-danger-500',
|
|
|
|
|
|
|
|
normal: 'focus:border-primary-500 focus:ring-primary-500',
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function Select<T>(
|
|
|
|
function Select<T>(
|
|
|
|
{
|
|
|
|
{
|
|
|
|
borderStyle = 'bordered',
|
|
|
|
borderStyle = 'bordered',
|
|
|
|
defaultValue,
|
|
|
|
defaultValue,
|
|
|
|
display,
|
|
|
|
display,
|
|
|
|
disabled,
|
|
|
|
disabled,
|
|
|
|
|
|
|
|
errorMessage,
|
|
|
|
label,
|
|
|
|
label,
|
|
|
|
isLabelHidden,
|
|
|
|
isLabelHidden,
|
|
|
|
options,
|
|
|
|
options,
|
|
|
|
|
|
|
|
placeholder,
|
|
|
|
required,
|
|
|
|
required,
|
|
|
|
value,
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
onChange,
|
|
|
@ -50,7 +62,10 @@ function Select<T>(
|
|
|
|
}: Props<T>,
|
|
|
|
}: Props<T>,
|
|
|
|
ref: ForwardedRef<HTMLSelectElement>,
|
|
|
|
ref: ForwardedRef<HTMLSelectElement>,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
|
|
|
|
const hasError = errorMessage != null;
|
|
|
|
const id = useId();
|
|
|
|
const id = useId();
|
|
|
|
|
|
|
|
const errorId = useId();
|
|
|
|
|
|
|
|
const state: State = hasError ? 'error' : 'normal';
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div>
|
|
|
@ -69,10 +84,12 @@ function Select<T>(
|
|
|
|
)}
|
|
|
|
)}
|
|
|
|
<select
|
|
|
|
<select
|
|
|
|
ref={ref}
|
|
|
|
ref={ref}
|
|
|
|
|
|
|
|
aria-describedby={hasError ? errorId : undefined}
|
|
|
|
aria-label={isLabelHidden ? label : undefined}
|
|
|
|
aria-label={isLabelHidden ? label : undefined}
|
|
|
|
className={clsx(
|
|
|
|
className={clsx(
|
|
|
|
display === 'block' && 'block w-full',
|
|
|
|
display === 'block' && 'block w-full',
|
|
|
|
'focus:border-primary-500 focus:ring-primary-500 rounded-md py-2 pl-3 pr-8 text-base focus:outline-none sm:text-sm',
|
|
|
|
'rounded-md py-2 pl-3 pr-8 text-base focus:outline-none sm:text-sm',
|
|
|
|
|
|
|
|
stateClasses[state],
|
|
|
|
borderClasses[borderStyle],
|
|
|
|
borderClasses[borderStyle],
|
|
|
|
disabled && 'bg-slate-100',
|
|
|
|
disabled && 'bg-slate-100',
|
|
|
|
)}
|
|
|
|
)}
|
|
|
@ -85,12 +102,22 @@ function Select<T>(
|
|
|
|
onChange?.(event.target.value);
|
|
|
|
onChange?.(event.target.value);
|
|
|
|
}}
|
|
|
|
}}
|
|
|
|
{...props}>
|
|
|
|
{...props}>
|
|
|
|
|
|
|
|
{placeholder && (
|
|
|
|
|
|
|
|
<option disabled={true} hidden={true} selected={true} value="">
|
|
|
|
|
|
|
|
{placeholder}
|
|
|
|
|
|
|
|
</option>
|
|
|
|
|
|
|
|
)}
|
|
|
|
{options.map(({ label: optionLabel, value: optionValue }) => (
|
|
|
|
{options.map(({ label: optionLabel, value: optionValue }) => (
|
|
|
|
<option key={String(optionValue)} value={String(optionValue)}>
|
|
|
|
<option key={String(optionValue)} value={String(optionValue)}>
|
|
|
|
{optionLabel}
|
|
|
|
{optionLabel}
|
|
|
|
</option>
|
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
))}
|
|
|
|
</select>
|
|
|
|
</select>
|
|
|
|
|
|
|
|
{errorMessage && (
|
|
|
|
|
|
|
|
<p className="text-danger-600 mt-2 text-sm" id={errorId}>
|
|
|
|
|
|
|
|
{errorMessage}
|
|
|
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|