@dreamworld/dw-chip-select 1.0.0-support-keyboard-navigation.1 → 1.0.0-support-keyboard-navigation.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dw-chip-select.js +36 -24
  2. package/package.json +1 -1
package/dw-chip-select.js CHANGED
@@ -236,35 +236,47 @@ export class DwChipSelect extends LitElement {
236
236
  }
237
237
 
238
238
  _onKeyDown(e) {
239
+ if (!this.focused) {
240
+ return;
241
+ }
239
242
  const { keyCode } = e;
240
- if (this.focused) {
241
- if (
242
- KeyCode.ARROW_DOWN === keyCode ||
243
- KeyCode.ARROW_LEFT === keyCode ||
244
- KeyCode.ARROW_RIGHT === keyCode ||
245
- KeyCode.ARROW_UP === keyCode
246
- ) {
243
+ switch (keyCode) {
244
+ case KeyCode.ARROW_DOWN:
245
+ case KeyCode.ARROW_LEFT:
246
+ case KeyCode.ARROW_RIGHT:
247
+ case KeyCode.ARROW_UP:
248
+ case KeyCode.ENTER:
247
249
  e.stopPropagation();
248
250
  e.preventDefault();
249
- }
250
- if (KeyCode.ARROW_DOWN === keyCode || KeyCode.ARROW_LEFT === keyCode) {
251
- if (this.items && this.items.length > 0) {
252
- if (this.items.length - 1 === this._activatedIndex) {
253
- this._activatedIndex = 0;
254
- } else {
255
- this._activatedIndex++;
256
- }
251
+ break;
252
+ default:
253
+ break;
254
+ }
255
+
256
+ const itemsLength = this.items ? this.items.length : 0;
257
+
258
+ switch (keyCode) {
259
+ case KeyCode.ARROW_DOWN:
260
+ case KeyCode.ARROW_RIGHT:
261
+ if (itemsLength > 0) {
262
+ this._activatedIndex = (this._activatedIndex + 1) % itemsLength;
257
263
  }
258
- }
259
- if (KeyCode.ARROW_RIGHT === keyCode || KeyCode.ARROW_UP === keyCode) {
260
- if (this.items && this.items.length > 0) {
261
- if (this._activatedIndex === 0) {
262
- this._activatedIndex = this.items.length - 1;
263
- } else {
264
- this._activatedIndex--;
265
- }
264
+ break;
265
+ case KeyCode.ARROW_UP:
266
+ case KeyCode.ARROW_LEFT:
267
+ if (itemsLength > 0) {
268
+ this._activatedIndex = (this._activatedIndex - 1 + itemsLength) % itemsLength;
266
269
  }
267
- }
270
+ break;
271
+ case KeyCode.ENTER:
272
+ if (this._activatedIndex > -1) {
273
+ const activatedItem = this.items[this._activatedIndex];
274
+ const isSelected = this.#_isSelected(activatedItem);
275
+ this._onChipToggle(activatedItem, isSelected);
276
+ }
277
+ break;
278
+ default:
279
+ break;
268
280
  }
269
281
  }
270
282
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dreamworld/dw-chip-select",
3
- "version": "1.0.0-support-keyboard-navigation.1",
3
+ "version": "1.0.0-support-keyboard-navigation.2",
4
4
  "description": "Chips are compact elements that represent an input, attribute, or action.",
5
5
  "main": "dw-chip-select.js",
6
6
  "scripts": {