@dreamworld/dw-chip-select 1.0.0-integrate-chip-in-chip-select.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.
package/dw-chip-select.js CHANGED
@@ -5,7 +5,7 @@ import { repeat } from "lit/directives/repeat.js";
5
5
  import * as TypographyLiterals from "@dreamworld/material-styles/typography-literals.js";
6
6
 
7
7
  // Utils
8
- import { ChipTypes } from "./utils";
8
+ import { ChipTypes, KeyCode } from "./utils";
9
9
  import isEqual from "lodash-es/isEqual.js";
10
10
  import cloneDeep from "lodash-es/cloneDeep.js";
11
11
 
@@ -16,6 +16,7 @@ export class DwChipSelect extends LitElement {
16
16
  return css`
17
17
  :host {
18
18
  display: block;
19
+ outline: none;
19
20
  }
20
21
 
21
22
  .label {
@@ -91,20 +92,35 @@ export class DwChipSelect extends LitElement {
91
92
  */
92
93
  valueEquator: { type: Function },
93
94
 
95
+ /**
96
+ * Whether component is focused or not
97
+ */
98
+ focused: { type: Boolean },
99
+
94
100
  /**
95
101
  * Computed property from valueProvider and valueExpression
96
102
  */
97
103
  _valueProvider: { type: Function },
104
+
105
+ /**
106
+ * index of activated Item
107
+ * default: -1
108
+ */
109
+ _activatedIndex: { type: Number },
98
110
  };
99
111
  }
100
112
 
101
113
  constructor() {
102
114
  super();
115
+ this.tabIndex = "0";
103
116
  this.valueTextProvider = (item) => item;
104
117
  this._valueProvider = (item) => item;
105
118
  this.type = ChipTypes.filter;
119
+ this._activatedIndex = -1;
106
120
 
107
121
  this.valueEquator = (v1, v2) => v1 === v2;
122
+
123
+ this._onKeyDown = this._onKeyDown.bind(this);
108
124
  }
109
125
 
110
126
  render() {
@@ -122,9 +138,11 @@ export class DwChipSelect extends LitElement {
122
138
  return html`<div class="chip-wrapper">
123
139
  ${repeat(this.items, (item, index) => {
124
140
  const selected = this.#_isSelected(item);
141
+ const activated = this.#_isActivated(index);
125
142
  return html`<dw-chip
126
143
  .value=${this.valueTextProvider(item)}
127
144
  ?selected=${selected}
145
+ ?activated=${activated}
128
146
  .type=${this.type}
129
147
  @click=${() => this._onChipToggle(item, selected)}
130
148
  ></dw-chip>`;
@@ -135,6 +153,11 @@ export class DwChipSelect extends LitElement {
135
153
  connectedCallback() {
136
154
  super.connectedCallback();
137
155
  this.#_computeValueProvider();
156
+
157
+ this.addEventListener("focusin", this._onFocusIn);
158
+ this.addEventListener("focusout", this._onFocusOut);
159
+
160
+ window.addEventListener("keydown", this._onKeyDown);
138
161
  }
139
162
 
140
163
  willUpdate(_changedProperties) {
@@ -197,6 +220,65 @@ export class DwChipSelect extends LitElement {
197
220
 
198
221
  return this.valueEquator(this._valueProvider(item), this.value);
199
222
  }
223
+
224
+ #_isActivated(index) {
225
+ return this._activatedIndex === index;
226
+ }
227
+
228
+ _onFocusIn() {
229
+ this._activatedIndex = 0;
230
+ this.focused = true;
231
+ }
232
+
233
+ _onFocusOut() {
234
+ this._activatedIndex = -1;
235
+ this.focused = false;
236
+ }
237
+
238
+ _onKeyDown(e) {
239
+ if (!this.focused) {
240
+ return;
241
+ }
242
+ const { keyCode } = e;
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:
249
+ e.stopPropagation();
250
+ e.preventDefault();
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;
263
+ }
264
+ break;
265
+ case KeyCode.ARROW_UP:
266
+ case KeyCode.ARROW_LEFT:
267
+ if (itemsLength > 0) {
268
+ this._activatedIndex = (this._activatedIndex - 1 + itemsLength) % itemsLength;
269
+ }
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;
280
+ }
281
+ }
200
282
  }
201
283
 
202
284
  customElements.define(componentName, DwChipSelect);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dreamworld/dw-chip-select",
3
- "version": "1.0.0-integrate-chip-in-chip-select.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": {
package/utils.js CHANGED
@@ -2,4 +2,13 @@ export const ChipTypes = {
2
2
  filter: "filter",
3
3
  choice: "choice",
4
4
  input: "input",
5
+ };
6
+
7
+ export const KeyCode = {
8
+ ENTER: 13,
9
+ ARROW_UP: 38,
10
+ ARROW_DOWN: 40,
11
+ ARROW_LEFT: 37,
12
+ ARROW_RIGHT: 39,
13
+ SPACE: 32,
5
14
  };