Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(forms): add message id to input aria-describedby attribute #1389

Merged
merged 5 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/forms/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"index.cjs.js": {
"bundled": 156277,
"minified": 107076,
"gzipped": 18758
"bundled": 157007,
"minified": 107387,
"gzipped": 18832
},
"index.esm.js": {
"bundled": 147057,
"minified": 98835,
"gzipped": 18335,
"bundled": 147775,
"minified": 99134,
"gzipped": 18406,
"treeshaked": {
"rollup": {
"code": 80737,
"code": 81008,
"import_statements": 745
},
"webpack": {
"code": 88465
"code": 88771
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/forms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"sideEffects": false,
"types": "dist/typings/index.d.ts",
"dependencies": {
"@zendeskgarden/container-field": "^1.3.6",
"@zendeskgarden/container-field": "^2.1.0",
"@zendeskgarden/container-utilities": "^0.7.0",
"lodash.debounce": "^4.0.8",
"polished": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/forms/src/elements/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const Input = React.forwardRef<HTMLInputElement, IInputProps>(
}

if (fieldContext) {
combinedProps = fieldContext.getInputProps(combinedProps, { isDescribed: true });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that the isDescribed option is true on multiple input components including Input. I found it was safe to remove after getInputProps was given a default value for the second function parameter. Are there any concerns with removing?

combinedProps = fieldContext.getInputProps(combinedProps);
}

return <StyledTextInput {...(combinedProps as any)} />;
Expand Down
22 changes: 21 additions & 1 deletion packages/forms/src/elements/common/Field.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import React from 'react';
import { render, act } from 'garden-test-utils';
import { Field, Label, Input, Hint } from '../..';
import { Field, Label, Input, Hint, Message } from '../..';

describe('Field', () => {
it('passes ref to underlying DOM element', () => {
Expand Down Expand Up @@ -69,4 +69,24 @@ describe('Field', () => {

expect(getByTestId('input')).not.toHaveAttribute('aria-describedby', expect.any(String));
});

it('renders correct aria attributes when Message component is present', () => {
const ExampleField = ({ showMessage = false }) => (
<Field>
<Label>Label</Label>
{showMessage ? <Message>Message</Message> : null}
<Input data-test-id="input" />
</Field>
);

const { getByTestId, rerender } = render(<ExampleField showMessage />);

expect(getByTestId('input')).toHaveAttribute('aria-describedby', expect.any(String));

act(() => {
rerender(<ExampleField showMessage={false} />);
});

expect(getByTestId('input')).not.toHaveAttribute('aria-describedby', expect.any(String));
});
});
28 changes: 19 additions & 9 deletions packages/forms/src/elements/common/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,36 @@ export interface IFieldProps extends HTMLAttributes<HTMLDivElement> {
export const Field = React.forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(
(props, ref) => {
const [hasHint, setHasHint] = useState(false);
const [hasMessage, setHasMessage] = useState(false);
const [isLabelActive, setIsLabelActive] = useState(false);
const [isLabelHovered, setIsLabelHovered] = useState(false);
const multiThumbRangeRef = useRef<HTMLDivElement>(null);
const getMessageProps = (messageProps: any) => ({ role: 'alert', ...messageProps });
const { getInputProps, ...propGetters } = useField(props.id);
const { getInputProps, getMessageProps, ...propGetters } = useField(props.id);
const fieldProps = useMemo(
() => ({
...propGetters,
getMessageProps,
getInputProps: (options: any, describeOptions: any = {}) =>
getInputProps(options, { ...describeOptions, isDescribed: hasHint, hasMessage }),
getMessageProps: (options: any) => getMessageProps({ role: 'alert', ...options }),
isLabelActive,
setIsLabelActive,
isLabelHovered,
setIsLabelHovered,
multiThumbRangeRef,
getInputProps: (options: any, describeOptions: any) =>
getInputProps(options, { ...describeOptions, isDescribed: hasHint }),
setHint: (hintPresent: boolean) => setHasHint(hintPresent),
hasHint
hasHint,
setHasHint,
Copy link
Contributor Author

@Francois-Esquire Francois-Esquire Jul 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setHint was renamed to setHasHint for consistency and clarity in its function, the new setHasMessage also follows this naming pattern.

hasMessage,
setHasMessage,
multiThumbRangeRef
}),
[propGetters, isLabelActive, isLabelHovered, hasHint, getInputProps]
[
propGetters,
getInputProps,
getMessageProps,
isLabelActive,
isLabelHovered,
hasHint,
hasMessage
]
);

return (
Expand Down
16 changes: 8 additions & 8 deletions packages/forms/src/elements/common/Hint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ import { StyledHint, StyledCheckHint, StyledRadioHint, StyledToggleHint } from '
*/
export const Hint = React.forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(
(props, ref) => {
const fieldContext = useFieldContext();
const { hasHint, setHasHint, getHintProps } = useFieldContext() || {};
const type = useInputContext();

useEffect(() => {
if (fieldContext && !fieldContext.hasHint) {
fieldContext.setHint(true);
if (!hasHint) {
setHasHint!(true);
}

return () => {
if (fieldContext && fieldContext.hasHint) {
fieldContext.setHint(false);
if (hasHint) {
setHasHint!(false);
}
};
}, [fieldContext]);
}, [hasHint, setHasHint]);

let HintComponent;

Expand All @@ -44,8 +44,8 @@ export const Hint = React.forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivEleme

let combinedProps = props;

if (fieldContext) {
combinedProps = fieldContext.getHintProps(combinedProps);
if (typeof getHintProps === 'function') {
combinedProps = getHintProps!(combinedProps);
}

return <HintComponent ref={ref} {...(combinedProps as any)} />;
Expand Down
20 changes: 16 additions & 4 deletions packages/forms/src/elements/common/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/

import React from 'react';
import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import { useText } from '@zendeskgarden/react-theming';

Expand All @@ -25,9 +25,21 @@ import {
*/
export const Message = React.forwardRef<HTMLDivElement, IMessageProps>(
({ validation, validationLabel, children, ...props }, ref) => {
const fieldContext = useFieldContext();
const { hasMessage, setHasMessage, getMessageProps } = useFieldContext() || {};
const type = useInputContext();

useEffect(() => {
if (!hasMessage) {
setHasMessage!(true);
}

return () => {
if (hasMessage) {
setHasMessage!(false);
}
};
}, [hasMessage, setHasMessage]);

let MessageComponent;

if (type === 'checkbox') {
Expand All @@ -42,8 +54,8 @@ export const Message = React.forwardRef<HTMLDivElement, IMessageProps>(

let combinedProps = { validation, validationLabel, ...props };

if (fieldContext) {
combinedProps = fieldContext.getMessageProps(combinedProps);
if (typeof getMessageProps === 'function') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems indicative of poor typing within the context, but might not be worth addressing here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've cleaned this up and removed the typeof check, I was mistakenly thinking the Field context could be misused (however it is not publicly exported).

combinedProps = getMessageProps!(combinedProps);
}

const ariaLabel = useText(Message, combinedProps, 'validationLabel', validation as string);
Expand Down
4 changes: 3 additions & 1 deletion packages/forms/src/utils/useFieldContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ interface IFieldContext extends IUseFieldPropGetters {
setIsLabelHovered: (isLabelHovered: boolean) => void;
setIsLabelActive: (isLabelActive: boolean) => void;
multiThumbRangeRef: MutableRefObject<HTMLDivElement | null>;
setHint: (hintPresent: boolean) => void;
setHasHint: (hintPresent: boolean) => void;
hasHint: boolean;
setHasMessage: (messagePresent: boolean) => void;
hasMessage: boolean;
}

export const FieldContext = createContext<IFieldContext | undefined>(undefined);
Expand Down
16 changes: 8 additions & 8 deletions packages/forms/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.172.tgz#aad774c28e7bfd7a67de25408e03ee5a8c3d028a"
integrity sha512-/BHF5HAx3em7/KkzVKm3LrsD6HZAXuXO1AJZQ3cRRBZj4oHZDviWPYu0aEplAqDFNHZPW6d3G7KN+ONcCCC7pw==

"@zendeskgarden/container-field@^1.3.6":
version "1.3.8"
resolved "https://registry.yarnpkg.com/@zendeskgarden/container-field/-/container-field-1.3.8.tgz#b92572527a45d3b9c58c8951f47996d0ea93376c"
integrity sha512-WWvyjNsCzbE1br3VEgQEiyTwrmsBP/yi+mq6QkUu8+ArtJ46tbX+2FcD4un39nnqrVfFyNIdOoHertt2rIHX5w==
"@zendeskgarden/container-field@^2.1.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@zendeskgarden/container-field/-/container-field-2.1.0.tgz#ed1e0fb8c320dfafb78e000eea5dfe197b980cf4"
integrity sha512-kqfOIsIUeWATva5DBo2nVpbtrPgRdnyOu1bOeZWH9B7tdhNIIStWnwrcj6T4E5L27saZtEtmyH3LEPKJjwDSxA==
dependencies:
"@babel/runtime" "^7.8.4"
react-uid "^2.2.0"
Expand All @@ -60,10 +60,10 @@
"@babel/runtime" "^7.8.4"
"@reach/auto-id" "^0.16.0"

"@zendeskgarden/react-theming@^8.52.0":
version "8.52.0"
resolved "https://registry.yarnpkg.com/@zendeskgarden/react-theming/-/react-theming-8.52.0.tgz#88d6f8c24515828ebfe313cae76deaa9407bb513"
integrity sha512-LMQr71DPL3hhNR6mRY61JMerXjqLpJp7TZHLe0f+FXeNeA2RI9IXQYUPkTHWDW0Pvc1dWCPd0P9L9AnvYRjyig==
"@zendeskgarden/react-theming@^8.54.0":
version "8.54.0"
resolved "https://registry.yarnpkg.com/@zendeskgarden/react-theming/-/react-theming-8.54.0.tgz#104f0b67f1cb3ce2132a7f065494eabab1ea0f07"
integrity sha512-fkS/dFb+GGJKX8k3m/TjuXPVbf2zhusY37ZN3dOKisCA9ToviCAF0Go+TG+Qzv+//cT1gkdKb76RIFwRxHRbfQ==
dependencies:
"@zendeskgarden/container-focusvisible" "^0.4.6"
"@zendeskgarden/container-utilities" "^0.7.0"
Expand Down