@dreamworld/dw-chip-select 1.0.0-create-chip.1 → 1.0.0-support-keyboard-navigation.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.
@@ -2,6 +2,7 @@ import { css, html, LitElement } from "lit";
2
2
  import "../dw-chip-select.js";
3
3
  import "../dw-chip.js";
4
4
  import { ThemeStyle } from "@dreamworld/material-styles/theme.js";
5
+ import { country_list, country_with_code, PaymentTypes } from "./utils.js";
5
6
 
6
7
  const componentName = "dw-chip-select-demo";
7
8
 
@@ -18,19 +19,50 @@ export class DwChipSelectDemo extends LitElement {
18
19
  .wrapper {
19
20
  display: flex;
20
21
  }
22
+
23
+ dw-chip-select {
24
+ margin-top: 16px;
25
+ }
21
26
  `,
22
27
  ];
23
28
  }
24
29
  render() {
25
30
  return html`
26
- <dw-chip-select></dw-chip-select> <br />
27
- <div class="wrapper">
28
- <dw-chip .value=${"Chip View"}></dw-chip>
31
+ <dw-chip-select
32
+ label="Filter"
33
+ .valueExpression=${"name"}
34
+ .items=${PaymentTypes}
35
+ .valueTextProvider=${(item) => item.label}
36
+ @change=${this._onChange}
37
+ ></dw-chip-select>
38
+ <dw-chip-select
39
+ type="choice"
40
+ label="Choice"
41
+ .valueExpression=${"name"}
42
+ .items=${PaymentTypes}
43
+ .valueTextProvider=${(item) => item.label}
44
+ @change=${this._onChange}
45
+ ></dw-chip-select>
29
46
 
30
- <dw-chip .value=${"Selected"} selected></dw-chip>
31
- </div>
47
+ <dw-chip-select
48
+ label="Country Filter"
49
+ .items=${country_list}
50
+ @change=${this._onChange}
51
+ ></dw-chip-select>
52
+
53
+ <dw-chip-select
54
+ label="Country Choice"
55
+ type="choice"
56
+ .items=${country_with_code}
57
+ .valueTextProvider=${(item) => `${item.name}: ${item.code}`}
58
+ @change=${this._onChange}
59
+ ></dw-chip-select>
32
60
  `;
33
61
  }
62
+
63
+ _onChange(e) {
64
+ console.log("change", e);
65
+ }
34
66
  }
35
67
 
36
68
  customElements.define(componentName, DwChipSelectDemo);
package/demo/utils.js ADDED
@@ -0,0 +1,55 @@
1
+ export const PaymentTypes = [
2
+ { name: "RECEIPT", label: "Receipt" },
3
+ { name: "PAYMENT", label: "Payment" },
4
+ ];
5
+
6
+ export const country_list = [
7
+ "Afghanistan",
8
+ "Albania",
9
+ "Algeria",
10
+ "Andorra",
11
+ "Chile",
12
+ "China",
13
+ "Colombia",
14
+ "India",
15
+ "Malawi",
16
+ "Malaysia",
17
+ "Maldives",
18
+ "Netherlands Antilles",
19
+ "New Caledonia",
20
+ "New Zealand",
21
+ "Nicaragua",
22
+ "Niger",
23
+ "Nigeria",
24
+ "Norway",
25
+ "Oman",
26
+ "Pakistan",
27
+ ];
28
+
29
+ export const country_with_code = [
30
+ { name: "Afghanistan", code: "AF" },
31
+ { name: "land Islands", code: "AX" },
32
+ { name: "Albania", code: "AL" },
33
+ { name: "Algeria", code: "DZ" },
34
+ { name: "American Samoa", code: "AS" },
35
+ { name: "AndorrA", code: "AD" },
36
+ { name: "Angola", code: "AO" },
37
+ { name: "Anguilla", code: "AI" },
38
+ { name: "Antarctica", code: "AQ" },
39
+ { name: "Antigua and Barbuda", code: "AG" },
40
+ { name: "Argentina", code: "AR" },
41
+ { name: "Armenia", code: "AM" },
42
+ { name: "Aruba", code: "AW" },
43
+ { name: "Australia", code: "AU" },
44
+ { name: "Croatia", code: "HR" },
45
+ { name: "Cuba", code: "CU" },
46
+ { name: "Cyprus", code: "CY" },
47
+ { name: "Czech Republic", code: "CZ" },
48
+ { name: "Denmark", code: "DK" },
49
+ { name: "Djibouti", code: "DJ" },
50
+ { name: "Uganda", code: "UG" },
51
+ { name: "Ukraine", code: "UA" },
52
+ { name: "United Arab Emirates", code: "AE" },
53
+ { name: "United Kingdom", code: "GB" },
54
+ { name: "United States", code: "US" },
55
+ ];
package/dw-chip-select.js CHANGED
@@ -1,10 +1,271 @@
1
- import { html, LitElement } from "lit";
1
+ import { css, html, LitElement, nothing } from "lit";
2
+ import { repeat } from "lit/directives/repeat.js";
3
+
4
+ // Styles
5
+ import * as TypographyLiterals from "@dreamworld/material-styles/typography-literals.js";
6
+
7
+ // Utils
8
+ import { ChipTypes, KeyCode } from "./utils";
9
+ import isEqual from "lodash-es/isEqual.js";
10
+ import cloneDeep from "lodash-es/cloneDeep.js";
2
11
 
3
12
  const componentName = "dw-chip-select";
4
13
 
5
14
  export class DwChipSelect extends LitElement {
15
+ static get styles() {
16
+ return css`
17
+ :host {
18
+ display: block;
19
+ outline: none;
20
+ }
21
+
22
+ .label {
23
+ color: var(--mdc-theme-text-secondary-on-surface, rgba(0, 0, 0, 0.6));
24
+ ${TypographyLiterals.body2};
25
+ }
26
+
27
+ .chip-wrapper {
28
+ display: flex;
29
+ flex-wrap: wrap;
30
+ }
31
+
32
+ dw-chip {
33
+ margin-right: 8px;
34
+ margin-top: 8px;
35
+ }
36
+ `;
37
+ }
38
+
39
+ static get properties() {
40
+ return {
41
+ /**
42
+ * This is the type of the chip.
43
+ * The default value is `filter`.
44
+ * Possible values: `filter`, `choice`, and `input`.
45
+ */
46
+ type: { type: String },
47
+
48
+ /**
49
+ * Selected list item object.
50
+ * When the type is `filter`, the value data type is an array of objects.
51
+ * When the type is `choice`, the value data type is an object.
52
+ */
53
+ value: { type: Object },
54
+
55
+ /**
56
+ * This is a list of selectable items.
57
+ */
58
+ items: { type: Array },
59
+
60
+ /**
61
+ * This function returns a string and provides the value that represents the chip item.
62
+ */
63
+ valueTextProvider: { type: Function },
64
+
65
+ /**
66
+ * This function provides the value.
67
+ */
68
+ valueProvider: { type: Function },
69
+
70
+ /**
71
+ * This provides a value based on the input received from the valueExpression key.
72
+ */
73
+ valueExpression: { type: String },
74
+
75
+ /**
76
+ * This sets the name attribute on the internal input.
77
+ * The name property should only be used for browser autofill,
78
+ * as webcomponent form participation does not currently consider the name attribute.
79
+ */
80
+ name: { type: String },
81
+
82
+ /**
83
+ * This sets the label string and shows the label at the top of the chips.
84
+ */
85
+ label: { type: String },
86
+
87
+ /**
88
+ * Set this to configure custom logic to detect whether value is changed or not.
89
+ * Default: compares both values by strict equality (by reference) `v1 === v2`.
90
+ * It must return a Boolean.
91
+ * Function receives 2 arguments: (v1, v2). Should return `true` when both values are same otherwise `false`.
92
+ */
93
+ valueEquator: { type: Function },
94
+
95
+ /**
96
+ * Whether component is focused or not
97
+ */
98
+ focused: { type: Boolean },
99
+
100
+ /**
101
+ * Computed property from valueProvider and valueExpression
102
+ */
103
+ _valueProvider: { type: Function },
104
+
105
+ /**
106
+ * index of activated Item
107
+ * default: -1
108
+ */
109
+ _activatedIndex: { type: Number },
110
+ };
111
+ }
112
+
113
+ constructor() {
114
+ super();
115
+ this.tabIndex = "0";
116
+ this.valueTextProvider = (item) => item;
117
+ this._valueProvider = (item) => item;
118
+ this.type = ChipTypes.filter;
119
+ this._activatedIndex = -1;
120
+
121
+ this.valueEquator = (v1, v2) => v1 === v2;
122
+
123
+ this._onKeyDown = this._onKeyDown.bind(this);
124
+ }
125
+
6
126
  render() {
7
- return html`Dw Chip Select`;
127
+ return html`
128
+ <div class="label">${this.label}</div>
129
+ ${this._renderChipList}
130
+ `;
131
+ }
132
+
133
+ get _renderChipList() {
134
+ if (!this.items || this.items.length === 0) {
135
+ return nothing;
136
+ }
137
+
138
+ return html`<div class="chip-wrapper">
139
+ ${repeat(this.items, (item, index) => {
140
+ const selected = this.#_isSelected(item);
141
+ const activated = this.#_isActivated(index);
142
+ return html`<dw-chip
143
+ .value=${this.valueTextProvider(item)}
144
+ ?selected=${selected}
145
+ ?activated=${activated}
146
+ .type=${this.type}
147
+ @click=${() => this._onChipToggle(item, selected)}
148
+ ></dw-chip>`;
149
+ })}
150
+ </div>`;
151
+ }
152
+
153
+ connectedCallback() {
154
+ super.connectedCallback();
155
+ this.#_computeValueProvider();
156
+
157
+ this.addEventListener("focusin", this._onFocusIn);
158
+ this.addEventListener("focusout", this._onFocusOut);
159
+
160
+ window.addEventListener("keydown", this._onKeyDown);
161
+ }
162
+
163
+ willUpdate(_changedProperties) {
164
+ if (_changedProperties.has("valueProvider") || _changedProperties.has("valueExpression")) {
165
+ this.#_computeValueProvider();
166
+ }
167
+ }
168
+
169
+ _onChipToggle(item, selected) {
170
+ const oItem = this._valueProvider(item);
171
+ let oValue = this.value;
172
+ const previousValue = cloneDeep(oValue);
173
+
174
+ switch (this.type) {
175
+ case ChipTypes.filter:
176
+ if (selected) {
177
+ let index = oValue.findIndex((element) => this.valueEquator(oItem, element));
178
+ if (index > -1) {
179
+ oValue.splice(index, 1);
180
+ oValue = [...oValue];
181
+ }
182
+ } else {
183
+ if (!oValue || !Array.isArray(oValue)) oValue = [];
184
+ oValue = [...oValue, oItem];
185
+ }
186
+ break;
187
+ case ChipTypes.choice:
188
+ if (selected) {
189
+ oValue = undefined;
190
+ } else {
191
+ oValue = oItem;
192
+ }
193
+ break;
194
+ }
195
+
196
+ if (!isEqual(previousValue, oValue)) {
197
+ this.value = oValue;
198
+ this.dispatchEvent(new CustomEvent("change", { detail: this.value }));
199
+ }
200
+ }
201
+
202
+ #_computeValueProvider() {
203
+ if (!this.valueProvider && !this.valueExpression) {
204
+ this._valueProvider = (item) => item;
205
+ return;
206
+ }
207
+
208
+ if (this.valueExpression) {
209
+ this._valueProvider = (item) => item[this.valueExpression];
210
+ return;
211
+ }
212
+
213
+ this._valueProvider = this.valueProvider;
214
+ }
215
+
216
+ #_isSelected(item) {
217
+ if (Array.isArray(this.value) && this.value.length > 0) {
218
+ return this.value.some((element) => this.valueEquator(this._valueProvider(item), element));
219
+ }
220
+
221
+ return this.valueEquator(this._valueProvider(item), this.value);
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
+ 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
+ ) {
247
+ e.stopPropagation();
248
+ 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
+ }
257
+ }
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
+ }
266
+ }
267
+ }
268
+ }
8
269
  }
9
270
  }
10
271
 
package/dw-chip.js CHANGED
@@ -1,16 +1,14 @@
1
1
  import { html, LitElement, css, nothing } from "lit";
2
2
  import "@dreamworld/dw-ripple";
3
3
  import "@dreamworld/dw-icon";
4
+ import { ChipTypes } from "./utils";
5
+ import { classMap } from "lit/directives/class-map.js";
6
+ import {animate} from '@lit-labs/motion';
4
7
 
5
8
  // Styles
6
9
  import * as TypographyLiterals from "@dreamworld/material-styles/typography-literals.js";
7
10
 
8
11
  const componentName = "dw-chip";
9
- const Types = {
10
- filter: "filter",
11
- choice: "choice",
12
- input: "input",
13
- };
14
12
 
15
13
  export class DwChip extends LitElement {
16
14
  static get styles() {
@@ -26,6 +24,7 @@ export class DwChip extends LitElement {
26
24
 
27
25
  overflow: hidden;
28
26
  box-sizing: border-box;
27
+ cursor: pointer;
29
28
  }
30
29
 
31
30
  :host(:not([selected])) {
@@ -48,6 +47,15 @@ export class DwChip extends LitElement {
48
47
  .value {
49
48
  ${TypographyLiterals.body2};
50
49
  }
50
+
51
+ .hide {
52
+ width: 0;
53
+ overflow: hidden;
54
+ }
55
+
56
+ .show {
57
+ width: max-content;
58
+ }
51
59
  `;
52
60
  }
53
61
 
@@ -91,7 +99,7 @@ export class DwChip extends LitElement {
91
99
  constructor() {
92
100
  super();
93
101
  this.icon = "done";
94
- this.type = Types.filter;
102
+ this.type = ChipTypes.filter;
95
103
  }
96
104
 
97
105
  render() {
@@ -108,8 +116,12 @@ export class DwChip extends LitElement {
108
116
  }
109
117
 
110
118
  get _renderLeadingIcon() {
111
- if (this.selected && this.type === Types.filter) {
112
- return html`<dw-icon name=${this.icon}></dw-icon>`;
119
+ if (this.type === ChipTypes.filter) {
120
+ return html`<dw-icon
121
+ name=${this.icon}
122
+ class="hide ${classMap({ show: this.selected })}"
123
+ ${animate()}
124
+ ></dw-icon>`;
113
125
  }
114
126
  return nothing;
115
127
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dreamworld/dw-chip-select",
3
- "version": "1.0.0-create-chip.1",
3
+ "version": "1.0.0-support-keyboard-navigation.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": {
@@ -32,7 +32,9 @@
32
32
  "@dreamworld/dw-icon": "^4.1.1",
33
33
  "@dreamworld/dw-ripple": "^3.1.3-fix-package-json.1",
34
34
  "@dreamworld/material-styles": "^3.2.2",
35
- "lit": "^2.7.0"
35
+ "@lit-labs/motion": "^1.0.3",
36
+ "lit": "^2.7.0",
37
+ "lodash-es": "^4.17.21"
36
38
  },
37
39
  "devDependencies": {
38
40
  "@open-wc/building-rollup": "^2.2.2",
package/utils.js ADDED
@@ -0,0 +1,14 @@
1
+ export const ChipTypes = {
2
+ filter: "filter",
3
+ choice: "choice",
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,
14
+ };