2025-08-19 22:54:15 +03:00
|
|
|
import { cloneElement, ComponentChildren, RefObject, VNode } from "preact";
|
|
|
|
|
import { CSSProperties } from "preact/compat";
|
|
|
|
|
import { useUniqueName } from "./hooks";
|
2025-08-04 20:34:47 +03:00
|
|
|
|
|
|
|
|
interface FormGroupProps {
|
2025-08-19 22:54:15 +03:00
|
|
|
name: string;
|
2025-08-05 19:06:47 +03:00
|
|
|
labelRef?: RefObject<HTMLLabelElement>;
|
2025-08-06 18:38:52 +03:00
|
|
|
label?: string;
|
2025-08-04 21:17:35 +03:00
|
|
|
title?: string;
|
|
|
|
|
className?: string;
|
2025-08-25 17:17:56 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2025-08-19 22:54:15 +03:00
|
|
|
children: VNode<any>;
|
2025-08-07 19:20:35 +03:00
|
|
|
description?: string | ComponentChildren;
|
2025-08-14 22:27:07 +03:00
|
|
|
disabled?: boolean;
|
2025-08-19 22:54:15 +03:00
|
|
|
style?: CSSProperties;
|
2025-08-04 20:34:47 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-19 22:54:15 +03:00
|
|
|
export default function FormGroup({ name, label, title, className, children, description, labelRef, disabled, style }: FormGroupProps) {
|
|
|
|
|
const id = useUniqueName(name);
|
|
|
|
|
const childWithId = cloneElement(children, { id });
|
|
|
|
|
|
2025-08-04 20:34:47 +03:00
|
|
|
return (
|
2025-08-19 22:54:15 +03:00
|
|
|
<div className={`form-group ${className} ${disabled ? "disabled" : ""}`} title={title} style={style}>
|
|
|
|
|
{ label &&
|
|
|
|
|
<label style={{ width: "100%" }} ref={labelRef} htmlFor={id}>{label}</label>}
|
|
|
|
|
|
|
|
|
|
{childWithId}
|
2025-08-04 21:17:35 +03:00
|
|
|
|
2025-08-24 20:57:23 +03:00
|
|
|
{description && <div><small className="form-text">{description}</small></div>}
|
2025-08-04 20:34:47 +03:00
|
|
|
</div>
|
|
|
|
|
);
|
2025-08-19 23:34:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Similar to {@link FormGroup} but allows more than one child. Due to this behaviour, there is no automatic ID assignment.
|
|
|
|
|
*/
|
|
|
|
|
export function FormMultiGroup({ label, children }: { label: string, children: ComponentChildren }) {
|
|
|
|
|
return (
|
|
|
|
|
<div className={`form-group`}>
|
|
|
|
|
{label && <label>{label}</label>}
|
|
|
|
|
{children}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2025-08-04 20:34:47 +03:00
|
|
|
}
|