From 8fa48910518fd35930ffbb4535a44c31aab07338 Mon Sep 17 00:00:00 2001 From: Marcos Creuz Filho Date: Fri, 17 Mar 2023 10:17:38 -0300 Subject: [PATCH 1/4] feat(input): add a11y attributes to Input --- packages/yoga/src/Input/web/Helper.jsx | 9 +++++- packages/yoga/src/Input/web/Input.jsx | 38 ++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/packages/yoga/src/Input/web/Helper.jsx b/packages/yoga/src/Input/web/Helper.jsx index 32abe0930b..55a8bba3b7 100644 --- a/packages/yoga/src/Input/web/Helper.jsx +++ b/packages/yoga/src/Input/web/Helper.jsx @@ -47,9 +47,14 @@ const Helper = ({ maxLength, length, hideMaxLength, + a11yId, }) => ( - {(error || helper) && {error || helper}} + {(error || helper) && ( + + {error || helper} + + )} {maxLength && !hideMaxLength && ( {length}/{maxLength} @@ -65,6 +70,7 @@ Helper.propTypes = { maxLength: number, length: number, hideMaxLength: bool, + a11yId: string, }; Helper.defaultProps = { @@ -74,6 +80,7 @@ Helper.defaultProps = { maxLength: undefined, length: undefined, hideMaxLength: undefined, + a11yId: undefined, }; export default Helper; diff --git a/packages/yoga/src/Input/web/Input.jsx b/packages/yoga/src/Input/web/Input.jsx index 604bd4643c..df19cf4bbe 100644 --- a/packages/yoga/src/Input/web/Input.jsx +++ b/packages/yoga/src/Input/web/Input.jsx @@ -96,6 +96,8 @@ const Input = React.forwardRef( onClean, hideMaxLength, rightIcon, + a11yId, + includeAriaAttributes, ...props }, ref, @@ -112,6 +114,26 @@ const Input = React.forwardRef( } }; + const hasHelper = helper || maxLength || error; + + const helperA11yId = includeAriaAttributes && a11yId && `${a11yId}-helper`; + const labelA11yId = includeAriaAttributes && a11yId && `${a11yId}-label`; + let a11yFieldProps; + + if (includeAriaAttributes) { + a11yFieldProps = a11yId + ? { + ...(hasHelper && { 'aria-describedby': helperA11yId }), + ...(label && { 'aria-labelledby': labelA11yId }), + } + : { + ...(label && { 'aria-label': label }), + }; + a11yFieldProps['aria-invalid'] = !!error; + } else { + a11yFieldProps = {}; + } + return (
) : ( children )} -
- {(helper || maxLength || error) && ( + {hasHelper && ( )}
@@ -213,6 +241,10 @@ Input.propTypes = { hideMaxLength: bool, placeholder: string, rightIcon: node, + /** a unique prefix to generate HTML IDs to be used for aria-labelledby and aria-describedby */ + a11yId: string, + /** useful for components that extend the Input component and have their own ARIA attributes implementation (e.g. Dropdown) */ + includeAriaAttributes: bool, }; Input.defaultProps = { @@ -233,6 +265,8 @@ Input.defaultProps = { hideMaxLength: false, placeholder: undefined, rightIcon: undefined, + a11yId: undefined, + includeAriaAttributes: true, }; export default Input; From c28e8102d757ab7b52335d6066bdf84a76c1f4c6 Mon Sep 17 00:00:00 2001 From: Marcos Creuz Filho Date: Fri, 17 Mar 2023 10:23:34 -0300 Subject: [PATCH 2/4] feat(input): prevent aria attributes to be added in the Input rendered on Dropdown and AutoComplete --- packages/yoga/src/AutoComplete/web/AutoComplete.jsx | 1 + packages/yoga/src/Dropdown/web/Dropdown.jsx | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/yoga/src/AutoComplete/web/AutoComplete.jsx b/packages/yoga/src/AutoComplete/web/AutoComplete.jsx index a950371e70..59d5875841 100644 --- a/packages/yoga/src/AutoComplete/web/AutoComplete.jsx +++ b/packages/yoga/src/AutoComplete/web/AutoComplete.jsx @@ -273,6 +273,7 @@ const AutoComplete = React.forwardRef( ) } ref={inputRef} + includeAriaAttributes={false} /> {isSuggestionsOpen && ( diff --git a/packages/yoga/src/Dropdown/web/Dropdown.jsx b/packages/yoga/src/Dropdown/web/Dropdown.jsx index 050a3d1787..e933d278de 100644 --- a/packages/yoga/src/Dropdown/web/Dropdown.jsx +++ b/packages/yoga/src/Dropdown/web/Dropdown.jsx @@ -305,6 +305,7 @@ const Dropdown = React.forwardRef( label={label} full={full} ref={inputRef} + includeAriaAttributes={false} {...getInputProps()} />