Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: bind FormField label to explicit child id so association always holds

Adam Eivy4b05ac9466b2
diff --git a/client/src/components/ui/FormField.jsx b/client/src/components/ui/FormField.jsx
index 37afa012de..fdd01b3ccf 100644
--- a/client/src/components/ui/FormField.jsx
+++ b/client/src/components/ui/FormField.jsx
@@ -10,12 +10,13 @@ import { useId, Children, cloneElement, isValidElement } from 'react';
*
* Styling is caller-owned so migrating an existing field is a lossless swap:
* pass `className` for the wrapper and `labelClassName` to match the field's
- * current label classes. The `id` is injected only when the first child has no
- * `id` of its own, so an explicit id still wins.
+ * current label classes. If the first child already has an `id`, that id wins
+ * and the label points at it; otherwise the generated id is injected — either
+ * way the label/control association holds.
*
* @param {string} label - Visible label text.
* @param {import('react').ReactNode} [hint] - Optional hint rendered between label and field.
- * @param {import('react').ReactNode} children - The field control(s); the first element receives the generated id.
+ * @param {import('react').ReactNode} children - The field control(s); the first element is bound to the label.
* @param {string} [className] - Wrapper div className.
* @param {string} [labelClassName] - Label className (defaults to the common config-form label style).
*/
@@ -26,15 +27,21 @@ export function FormField({
className = '',
labelClassName = 'block text-sm text-gray-400 mb-1',
}) {
- const id = useId();
- const augmented = Children.map(children, (child, i) =>
- i === 0 && isValidElement(child) && !child.props.id
- ? cloneElement(child, { id })
- : child
- );
+ const generatedId = useId();
+ // The label must point at whatever id the first control actually has: reuse
+ // the child's own id when present, otherwise inject the generated one.
+ let controlId = generatedId;
+ const augmented = Children.map(children, (child, i) => {
+ if (i !== 0 || !isValidElement(child)) return child;
+ if (child.props.id) {
+ controlId = child.props.id;
+ return child;
+ }
+ return cloneElement(child, { id: controlId });
+ });
return (
<div className={className}>
- {label != null && <label htmlFor={id} className={labelClassName}>{label}</label>}
+ {label != null && <label htmlFor={controlId} className={labelClassName}>{label}</label>}
{hint != null && <p className="text-xs text-gray-500 mb-1">{hint}</p>}
{augmented}
</div>
diff --git a/client/src/components/ui/FormField.test.jsx b/client/src/components/ui/FormField.test.jsx
index 966ea5ceda..99259cc138 100644
--- a/client/src/components/ui/FormField.test.jsx
+++ b/client/src/components/ui/FormField.test.jsx
@@ -52,15 +52,16 @@ describe('FormField', () => {
expect(screen.getByText('One per line').id).toBe('');
});
- it('does not override an explicit id on the child', () => {
+ it('binds the label to an explicit child id (association still holds)', () => {
render(
<FormField label="Custom">
<input id="my-fixed-id" defaultValue="" />
</FormField>
);
- // With a fixed child id but a generated label htmlFor, the association
- // is not made — but the caller-provided id must be preserved.
- expect(document.getElementById('my-fixed-id')).toBeTruthy();
+ // The caller-provided id is preserved AND the label points at it, so the
+ // association is made without clobbering the explicit id.
+ const input = screen.getByLabelText('Custom');
+ expect(input.id).toBe('my-fixed-id');
});
it('applies caller-provided wrapper and label classes', () => {