parent
53be75b7d5
commit
a828903299
@ -1,42 +1,78 @@
|
|||||||
|
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
|
||||||
<input
|
className={clsx(
|
||||||
checked={context?.value != null ? value === context?.value : undefined}
|
'relative flex',
|
||||||
className="text-primary-600 focus:ring-primary-500 h-4 w-4 border-gray-300"
|
// Vertically center only when there's no description.
|
||||||
defaultChecked={
|
description == null && 'items-center',
|
||||||
context?.defaultValue != null
|
)}>
|
||||||
? value === context?.defaultValue
|
<div className="flex h-5 items-center">
|
||||||
: undefined
|
<input
|
||||||
}
|
aria-describedby={description != null ? descriptionId : undefined}
|
||||||
id={id}
|
checked={
|
||||||
name={context?.name}
|
context?.value != null ? value === context?.value : undefined
|
||||||
type="radio"
|
}
|
||||||
onChange={
|
className={clsx(
|
||||||
context?.onChange != null
|
'text-primary-600 focus:ring-primary-500 h-4 w-4 border-slate-300',
|
||||||
? (event) => {
|
disabled && 'bg-slate-100',
|
||||||
context?.onChange?.(value, event);
|
)}
|
||||||
}
|
defaultChecked={
|
||||||
: undefined
|
context?.defaultValue != null
|
||||||
}
|
? value === context?.defaultValue
|
||||||
/>
|
: undefined
|
||||||
<label
|
}
|
||||||
className="ml-3 block text-sm font-medium text-gray-700"
|
disabled={disabled}
|
||||||
htmlFor={id}>
|
id={id}
|
||||||
{label}
|
name={context?.name}
|
||||||
</label>
|
type="radio"
|
||||||
|
onChange={
|
||||||
|
context?.onChange != null
|
||||||
|
? (event) => {
|
||||||
|
context?.onChange?.(value, event);
|
||||||
|
}
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="ml-3 text-sm">
|
||||||
|
<label
|
||||||
|
className={clsx(
|
||||||
|
'block font-medium',
|
||||||
|
disabled ? 'text-slate-400' : 'text-slate-700',
|
||||||
|
)}
|
||||||
|
htmlFor={id}>
|
||||||
|
{label}
|
||||||
|
</label>
|
||||||
|
{description && (
|
||||||
|
<p
|
||||||
|
className={clsx(disabled ? 'text-slate-400' : 'text-slate-500')}
|
||||||
|
id={descriptionId}>
|
||||||
|
{description}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue