Skip to content

Commit

Permalink
feat(checkbox): Add keyboard handling for Enter and Space keys to tog… (
Browse files Browse the repository at this point in the history
#946)

### Summary of Changes
- Added keyboard functionality to the input component to toggle the
checked state when the Enter or Space key is pressed.
### Changes Made
1. Implemented the handleKeyDown function:
```
private handleKeyDown(event: KeyboardEvent) {
    if (event.code === "Enter" || event.code === "Space") {
        this.checked = !this.checked;
        this.onChange(this.checked);
        event.preventDefault();
    }
}

```
2. Associated the function with the @keydown event listener:
`<input @keydown=${this.handleKeyDown} />`

### Reason for Changes
This change was made to enhance user interaction with form elements via
the keyboard and to improve accessibility.

---------

Co-authored-by: Buse Selvi <[email protected]>
Co-authored-by: Erbil <[email protected]>
  • Loading branch information
3 people authored Nov 13, 2024
1 parent c686e21 commit dd096eb
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/components/checkbox-group/checkbox/bl-checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ export default class BlCheckbox extends FormControlMixin(LitElement) {
private handleFieldValueChange = (event: CustomEvent<Array<string>>) => {
this.checked = event.detail.includes(this.value);
};
private handleKeyDown(event: KeyboardEvent) {
if (event.code === "Space") {
this.checked = !this.checked;
this.onChange(this.checked);
event.preventDefault();
}
}

render(): TemplateResult {
let icon = "";
Expand Down Expand Up @@ -206,6 +213,7 @@ export default class BlCheckbox extends FormControlMixin(LitElement) {
aria-readonly=${this.disabled}
.indeterminate=${this.indeterminate}
@change=${this.handleChange}
@keydown=${this.handleKeyDown}
value=${ifDefined(this.value)}
@blur=${this.blur}
/>
Expand Down

0 comments on commit dd096eb

Please sign in to comment.