Files
Trilium/apps/client/src/widgets/react/FormRadioGroup.tsx

47 lines
1.5 KiB
TypeScript
Raw Normal View History

import type { ComponentChildren } from "preact";
import { useUniqueName } from "./hooks";
2025-08-03 21:18:18 +03:00
interface FormRadioProps {
name: string;
currentValue?: string;
values: {
value: string;
label: string | ComponentChildren;
2025-08-03 21:18:18 +03:00
}[];
onChange(newValue: string): void;
}
export default function FormRadioGroup({ values, ...restProps }: FormRadioProps) {
2025-08-03 21:18:18 +03:00
return (
<>
{(values || []).map(({ value, label }) => (
2025-08-14 17:54:52 +03:00
<div className="form-checkbox">
<FormRadio value={value} label={label} {...restProps} labelClassName="form-check-label" />
2025-08-03 21:18:18 +03:00
</div>
))}
</>
);
}
export function FormInlineRadioGroup({ values, ...restProps }: FormRadioProps) {
return (
<>
{values.map(({ value, label }) => (<FormRadio value={value} label={label} {...restProps} />))}
</>
)
}
function FormRadio({ name, value, label, currentValue, onChange, labelClassName }: Omit<FormRadioProps, "values"> & { value: string, label: ComponentChildren, labelClassName?: string }) {
return (
<label className={`tn-radio ${labelClassName ?? ""}`}>
<input
className="form-check-input"
type="radio"
name={useUniqueName(name)}
value={value}
checked={value === currentValue}
onChange={e => onChange((e.target as HTMLInputElement).value)} />
{label}
</label>
)
2025-08-03 21:18:18 +03:00
}