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-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-05 22:11:16 +03:00
|
|
|
{description && <small className="form-text">{description}</small>}
|
2025-08-04 20:34:47 +03:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|