[ui][radio list] support required, disabled and item descriptions

pull/329/head
Yangshun Tay 3 years ago
parent 53be75b7d5
commit a828903299

@ -83,6 +83,7 @@ export function Controlled() {
return ( return (
<RadioList <RadioList
description="You will be served it during dinner"
label="Choose a fruit" label="Choose a fruit"
value={value} value={value}
onChange={(newValue: string) => setValue(newValue)}> onChange={(newValue: string) => setValue(newValue)}>
@ -93,6 +94,132 @@ export function Controlled() {
); );
} }
export function Required() {
const items = [
{
label: 'Apple',
value: 'apple',
},
{
label: 'Banana',
value: 'banana',
},
{
label: 'Orange',
value: 'orange',
},
];
const [value, setValue] = useState('apple');
return (
<RadioList
description="You will be served it during dinner"
label="Choose a fruit"
required={true}
value={value}
onChange={(newValue: string) => setValue(newValue)}>
{items.map(({ label: itemLabel, value: itemValue }) => (
<RadioList.Item key={itemLabel} label={itemLabel} value={itemValue} />
))}
</RadioList>
);
}
export function Disabled() {
const items = [
{
description: 'A red fruit',
disabled: false,
label: 'Apple',
value: 'apple',
},
{
description: 'A yellow fruit',
disabled: true,
label: 'Banana',
value: 'banana',
},
{
description: 'An orange fruit',
disabled: false,
label: 'Orange',
value: 'orange',
},
];
const [value, setValue] = useState('apple');
return (
<div className="space-y-4">
<RadioList
disabled={true}
label="Choose a fruit (all fruits disabled)"
value={value}
onChange={(newValue: string) => setValue(newValue)}>
{items.map(({ label: itemLabel, value: itemValue }) => (
<RadioList.Item key={itemLabel} label={itemLabel} value={itemValue} />
))}
</RadioList>
<HorizontalDivider />
<RadioList
defaultValue={value}
label="Choose a fruit (some fruits disabled)"
name="fruit-5">
{items.map(
({ description, label: itemLabel, value: itemValue, disabled }) => (
<RadioList.Item
key={itemLabel}
description={description}
disabled={disabled}
label={itemLabel}
value={itemValue}
/>
),
)}
</RadioList>
</div>
);
}
export function ItemDescriptions() {
const items = [
{
description: 'A red fruit',
label: 'Apple',
value: 'apple',
},
{
description: 'A yellow fruit',
label: 'Banana',
value: 'banana',
},
{
description: 'An orange fruit',
label: 'Orange',
value: 'orange',
},
];
const [value, setValue] = useState('apple');
return (
<RadioList
label="Choose a fruit"
value={value}
onChange={(newValue: string) => setValue(newValue)}>
{items.map(({ description, label: itemLabel, value: itemValue }) => (
<RadioList.Item
key={itemValue}
description={description}
label={itemLabel}
value={itemValue}
/>
))}
</RadioList>
);
}
export function Orientation() { export function Orientation() {
const items = [ const items = [
{ {

@ -1,5 +1,6 @@
import clsx from 'clsx'; import clsx from 'clsx';
import type { ChangeEvent } from 'react'; import type { ChangeEvent } from 'react';
import { useId } from 'react';
import { RadioListContext } from './RadioListContext'; import { RadioListContext } from './RadioListContext';
import RadioListItem from './RadioListItem'; import RadioListItem from './RadioListItem';
@ -10,11 +11,13 @@ type Props<T> = Readonly<{
children: ReadonlyArray<React.ReactElement<typeof RadioListItem>>; children: ReadonlyArray<React.ReactElement<typeof RadioListItem>>;
defaultValue?: T; defaultValue?: T;
description?: string; description?: string;
disabled?: boolean;
isLabelHidden?: boolean; isLabelHidden?: boolean;
label: string; label: string;
name?: string; name?: string;
onChange?: (value: T, event: ChangeEvent<HTMLInputElement>) => void; onChange?: (value: T, event: ChangeEvent<HTMLInputElement>) => void;
orientation?: RadioListOrientation; orientation?: RadioListOrientation;
required?: boolean;
value?: T; value?: T;
}>; }>;
@ -24,35 +27,47 @@ export default function RadioList<T>({
children, children,
defaultValue, defaultValue,
description, description,
disabled,
orientation = 'vertical', orientation = 'vertical',
isLabelHidden, isLabelHidden,
name, name,
label, label,
required,
value, value,
onChange, onChange,
}: Props<T>) { }: Props<T>) {
const labelId = useId();
return ( return (
<RadioListContext.Provider
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore TODO: Figure out how to type the onChange. // @ts-ignore TODO: Figure out how to type the onChange.
<RadioListContext.Provider value={{ defaultValue, name, onChange, value }}> value={{ defaultValue, disabled, name, onChange, value }}>
<div> <div>
<div className={clsx(isLabelHidden ? 'sr-only' : 'mb-4')}> <div className={clsx(isLabelHidden ? 'sr-only' : 'mb-2')}>
<label className="text-base font-medium text-gray-900">{label}</label> <label className="text-sm font-medium text-gray-900" id={labelId}>
{label}
{required && (
<span aria-hidden="true" className="text-danger-500">
{' '}
*
</span>
)}
</label>
{description && ( {description && (
<p className="text-sm leading-5 text-gray-500">{description}</p> <p className="text-xs leading-5 text-gray-500">{description}</p>
)} )}
</div> </div>
<fieldset>
<legend className="sr-only">TODO</legend>
<div <div
aria-labelledby={labelId}
aria-required={required != null ? required : undefined}
className={clsx( className={clsx(
'space-y-4', 'space-y-2',
orientation === 'horizontal' && orientation === 'horizontal' &&
'sm:flex sm:items-center sm:space-y-0 sm:space-x-10', 'sm:flex sm:items-center sm:space-y-0 sm:space-x-10',
)}> )}
role="radiogroup">
{children} {children}
</div> </div>
</fieldset>
</div> </div>
</RadioListContext.Provider> </RadioListContext.Provider>
); );

@ -3,6 +3,7 @@ import { createContext, useContext } from 'react';
type RadioListContextValue<T = unknown> = { type RadioListContextValue<T = unknown> = {
defaultValue?: T; defaultValue?: T;
disabled?: boolean;
name?: string; name?: string;
onChange?: ( onChange?: (
value: T, value: T,

@ -1,26 +1,49 @@
import clsx from 'clsx';
import { useId } from 'react'; import { useId } from 'react';
import { useRadioListContext } from './RadioListContext'; import { useRadioListContext } from './RadioListContext';
type Props<T> = Readonly<{ type Props<T> = Readonly<{
label?: string; description?: string;
disabled?: boolean;
label: string;
value: T; value: T;
}>; }>;
export default function RadioListItem<T>({ label, value }: Props<T>) { export default function RadioListItem<T>({
description,
disabled: disabledProp = false,
label,
value,
}: Props<T>) {
const id = useId(); const id = useId();
const descriptionId = useId();
const context = useRadioListContext(); const context = useRadioListContext();
const disabled = context?.disabled ?? disabledProp;
return ( return (
<div className="flex items-center"> <div
className={clsx(
'relative flex',
// Vertically center only when there's no description.
description == null && 'items-center',
)}>
<div className="flex h-5 items-center">
<input <input
checked={context?.value != null ? value === context?.value : undefined} aria-describedby={description != null ? descriptionId : undefined}
className="text-primary-600 focus:ring-primary-500 h-4 w-4 border-gray-300" checked={
context?.value != null ? value === context?.value : undefined
}
className={clsx(
'text-primary-600 focus:ring-primary-500 h-4 w-4 border-slate-300',
disabled && 'bg-slate-100',
)}
defaultChecked={ defaultChecked={
context?.defaultValue != null context?.defaultValue != null
? value === context?.defaultValue ? value === context?.defaultValue
: undefined : undefined
} }
disabled={disabled}
id={id} id={id}
name={context?.name} name={context?.name}
type="radio" type="radio"
@ -32,11 +55,24 @@ export default function RadioListItem<T>({ label, value }: Props<T>) {
: undefined : undefined
} }
/> />
</div>
<div className="ml-3 text-sm">
<label <label
className="ml-3 block text-sm font-medium text-gray-700" className={clsx(
'block font-medium',
disabled ? 'text-slate-400' : 'text-slate-700',
)}
htmlFor={id}> htmlFor={id}>
{label} {label}
</label> </label>
{description && (
<p
className={clsx(disabled ? 'text-slate-400' : 'text-slate-500')}
id={descriptionId}>
{description}
</p>
)}
</div>
</div> </div>
); );
} }

Loading…
Cancel
Save