Skip to content

Commit

Permalink
Add message-aria-describedby to checkbox group (#1327)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mottie authored Sep 12, 2024
1 parent 5463bdb commit 5ffb403
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
13 changes: 11 additions & 2 deletions packages/storybook/stories/va-checkbox-group-uswds.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const vaCheckboxGroup = args => {
required,
hint,
'label-header-level': labelHeaderLevel,
'message-aria-describedby': messageAriaDescribedby,
...rest
} = args;
return (
Expand All @@ -43,6 +44,7 @@ const vaCheckboxGroup = args => {
required={required}
hint={hint}
label-header-level={labelHeaderLevel}
message-aria-describedby={messageAriaDescribedby}
>
<va-checkbox label="Sojourner Truth" name="example" value="1" />
<va-checkbox label="Frederick Douglass" name="example" value="2" />
Expand Down Expand Up @@ -152,6 +154,7 @@ const defaultArgs = {
'form-heading-level': null,
'form-heading': null,
'form-description': null,
'message-aria-describedby': null,
};

export const Default = Template.bind(null);
Expand Down Expand Up @@ -206,7 +209,7 @@ const FormsPatternMultipleTemplate = ({
label,
required,
tile,
'message-aria-describedby': messageAriaDescribedBy,
'message-aria-describedby': messageAriaDescribedby,
}) => {
const handleClick = () => {
const header = document
Expand Down Expand Up @@ -292,7 +295,7 @@ const FormsPatternSingleTemplate = ({
error,
label,
required,
'message-aria-describedby': messageAriaDescribedBy,
'message-aria-describedby': messageAriaDescribedby,
}) => {
const id = Math.floor(Math.random() * 10) + 1;
const handleClick = () => {
Expand Down Expand Up @@ -362,6 +365,12 @@ Tile.args = {
label: 'Select any historical figure',
};

export const withDescriptionMessage = Template.bind(null);
withDescriptionMessage.args = {
...defaultArgs,
'message-aria-describedby': 'some additional info',
};

export const Internationalization = I18nTemplate.bind(null);
Internationalization.args = {
...defaultArgs,
Expand Down
8 changes: 8 additions & 0 deletions packages/web-components/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,10 @@ export namespace Components {
* Insert a header with defined level inside the label (legend)
*/
"labelHeaderLevel"?: string;
/**
* An optional message that will be read by screen readers when a checkbox is focused.
*/
"messageAriaDescribedby"?: string;
/**
* Whether or not this input field is required.
*/
Expand Down Expand Up @@ -3437,6 +3441,10 @@ declare namespace LocalJSX {
* Insert a header with defined level inside the label (legend)
*/
"labelHeaderLevel"?: string;
/**
* An optional message that will be read by screen readers when a checkbox is focused.
*/
"messageAriaDescribedby"?: string;
/**
* The event used to track usage of the component. This is emitted when an input value changes and enableAnalytics is true.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ describe('va-checkbox-group', () => {
`);
});

it('renders aria message text if included', async () => {
const page = await newE2EPage();
await page.setContent('<va-checkbox-group message-aria-describedby="Some additional info"></va-checkbox-group>');
const message = await page.find('va-checkbox-group >>> #description-message');
expect(message.textContent).toEqual("Some additional info");
expect(message).toHaveClass("usa-sr-only");
});

it('useFormsPattern displays header for the single field pattern', async () => {
const page = await newE2EPage();
await page.setContent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export class VaCheckboxGroup {
* Insert a header with defined level inside the label (legend)
*/
@Prop() labelHeaderLevel?: string;
/**
* An optional message that will be read by screen readers when a checkbox is focused.
*/
@Prop() messageAriaDescribedby?: string;
/**
* Enabling this will add a heading and description for integrating into the forms pattern. Accepts `single` or `multiple` to indicate if the form is a single input or will have multiple inputs.
*/
Expand All @@ -76,7 +80,7 @@ export class VaCheckboxGroup {
* The heading level for the heading if `useFormsPattern` is true.
*/
@Prop() formHeadingLevel?: number = 3;

/**
* The content of the heading if `useFormsPattern` is true.
*/
Expand Down Expand Up @@ -132,15 +136,16 @@ export class VaCheckboxGroup {
required,
error,
hint,
messageAriaDescribedby,
useFormsPattern,
formHeadingLevel,
formHeading, } = this;
const HeaderLevel = this.getHeaderLevel();
const ariaLabeledByIds =
`${useFormsPattern && formHeading ? 'form-question' : ''} ${
const ariaLabeledByIds =
`${useFormsPattern && formHeading ? 'form-question' : ''} ${
useFormsPattern ? 'form-description' : ''} ${
useFormsPattern === 'multiple' ? 'header-message' : ''}`.trim() || null;

const messageAriaDescribedbyId = messageAriaDescribedby ? 'description-message' : null;

const legendClass = classnames({
'usa-legend': true,
Expand Down Expand Up @@ -170,7 +175,7 @@ export class VaCheckboxGroup {
{formsHeading}
<div class="input-wrap">
<fieldset class="usa-fieldset" aria-labelledby={ariaLabeledByIds}>
<legend class={legendClass}>
<legend class={legendClass} aria-describedby={messageAriaDescribedbyId}>
{HeaderLevel ? (
<HeaderLevel part="header">{label}</HeaderLevel>
) : (
Expand All @@ -183,6 +188,11 @@ export class VaCheckboxGroup {
</span>
)
}
{messageAriaDescribedby && (
<span id="description-message" class="usa-sr-only">
{messageAriaDescribedby}
</span>
)}
{required && <span class="usa-label--required">{i18next.t('required')}</span>}
{hint && <div class="usa-hint">{hint}</div>}
</legend>
Expand Down

0 comments on commit 5ffb403

Please sign in to comment.