Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(form): helper text leaves aria-describedby when it unmounts

Raule131b070a09d
diff --git a/packages/react/src/Form/FormControl.tsx b/packages/react/src/Form/FormControl.tsx
index 83b0a245d0..d8010d5a2b 100644
--- a/packages/react/src/Form/FormControl.tsx
+++ b/packages/react/src/Form/FormControl.tsx
@@ -8,6 +8,7 @@ import {
forwardRef,
useCallback,
useContext,
+ useEffect,
useId,
useState
} from "react";
@@ -115,10 +116,15 @@ function useFormControlProvider(props: FormControlContextType) {
const [hasFeedbackText, setHasFeedbackText] = useState(false);
/**
- * Track whether the `FormHelperText` has been rendered.
- * We use this to append its id the `aria-describedby` of the `input`.
+ * How many `FormHelperText`s are on screen. Each registers when it mounts and
+ * unregisters when it unmounts, so helper text that gives way to the error
+ * message also leaves the control's `aria-describedby`.
*/
- const [hasHelpText, setHasHelpText] = useState(false);
+ const [helpTextCount, setHelpTextCount] = useState(0);
+ const registerHelpText = useCallback(() => {
+ setHelpTextCount((count) => count + 1);
+ return () => setHelpTextCount((count) => count - 1);
+ }, []);
/**
* The id of the rendered `FormLabel`, once it is on screen. Controls that
@@ -137,14 +143,7 @@ function useFormControlProvider(props: FormControlContextType) {
) => ({
id: helpTextId,
...props,
- /**
- * Notify the field context when the help text is rendered on screen,
- * so we can apply the correct `aria-describedby` to the field (e.g. input, textarea).
- */
- ref: mergeRefs(forwardedRef, (node) => {
- if (!node) return;
- setHasHelpText(true);
- })
+ ref: forwardedRef
}),
[helpTextId]
);
@@ -219,8 +218,8 @@ function useFormControlProvider(props: FormControlContextType) {
onBlur: () => setFocus(false),
hasFeedbackText,
setHasFeedbackText,
- hasHelpText,
- setHasHelpText,
+ hasHelpText: helpTextCount > 0,
+ registerHelpText,
renderedLabelId,
id,
labelId,
@@ -283,6 +282,8 @@ export interface FormHelperTextProps extends ComponentProps<"div"> {}
export const FormHelperText = forwardRef<HTMLDivElement, FormHelperTextProps>(
function FormHelperText(props, ref) {
const field = useFormControlContext();
+ const { registerHelpText } = field;
+ useEffect(() => registerHelpText(), [registerHelpText]);
return (
<div
diff --git a/packages/react/src/__tests__/FormControl.test.tsx b/packages/react/src/__tests__/FormControl.test.tsx
index 73e328e4fb..62349213ae 100644
--- a/packages/react/src/__tests__/FormControl.test.tsx
+++ b/packages/react/src/__tests__/FormControl.test.tsx
@@ -10,7 +10,7 @@ import type { Root } from "react-dom/client";
import { createRoot } from "react-dom/client";
import { afterEach, beforeAll, describe, expect, it } from "vitest";
import { CommandTrigger } from "../Command";
-import { FormControl } from "../Form/FormControl";
+import { FormControl, FormHelperText } from "../Form/FormControl";
import { FormErrorMessage } from "../Form/FormError";
import { FormLabel } from "../Form/FormLabel";
import { Input, InputGroup } from "../Input";
@@ -143,6 +143,29 @@ describe("controls take the FormControl's state", () => {
expect(input.className).toContain("border-destructive");
});
+ it("helper text leaves the description when the error replaces it", () => {
+ const field = (invalid: boolean) => (
+ <FormControl isInvalid={invalid}>
+ <Input id="code" />
+ {invalid ? (
+ <FormErrorMessage>Code is required</FormErrorMessage>
+ ) : (
+ <FormHelperText>Up to 10 characters</FormHelperText>
+ )}
+ </FormControl>
+ );
+ const view = render(field(false));
+ const input = view.querySelector("input")!;
+ const helper = [...view.querySelectorAll("div[id]")].find(
+ (el) => el.textContent === "Up to 10 characters"
+ )!;
+ expect(input.getAttribute("aria-describedby")).toBe(helper.id);
+
+ act(() => root!.render(field(true)));
+ const error = view.querySelector('[aria-live="polite"]')!;
+ expect(input.getAttribute("aria-describedby")).toBe(error.id);
+ });
+
it("a valid field leaves its input alone", () => {
const view = render(
<FormControl>