mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2025-07-27 20:22:33 +08:00
[portal][ui] add error message to checkbox input
This commit is contained in:
@ -7,6 +7,7 @@ type Props = Readonly<{
|
|||||||
defaultValue?: boolean;
|
defaultValue?: boolean;
|
||||||
description?: string;
|
description?: string;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
|
errorMessage?: string;
|
||||||
label: string;
|
label: string;
|
||||||
name?: string;
|
name?: string;
|
||||||
onChange?: (
|
onChange?: (
|
||||||
@ -21,6 +22,7 @@ function CheckboxInput(
|
|||||||
defaultValue,
|
defaultValue,
|
||||||
description,
|
description,
|
||||||
disabled = false,
|
disabled = false,
|
||||||
|
errorMessage,
|
||||||
label,
|
label,
|
||||||
name,
|
name,
|
||||||
value,
|
value,
|
||||||
@ -30,59 +32,67 @@ function CheckboxInput(
|
|||||||
) {
|
) {
|
||||||
const id = useId();
|
const id = useId();
|
||||||
const descriptionId = useId();
|
const descriptionId = useId();
|
||||||
|
const errorId = useId();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div>
|
||||||
className={clsx(
|
<div
|
||||||
'relative flex',
|
className={clsx(
|
||||||
// Vertically center only when there's no description.
|
'relative flex',
|
||||||
description == null && 'items-center',
|
// Vertically center only when there's no description.
|
||||||
)}>
|
description == null && 'items-center',
|
||||||
<div className="flex h-5 items-center">
|
)}>
|
||||||
<input
|
<div className="flex h-5 items-center">
|
||||||
ref={ref}
|
<input
|
||||||
aria-describedby={description != null ? descriptionId : undefined}
|
ref={ref}
|
||||||
checked={value}
|
aria-describedby={description != null ? descriptionId : undefined}
|
||||||
className={clsx(
|
checked={value}
|
||||||
'h-4 w-4 rounded border-slate-300',
|
|
||||||
disabled
|
|
||||||
? 'bg-slate-100 text-slate-400'
|
|
||||||
: 'text-primary-600 focus:ring-primary-500',
|
|
||||||
)}
|
|
||||||
defaultChecked={defaultValue}
|
|
||||||
disabled={disabled}
|
|
||||||
id={id}
|
|
||||||
name={name}
|
|
||||||
type="checkbox"
|
|
||||||
onChange={
|
|
||||||
onChange != null
|
|
||||||
? (event) => {
|
|
||||||
onChange?.(event.target.checked, 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(
|
className={clsx(
|
||||||
'text-xs',
|
'h-4 w-4 rounded border-slate-300',
|
||||||
disabled ? 'text-slate-400' : 'text-slate-500',
|
disabled
|
||||||
|
? 'bg-slate-100 text-slate-400'
|
||||||
|
: 'text-primary-600 focus:ring-primary-500',
|
||||||
)}
|
)}
|
||||||
id={descriptionId}>
|
defaultChecked={defaultValue}
|
||||||
{description}
|
disabled={disabled}
|
||||||
</p>
|
id={id}
|
||||||
)}
|
name={name}
|
||||||
|
type="checkbox"
|
||||||
|
onChange={(event) => {
|
||||||
|
if (!onChange) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
onChange(event.target.checked, event);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</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(
|
||||||
|
'text-xs',
|
||||||
|
disabled ? 'text-slate-400' : 'text-slate-500',
|
||||||
|
)}
|
||||||
|
id={descriptionId}>
|
||||||
|
{description}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
{errorMessage && (
|
||||||
|
<p className="text-danger-600 mt-2 text-sm" id={errorId}>
|
||||||
|
{errorMessage}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user