mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2025-07-14 18:05:55 +08:00
[ui][select] support required
This commit is contained in:
@ -177,6 +177,35 @@ export function Disabled() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function Required() {
|
||||||
|
const [value, setValue] = useState('apple');
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-x-4">
|
||||||
|
<Select
|
||||||
|
label="Select a fruit"
|
||||||
|
options={[
|
||||||
|
{
|
||||||
|
label: 'Apple',
|
||||||
|
value: 'apple',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Banana',
|
||||||
|
value: 'banana',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Orange',
|
||||||
|
value: 'orange',
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
required={true}
|
||||||
|
value={value}
|
||||||
|
onChange={setValue}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function Uncontrolled() {
|
export function Uncontrolled() {
|
||||||
return (
|
return (
|
||||||
<div className="space-x-4">
|
<div className="space-x-4">
|
||||||
|
@ -43,6 +43,7 @@ function Select<T>(
|
|||||||
label,
|
label,
|
||||||
isLabelHidden,
|
isLabelHidden,
|
||||||
options,
|
options,
|
||||||
|
required,
|
||||||
value,
|
value,
|
||||||
onChange,
|
onChange,
|
||||||
...props
|
...props
|
||||||
@ -58,6 +59,12 @@ function Select<T>(
|
|||||||
className={clsx('mb-1 block text-sm font-medium text-slate-700')}
|
className={clsx('mb-1 block text-sm font-medium text-slate-700')}
|
||||||
htmlFor={id ?? undefined}>
|
htmlFor={id ?? undefined}>
|
||||||
{label}
|
{label}
|
||||||
|
{required && (
|
||||||
|
<span aria-hidden="true" className="text-danger-500">
|
||||||
|
{' '}
|
||||||
|
*
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
</label>
|
</label>
|
||||||
)}
|
)}
|
||||||
<select
|
<select
|
||||||
@ -72,6 +79,7 @@ function Select<T>(
|
|||||||
defaultValue={defaultValue != null ? String(defaultValue) : undefined}
|
defaultValue={defaultValue != null ? String(defaultValue) : undefined}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
id={id}
|
id={id}
|
||||||
|
required={required}
|
||||||
value={value != null ? String(value) : undefined}
|
value={value != null ? String(value) : undefined}
|
||||||
onChange={(event) => {
|
onChange={(event) => {
|
||||||
onChange?.(event.target.value);
|
onChange?.(event.target.value);
|
||||||
|
Reference in New Issue
Block a user