2025-08-14 17:36:11 +03:00
|
|
|
import type { ComponentChildren } from "preact";
|
2025-08-14 17:53:24 +03:00
|
|
|
import { useUniqueName } from "./hooks";
|
2025-08-14 17:36:11 +03:00
|
|
|
|
2025-08-03 21:18:18 +03:00
|
|
|
interface FormRadioProps {
|
|
|
|
|
name: string;
|
|
|
|
|
currentValue?: string;
|
|
|
|
|
values: {
|
|
|
|
|
value: string;
|
2025-08-14 17:36:11 +03:00
|
|
|
label: string | ComponentChildren;
|
2025-08-03 21:18:18 +03:00
|
|
|
}[];
|
|
|
|
|
onChange(newValue: string): void;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-15 00:19:37 +03:00
|
|
|
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">
|
2025-08-15 00:19:37 +03:00
|
|
|
<FormRadio value={value} label={label} {...restProps} labelClassName="form-check-label" />
|
2025-08-03 21:18:18 +03:00
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</>
|
|
|
|
|
);
|
2025-08-15 00:19:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|