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): only an invalid control is described by the error message

Raulb682fc65f8db
diff --git a/packages/react/src/Form/useFormControl.tsx b/packages/react/src/Form/useFormControl.tsx
index a02e030a92..0da38e808b 100644
--- a/packages/react/src/Form/useFormControl.tsx
+++ b/packages/react/src/Form/useFormControl.tsx
@@ -28,8 +28,9 @@ export function useFormControl(props: UseFormControlProps) {
const describedBy = props["aria-describedby"]
? [props["aria-describedby"]]
: [];
- // The error message is described first.
- if (field?.isInvalid && field.hasFeedbackText) {
+ // The error message is described first, and only while the control shows
+ // as invalid: a caller's own isInvalid={false} wins over the FormControl.
+ if (isInvalid && field?.hasFeedbackText) {
describedBy.push(field.feedbackId);
}
if (field?.hasHelpText) {
diff --git a/packages/react/src/__tests__/FormControl.test.tsx b/packages/react/src/__tests__/FormControl.test.tsx
index 62349213ae..b5992ec097 100644
--- a/packages/react/src/__tests__/FormControl.test.tsx
+++ b/packages/react/src/__tests__/FormControl.test.tsx
@@ -166,6 +166,18 @@ describe("controls take the FormControl's state", () => {
expect(input.getAttribute("aria-describedby")).toBe(error.id);
});
+ it("a control the caller marks valid is not described by the error", () => {
+ const view = render(
+ <FormControl isInvalid>
+ <Input id="code" isInvalid={false} />
+ <FormErrorMessage>Code is required</FormErrorMessage>
+ </FormControl>
+ );
+ const input = view.querySelector("input")!;
+ expect(input.hasAttribute("aria-invalid")).toBe(false);
+ expect(input.hasAttribute("aria-describedby")).toBe(false);
+ });
+
it("a valid field leaves its input alone", () => {
const view = render(
<FormControl>