[ui][radio list] remove disabled prop on radio list level

pull/333/head
Yangshun Tay 2 years ago
parent a818e7d820
commit 21e5e0672a

@ -29,7 +29,11 @@ export default {
export function Basic({ export function Basic({
description, description,
label, label,
}: Pick<React.ComponentProps<typeof RadioList>, 'description' | 'label'>) { orientation,
}: Pick<
React.ComponentProps<typeof RadioList>,
'description' | 'label' | 'orientation'
>) {
const items = [ const items = [
{ {
label: 'Apple', label: 'Apple',
@ -50,7 +54,8 @@ export function Basic({
defaultValue="apple" defaultValue="apple"
description={description} description={description}
label={label} label={label}
name="fruit"> name="fruit"
orientation={orientation}>
{items.map(({ label: itemLabel, value }) => ( {items.map(({ label: itemLabel, value }) => (
<RadioList.Item key={itemLabel} label={itemLabel} value={value} /> <RadioList.Item key={itemLabel} label={itemLabel} value={value} />
))} ))}
@ -61,6 +66,7 @@ export function Basic({
Basic.args = { Basic.args = {
description: 'Your favorite fruit', description: 'Your favorite fruit',
label: 'Choose a fruit', label: 'Choose a fruit',
orientation: 'vertical',
}; };
export function Controlled() { export function Controlled() {
@ -148,22 +154,10 @@ export function Disabled() {
}, },
]; ];
const [value, setValue] = useState('apple');
return ( return (
<div className="space-y-4"> <div className="space-y-4">
<RadioList <RadioList
disabled={true} defaultValue="apple"
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)" label="Choose a fruit (some fruits disabled)"
name="fruit-5"> name="fruit-5">
{items.map( {items.map(

@ -11,7 +11,6 @@ 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;
@ -27,10 +26,9 @@ export default function RadioList<T>({
children, children,
defaultValue, defaultValue,
description, description,
disabled,
orientation = 'vertical',
isLabelHidden, isLabelHidden,
name, name,
orientation = 'vertical',
label, label,
required, required,
value, value,
@ -41,7 +39,7 @@ export default function RadioList<T>({
<RadioListContext.Provider <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.
value={{ defaultValue, disabled, name, onChange, value }}> value={{ defaultValue, name, onChange, value }}>
<div> <div>
<div className={clsx(isLabelHidden ? 'sr-only' : 'mb-2')}> <div className={clsx(isLabelHidden ? 'sr-only' : 'mb-2')}>
<label className="text-sm font-medium text-gray-900" id={labelId}> <label className="text-sm font-medium text-gray-900" id={labelId}>

@ -3,7 +3,6 @@ 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,

@ -12,14 +12,13 @@ type Props<T> = Readonly<{
export default function RadioListItem<T>({ export default function RadioListItem<T>({
description, description,
disabled: disabledProp = false, disabled = false,
label, label,
value, value,
}: Props<T>) { }: Props<T>) {
const id = useId(); const id = useId();
const descriptionId = useId(); const descriptionId = useId();
const context = useRadioListContext(); const context = useRadioListContext();
const disabled = context?.disabled ?? disabledProp;
return ( return (
<div <div
@ -67,7 +66,10 @@ export default function RadioListItem<T>({
</label> </label>
{description && ( {description && (
<p <p
className={clsx(disabled ? 'text-slate-400' : 'text-slate-500')} className={clsx(
'text-xs',
disabled ? 'text-slate-400' : 'text-slate-500',
)}
id={descriptionId}> id={descriptionId}>
{description} {description}
</p> </p>

Loading…
Cancel
Save