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

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/README.md CHANGED
@@ -1,3 +1,3 @@
1
- # dw-chip-select
1
+ # `<dw-chip-select>` [![Published on npm](https://img.shields.io/npm/v/@dreamworld/dw-chip-select.svg)](https://www.npmjs.com/package/@dreamworld/dw-chip-select)
2
2
 
3
3
  Chips are compact elements that represent an input, attribute, or action.
@@ -1,11 +1,9 @@
1
+ import { ThemeStyle } from "@dreamworld/material-styles/theme.js";
1
2
  import { css, html, LitElement } from "lit";
2
3
  import "../dw-chip-select.js";
3
4
  import "../dw-chip.js";
4
- import { ThemeStyle } from "@dreamworld/material-styles/theme.js";
5
5
  import { country_list, country_with_code, PaymentTypes } from "./utils.js";
6
6
 
7
- const componentName = "dw-chip-select-demo";
8
-
9
7
  export class DwChipSelectDemo extends LitElement {
10
8
  static get styles() {
11
9
  return [
@@ -65,4 +63,4 @@ export class DwChipSelectDemo extends LitElement {
65
63
  }
66
64
  }
67
65
 
68
- customElements.define(componentName, DwChipSelectDemo);
66
+ customElements.define("dw-chip-select-demo", DwChipSelectDemo);
package/dw-chip-select.js CHANGED
@@ -9,8 +9,6 @@ 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
 
12
- const componentName = "dw-chip-select";
13
-
14
12
  export class DwChipSelect extends LitElement {
15
13
  static get styles() {
16
14
  return css`
@@ -144,7 +142,7 @@ export class DwChipSelect extends LitElement {
144
142
  ?selected=${selected}
145
143
  ?activated=${activated}
146
144
  .type=${this.type}
147
- @click=${() => this._onChipToggle(item, selected)}
145
+ @click=${() => this._onChipToggle(item, selected, index)}
148
146
  ></dw-chip>`;
149
147
  })}
150
148
  </div>`;
@@ -156,17 +154,29 @@ export class DwChipSelect extends LitElement {
156
154
 
157
155
  this.addEventListener("focusin", this._onFocusIn);
158
156
  this.addEventListener("focusout", this._onFocusOut);
157
+ this.addEventListener('mousedown', this._onMouseDown)
159
158
 
160
159
  window.addEventListener("keydown", this._onKeyDown);
161
160
  }
162
161
 
162
+ disconnectedCallback() {
163
+ super.disconnectedCallback();
164
+
165
+ this.removeEventListener("focusin", this._onFocusIn);
166
+ this.removeEventListener("focusout", this._onFocusOut);
167
+ this.removeEventListener('mousedown', this._onMouseDown)
168
+
169
+ window.removeEventListener("keydown", this._onKeyDown);
170
+ }
171
+
163
172
  willUpdate(_changedProperties) {
164
173
  if (_changedProperties.has("valueProvider") || _changedProperties.has("valueExpression")) {
165
174
  this.#_computeValueProvider();
166
175
  }
167
176
  }
168
177
 
169
- _onChipToggle(item, selected) {
178
+ _onChipToggle(item, selected, index) {
179
+ this._activatedIndex = index;
170
180
  const oItem = this._valueProvider(item);
171
181
  let oValue = this.value;
172
182
  const previousValue = cloneDeep(oValue);
@@ -225,8 +235,12 @@ export class DwChipSelect extends LitElement {
225
235
  return this._activatedIndex === index;
226
236
  }
227
237
 
238
+ _onMouseDown() {
239
+ this.mouseDowned = true;
240
+ }
241
+
228
242
  _onFocusIn() {
229
- this._activatedIndex = 0;
243
+ this.mouseDowned ? this.mouseDowned = false : this._activatedIndex = 0;
230
244
  this.focused = true;
231
245
  }
232
246
 
@@ -236,37 +250,51 @@ export class DwChipSelect extends LitElement {
236
250
  }
237
251
 
238
252
  _onKeyDown(e) {
253
+ if (!this.focused) {
254
+ return;
255
+ }
239
256
  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
- ) {
257
+ switch (keyCode) {
258
+ case KeyCode.ARROW_DOWN:
259
+ case KeyCode.ARROW_LEFT:
260
+ case KeyCode.ARROW_RIGHT:
261
+ case KeyCode.ARROW_UP:
262
+ case KeyCode.ENTER:
263
+ case KeyCode.SPACE:
247
264
  e.stopPropagation();
248
265
  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
- }
266
+ break;
267
+ default:
268
+ break;
269
+ }
270
+
271
+ const itemsLength = this.items ? this.items.length : 0;
272
+
273
+ switch (keyCode) {
274
+ case KeyCode.ARROW_DOWN:
275
+ case KeyCode.ARROW_RIGHT:
276
+ if (itemsLength > 0) {
277
+ this._activatedIndex = (this._activatedIndex + 1) % itemsLength;
257
278
  }
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
- }
279
+ break;
280
+ case KeyCode.ARROW_UP:
281
+ case KeyCode.ARROW_LEFT:
282
+ if (itemsLength > 0) {
283
+ this._activatedIndex = (this._activatedIndex - 1 + itemsLength) % itemsLength;
266
284
  }
267
- }
285
+ break;
286
+ case KeyCode.ENTER:
287
+ case KeyCode.SPACE:
288
+ if (this._activatedIndex > -1) {
289
+ const activatedItem = this.items[this._activatedIndex];
290
+ const isSelected = this.#_isSelected(activatedItem);
291
+ this._onChipToggle(activatedItem, isSelected, this._activatedIndex);
292
+ }
293
+ break;
294
+ default:
295
+ break;
268
296
  }
269
297
  }
270
298
  }
271
299
 
272
- customElements.define(componentName, DwChipSelect);
300
+ customElements.define("dw-chip-select", DwChipSelect);
package/dw-chip.js CHANGED
@@ -1,14 +1,12 @@
1
- import { html, LitElement, css, nothing } from "lit";
2
- import "@dreamworld/dw-ripple";
3
1
  import "@dreamworld/dw-icon";
2
+ import "@dreamworld/dw-ripple";
3
+ import { css, html, LitElement, nothing } from "lit";
4
4
  import { ChipTypes } from "./utils";
5
5
  import { classMap } from "lit/directives/class-map.js";
6
- import {animate} from '@lit-labs/motion';
6
+ import { animate } from "@lit-labs/motion";
7
7
 
8
8
  // Styles
9
- import * as TypographyLiterals from "@dreamworld/material-styles/typography-literals.js";
10
-
11
- const componentName = "dw-chip";
9
+ import { body2 } from "@dreamworld/material-styles/typography-literals.js";
12
10
 
13
11
  export class DwChip extends LitElement {
14
12
  static get styles() {
@@ -21,6 +19,8 @@ export class DwChip extends LitElement {
21
19
  padding-right: 12px;
22
20
  height: var(--dw-chip-height, 32px);
23
21
  border-radius: calc(var(--dw-chip-height, 32px) / 2);
22
+ border: 1px solid transparent;
23
+ ${body2};
24
24
 
25
25
  overflow: hidden;
26
26
  box-sizing: border-box;
@@ -35,24 +35,15 @@ export class DwChip extends LitElement {
35
35
  color: var(--mdc-theme-primary, #02afcd);
36
36
  }
37
37
 
38
- dw-ripple {
39
- position: absolute;
40
- }
41
-
42
38
  :host([selected]) dw-icon {
43
39
  padding-right: 8px;
44
40
  --dw-icon-color: var(--mdc-theme-primary, #02afcd);
45
41
  }
46
42
 
47
- .value {
48
- ${TypographyLiterals.body2};
49
- }
50
-
51
43
  .hide {
52
44
  width: 0;
53
45
  overflow: hidden;
54
46
  }
55
-
56
47
  .show {
57
48
  width: max-content;
58
49
  }
@@ -119,7 +110,7 @@ export class DwChip extends LitElement {
119
110
  if (this.type === ChipTypes.filter) {
120
111
  return html`<dw-icon
121
112
  name=${this.icon}
122
- class="hide ${classMap({ show: this.selected })}"
113
+ class="${classMap({ show: this.selected, hide: !this.selected })}"
123
114
  ${animate()}
124
115
  ></dw-icon>`;
125
116
  }
@@ -127,4 +118,4 @@ export class DwChip extends LitElement {
127
118
  }
128
119
  }
129
120
 
130
- customElements.define(componentName, DwChip);
121
+ customElements.define("dw-chip", DwChip);
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-update-readme.1",
4
4
  "description": "Chips are compact elements that represent an input, attribute, or action.",
5
5
  "main": "dw-chip-select.js",
6
6
  "scripts": {
@@ -30,7 +30,7 @@
30
30
  ],
31
31
  "dependencies": {
32
32
  "@dreamworld/dw-icon": "^4.1.1",
33
- "@dreamworld/dw-ripple": "^3.1.3-fix-package-json.1",
33
+ "@dreamworld/dw-ripple": "^3.1.3",
34
34
  "@dreamworld/material-styles": "^3.2.2",
35
35
  "@lit-labs/motion": "^1.0.3",
36
36
  "lit": "^2.7.0",
package/utils.js CHANGED
@@ -11,4 +11,4 @@ export const KeyCode = {
11
11
  ARROW_LEFT: 37,
12
12
  ARROW_RIGHT: 39,
13
13
  SPACE: 32,
14
- };
14
+ };