Skip to content

Commit

Permalink
fix: ensure aria attributes based on state
Browse files Browse the repository at this point in the history
  • Loading branch information
Westbrook committed May 4, 2020
1 parent c8d3c60 commit 6369ff3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
24 changes: 15 additions & 9 deletions __snapshots__/Switch.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@

```html
<input
aria-checked="false"
id="input"
role="switch"
tabindex="0"
type="checkbox"
/>
<span id="switch"></span>
<label for="input" id="label">
<slot></slot>
id="input"
role="switch"
tabindex="0"
type="checkbox"
>
<span id="switch">
</span>
<label
for="input"
id="label"
>
<slot>
</slot>
</label>

```

5 changes: 0 additions & 5 deletions packages/checkbox/src/checkbox-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,11 @@ export class CheckboxBase extends Focusable {
this.dispatchEvent(changeEvent);
}

protected get ariaCheckedState(): 'true' | 'false' | 'mixed' {
return this.checked ? 'true' : 'false';
}

protected render(): TemplateResult {
return html`
<input
id="input"
type="checkbox"
aria-checked=${this.ariaCheckedState}
.checked=${this.checked}
@change=${this.handleChange}
/>
Expand Down
15 changes: 9 additions & 6 deletions packages/checkbox/src/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ export class Checkbox extends CheckboxBase {
];
}

protected get ariaCheckedState(): 'true' | 'false' | 'mixed' {
return this.indeterminate ? 'mixed' : super.ariaCheckedState;
}

protected render(): TemplateResult {
return html`
<label id="root">
Expand All @@ -64,14 +60,21 @@ export class Checkbox extends CheckboxBase {
`;
}

protected firstUpdated(changes: PropertyValues): void {
super.firstUpdated(changes);
protected updated(changes: PropertyValues): void {
super.updated(changes);
if (changes.has('invalid')) {
if (this.invalid) {
this.inputElement.setAttribute('aria-invalid', 'true');
} else {
this.inputElement.removeAttribute('aria-invalid');
}
}
if (changes.has('indeterminate')) {
if (this.indeterminate) {
this.inputElement.setAttribute('aria-checked', 'mixed');
} else {
this.inputElement.removeAttribute('aria-checked');
}
}
}
}

0 comments on commit 6369ff3

Please sign in to comment.