Merge pull request #728 from vector-im/dbkr/aria-describedby

Add aria-describedby associations
This commit is contained in:
David Baker 2022-11-07 22:45:37 +00:00 committed by GitHub
commit 85b02a3589
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import React, { ChangeEvent, FC, forwardRef, ReactNode } from "react"; import React, { ChangeEvent, FC, forwardRef, ReactNode, useId } from "react";
import classNames from "classnames"; import classNames from "classnames";
import styles from "./Input.module.css"; import styles from "./Input.module.css";
@ -96,6 +96,8 @@ export const InputField = forwardRef<
}, },
ref ref
) => { ) => {
const descriptionId = useId();
return ( return (
<Field <Field
className={classNames( className={classNames(
@ -113,6 +115,7 @@ export const InputField = forwardRef<
id={id} id={id}
ref={ref as React.ForwardedRef<HTMLTextAreaElement>} ref={ref as React.ForwardedRef<HTMLTextAreaElement>}
disabled={disabled} disabled={disabled}
aria-describedby={descriptionId}
{...rest} {...rest}
/> />
) : ( ) : (
@ -122,6 +125,7 @@ export const InputField = forwardRef<
type={type} type={type}
checked={checked} checked={checked}
disabled={disabled} disabled={disabled}
aria-describedby={descriptionId}
{...rest} {...rest}
/> />
)} )}
@ -135,7 +139,11 @@ export const InputField = forwardRef<
{label} {label}
</label> </label>
{suffix && <span>{suffix}</span>} {suffix && <span>{suffix}</span>}
{description && <p className={styles.description}>{description}</p>} {description && (
<p id={descriptionId} className={styles.description}>
{description}
</p>
)}
</Field> </Field>
); );
} }