-
Notifications
You must be signed in to change notification settings - Fork 12
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
va-checkbox: add indeterminate prop #1426
Changes from 6 commits
dc53b25
b4aebf0
53eb6b4
1df2169
7380476
6a4d45f
df914c5
218ce4e
667d9d2
c0b83ad
e3f7c0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,6 +106,11 @@ export class VaCheckbox { | |
*/ | ||
@Prop() name?: string; | ||
|
||
/** | ||
* When true, the checkbox can be toggled between checked and indeterminate states. | ||
*/ | ||
@Prop() indeterminate?: boolean = false; | ||
|
||
/** | ||
* The event used to track usage of the component. This is emitted when the | ||
* input value changes and enableAnalytics is true. | ||
|
@@ -154,6 +159,26 @@ export class VaCheckbox { | |
if (this.enableAnalytics) this.fireAnalyticsEvent(); | ||
}; | ||
|
||
/** | ||
* For a11y, input.indeterminate must be set with JavaScript, there is no HTML attribute for this. | ||
*/ | ||
private updateIndeterminateInput() { | ||
const input = this.el.shadowRoot.querySelector('input'); | ||
if (this.indeterminate && !this.checked) { | ||
input.indeterminate = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are adding this indeterminate attribute manually with JavaScript based on Ryan's a11y findings here. When I tested with VO, I confirmed that this is what announces "mixed" as the checkbox state when it's active. |
||
} else { | ||
input.indeterminate = false; | ||
} | ||
} | ||
|
||
componentDidUpdate() { | ||
this.updateIndeterminateInput(); | ||
} | ||
|
||
componentDidLoad() { | ||
this.updateIndeterminateInput(); | ||
} | ||
|
||
connectedCallback() { | ||
i18next.on('languageChanged', () => { | ||
forceUpdate(this.el); | ||
|
@@ -176,7 +201,8 @@ export class VaCheckbox { | |
checkboxDescription, | ||
disabled, | ||
messageAriaDescribedby, | ||
name | ||
name, | ||
indeterminate, | ||
} = this; | ||
const hasDescriptionSlot = | ||
!description && | ||
|
@@ -230,14 +256,15 @@ export class VaCheckbox { | |
aria-describedby={ariaDescribedbyIds} | ||
aria-invalid={error ? 'true' : 'false'} | ||
disabled={disabled} | ||
data-indeterminate={indeterminate && !checked} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The USWDS release notes say:
...but I couldn't get everything working for both the styles and screenreader without adding both |
||
onChange={this.handleChange} | ||
/> | ||
<label | ||
htmlFor="checkbox-element" | ||
class="usa-checkbox__label" | ||
part="label" | ||
role="checkbox" | ||
aria-checked={ariaChecked} | ||
aria-checked={indeterminate && !checked ? 'mixed' : ariaChecked} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm adding this because otherwise the Setting |
||
> | ||
{label} | ||
{required && ( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried also to get a test working that checked if the input element was getting the
.indeterminate
property from being set in JS but I could not get it to work. :/This is kind of a strange property though so either it's some kind of testing limitation with Stencil or I'm just not writing the test in a way that it can pick up that value.