@dreamworld/dw-chip-select 1.0.0-create-chip.1 → 1.0.0-integrate-chip-in-chip-select.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/demo/dw-chip-select-demo.js +37 -5
- package/demo/utils.js +55 -0
- package/dw-chip-select.js +193 -2
- package/dw-chip.js +20 -8
- package/package.json +4 -2
- package/utils.js +5 -0
|
@@ -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
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
31
|
-
|
|
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,201 @@
|
|
|
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 } 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
|
+
}
|
|
20
|
+
|
|
21
|
+
.label {
|
|
22
|
+
color: var(--mdc-theme-text-secondary-on-surface, rgba(0, 0, 0, 0.6));
|
|
23
|
+
${TypographyLiterals.body2};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
.chip-wrapper {
|
|
27
|
+
display: flex;
|
|
28
|
+
flex-wrap: wrap;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
dw-chip {
|
|
32
|
+
margin-right: 8px;
|
|
33
|
+
margin-top: 8px;
|
|
34
|
+
}
|
|
35
|
+
`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static get properties() {
|
|
39
|
+
return {
|
|
40
|
+
/**
|
|
41
|
+
* This is the type of the chip.
|
|
42
|
+
* The default value is `filter`.
|
|
43
|
+
* Possible values: `filter`, `choice`, and `input`.
|
|
44
|
+
*/
|
|
45
|
+
type: { type: String },
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Selected list item object.
|
|
49
|
+
* When the type is `filter`, the value data type is an array of objects.
|
|
50
|
+
* When the type is `choice`, the value data type is an object.
|
|
51
|
+
*/
|
|
52
|
+
value: { type: Object },
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* This is a list of selectable items.
|
|
56
|
+
*/
|
|
57
|
+
items: { type: Array },
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* This function returns a string and provides the value that represents the chip item.
|
|
61
|
+
*/
|
|
62
|
+
valueTextProvider: { type: Function },
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* This function provides the value.
|
|
66
|
+
*/
|
|
67
|
+
valueProvider: { type: Function },
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* This provides a value based on the input received from the valueExpression key.
|
|
71
|
+
*/
|
|
72
|
+
valueExpression: { type: String },
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* This sets the name attribute on the internal input.
|
|
76
|
+
* The name property should only be used for browser autofill,
|
|
77
|
+
* as webcomponent form participation does not currently consider the name attribute.
|
|
78
|
+
*/
|
|
79
|
+
name: { type: String },
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* This sets the label string and shows the label at the top of the chips.
|
|
83
|
+
*/
|
|
84
|
+
label: { type: String },
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Set this to configure custom logic to detect whether value is changed or not.
|
|
88
|
+
* Default: compares both values by strict equality (by reference) `v1 === v2`.
|
|
89
|
+
* It must return a Boolean.
|
|
90
|
+
* Function receives 2 arguments: (v1, v2). Should return `true` when both values are same otherwise `false`.
|
|
91
|
+
*/
|
|
92
|
+
valueEquator: { type: Function },
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Computed property from valueProvider and valueExpression
|
|
96
|
+
*/
|
|
97
|
+
_valueProvider: { type: Function },
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
constructor() {
|
|
102
|
+
super();
|
|
103
|
+
this.valueTextProvider = (item) => item;
|
|
104
|
+
this._valueProvider = (item) => item;
|
|
105
|
+
this.type = ChipTypes.filter;
|
|
106
|
+
|
|
107
|
+
this.valueEquator = (v1, v2) => v1 === v2;
|
|
108
|
+
}
|
|
109
|
+
|
|
6
110
|
render() {
|
|
7
|
-
return html`
|
|
111
|
+
return html`
|
|
112
|
+
<div class="label">${this.label}</div>
|
|
113
|
+
${this._renderChipList}
|
|
114
|
+
`;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
get _renderChipList() {
|
|
118
|
+
if (!this.items || this.items.length === 0) {
|
|
119
|
+
return nothing;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return html`<div class="chip-wrapper">
|
|
123
|
+
${repeat(this.items, (item, index) => {
|
|
124
|
+
const selected = this.#_isSelected(item);
|
|
125
|
+
return html`<dw-chip
|
|
126
|
+
.value=${this.valueTextProvider(item)}
|
|
127
|
+
?selected=${selected}
|
|
128
|
+
.type=${this.type}
|
|
129
|
+
@click=${() => this._onChipToggle(item, selected)}
|
|
130
|
+
></dw-chip>`;
|
|
131
|
+
})}
|
|
132
|
+
</div>`;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
connectedCallback() {
|
|
136
|
+
super.connectedCallback();
|
|
137
|
+
this.#_computeValueProvider();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
willUpdate(_changedProperties) {
|
|
141
|
+
if (_changedProperties.has("valueProvider") || _changedProperties.has("valueExpression")) {
|
|
142
|
+
this.#_computeValueProvider();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
_onChipToggle(item, selected) {
|
|
147
|
+
const oItem = this._valueProvider(item);
|
|
148
|
+
let oValue = this.value;
|
|
149
|
+
const previousValue = cloneDeep(oValue);
|
|
150
|
+
|
|
151
|
+
switch (this.type) {
|
|
152
|
+
case ChipTypes.filter:
|
|
153
|
+
if (selected) {
|
|
154
|
+
let index = oValue.findIndex((element) => this.valueEquator(oItem, element));
|
|
155
|
+
if (index > -1) {
|
|
156
|
+
oValue.splice(index, 1);
|
|
157
|
+
oValue = [...oValue];
|
|
158
|
+
}
|
|
159
|
+
} else {
|
|
160
|
+
if (!oValue || !Array.isArray(oValue)) oValue = [];
|
|
161
|
+
oValue = [...oValue, oItem];
|
|
162
|
+
}
|
|
163
|
+
break;
|
|
164
|
+
case ChipTypes.choice:
|
|
165
|
+
if (selected) {
|
|
166
|
+
oValue = undefined;
|
|
167
|
+
} else {
|
|
168
|
+
oValue = oItem;
|
|
169
|
+
}
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (!isEqual(previousValue, oValue)) {
|
|
174
|
+
this.value = oValue;
|
|
175
|
+
this.dispatchEvent(new CustomEvent("change", { detail: this.value }));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
#_computeValueProvider() {
|
|
180
|
+
if (!this.valueProvider && !this.valueExpression) {
|
|
181
|
+
this._valueProvider = (item) => item;
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (this.valueExpression) {
|
|
186
|
+
this._valueProvider = (item) => item[this.valueExpression];
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
this._valueProvider = this.valueProvider;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
#_isSelected(item) {
|
|
194
|
+
if (Array.isArray(this.value) && this.value.length > 0) {
|
|
195
|
+
return this.value.some((element) => this.valueEquator(this._valueProvider(item), element));
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return this.valueEquator(this._valueProvider(item), this.value);
|
|
8
199
|
}
|
|
9
200
|
}
|
|
10
201
|
|
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 =
|
|
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.
|
|
112
|
-
return html`<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-
|
|
3
|
+
"version": "1.0.0-integrate-chip-in-chip-select.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": "^
|
|
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",
|