From f17d78a99af59b30b140982ae2fc84fa00349c9a Mon Sep 17 00:00:00 2001 From: Mark Fitzgerald Date: Tue, 19 Nov 2024 15:54:23 -0800 Subject: [PATCH 1/3] Add 'aria-describedby' content for input field to reference. --- .../src/components/input-with-examples.tsx | 17 +++++++++++++++-- packages/perseus/src/components/text-input.tsx | 2 ++ packages/perseus/src/components/tooltip.tsx | 1 + 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/perseus/src/components/input-with-examples.tsx b/packages/perseus/src/components/input-with-examples.tsx index da253dfec3..461dfd4aa5 100644 --- a/packages/perseus/src/components/input-with-examples.tsx +++ b/packages/perseus/src/components/input-with-examples.tsx @@ -83,8 +83,14 @@ class InputWithExamples extends React.Component { _renderInput: () => any = () => { const id = this._getUniqueId(); + const examplesAria = `${this.props.examples[0]} + ${this.props.examples.slice(1).join(", or\n")}` + // @ts-expect-error TS2550: Property replaceAll does not exist on type string. + .replaceAll("*", "") + .replaceAll("$", "") + .replaceAll("\\ ", " ") + .replaceAll("\\text{pi}", "pi"); const inputProps = { - id: id, "aria-describedby": id, ref: "input", className: this._getInputClassName(), @@ -101,7 +107,14 @@ class InputWithExamples extends React.Component { autoCorrect: "off", spellCheck: "false", }; - return ; + return ( + <> + + + {examplesAria} + + + ); }; _handleFocus: () => void = () => { diff --git a/packages/perseus/src/components/text-input.tsx b/packages/perseus/src/components/text-input.tsx index abcb259260..4119f260a2 100644 --- a/packages/perseus/src/components/text-input.tsx +++ b/packages/perseus/src/components/text-input.tsx @@ -17,6 +17,7 @@ type Props = { placeholder?: string; onKeyDown?: () => void; style?: StyleType; + "aria-describedby"?: string; }; type DefaultProps = { @@ -110,6 +111,7 @@ class TextInput extends React.Component { value={formattedValue} type="text" aria-label={labelText} + aria-describedby={this.props["aria-describedby"]} onChange={(value) => this.props.onChange(value)} placeholder={placeholder} onFocus={onFocus} diff --git a/packages/perseus/src/components/tooltip.tsx b/packages/perseus/src/components/tooltip.tsx index d5a13e13ea..4f3479cc5d 100644 --- a/packages/perseus/src/components/tooltip.tsx +++ b/packages/perseus/src/components/tooltip.tsx @@ -340,6 +340,7 @@ class Tooltip extends React.Component { // eslint-disable-next-line react/no-string-refs ref="tooltipContainer" className="tooltipContainer" + role="tooltip" style={{ position: "absolute", // height must start out undefined, not null, so that From d08b48dd1f22c2856b662668de73f06fc8613351 Mon Sep 17 00:00:00 2001 From: Mark Fitzgerald Date: Tue, 19 Nov 2024 17:29:51 -0800 Subject: [PATCH 2/3] Add unit tests for ensuring that aria text is included in the DOM. Update snapshots --- .changeset/odd-moons-remember.md | 5 +++ .../server-item-renderer.test.tsx.snap | 6 +++ .../src/components/input-with-examples.tsx | 24 ++++++---- .../multi-renderer.test.tsx.snap | 6 +++ .../graded-group-set-jipt.test.ts.snap | 18 ++++++++ .../graded-group-set.test.ts.snap | 6 +++ .../group/__snapshots__/group.test.tsx.snap | 12 +++++ .../__snapshots__/numeric-input.test.ts.snap | 12 +++++ .../numeric-input/numeric-input.test.ts | 45 +++++++++++++++++++ 9 files changed, 125 insertions(+), 9 deletions(-) create mode 100644 .changeset/odd-moons-remember.md diff --git a/.changeset/odd-moons-remember.md b/.changeset/odd-moons-remember.md new file mode 100644 index 0000000000..3ff9692b33 --- /dev/null +++ b/.changeset/odd-moons-remember.md @@ -0,0 +1,5 @@ +--- +"@khanacademy/perseus": patch +--- + +[Numeric Input] - Associate format options tooltip content with input field for assistive technologies diff --git a/packages/perseus/src/__tests__/__snapshots__/server-item-renderer.test.tsx.snap b/packages/perseus/src/__tests__/__snapshots__/server-item-renderer.test.tsx.snap index da69cf654c..f04b8a590f 100644 --- a/packages/perseus/src/__tests__/__snapshots__/server-item-renderer.test.tsx.snap +++ b/packages/perseus/src/__tests__/__snapshots__/server-item-renderer.test.tsx.snap @@ -41,6 +41,7 @@ exports[`server item renderer should snapshot: initial render 1`] = `
+