-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathh2-checkbox-group.js
198 lines (174 loc) · 4.65 KB
/
h2-checkbox-group.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
`h2-checkbox-group`
Example:
```html
<h2-checkbox-group label="材料" value="0" items="{{items}}"></h2-checkbox-group>
<script>
items = [
{label: '薄膜', value: 0},
{label: '纤维', value: 1},
{label: '塑料', value: 2},
{label: '其它', value: 3}
];
</script>
```
## Styling
The following custom properties and mixins are available for styling:
|Custom property | Description | Default|
|----------------|-------------|----------|
|`--h2-checkbox-group-label` | Mixin applied to the label of checkbox | {}
|`--h2-checkbox-group-checked-color` | Mixin applied to color of the checkbox when it is checked | #0099FF
*/
import {html, PolymerElement} from "@polymer/polymer";
import {mixinBehaviors} from "@polymer/polymer/lib/legacy/class";
import {BaseBehavior} from "./behaviors/base-behavior";
import './behaviors/h2-elements-shared-styles.js';
import '@polymer/paper-checkbox';
import './h2-button';
/**
* @customElement
* @polymer
* @demo demo/h2-checkbox-group/index.html
*/
class H2CheckboxGroup extends mixinBehaviors(BaseBehavior, PolymerElement) {
static get template() {
return html`
<style include="h2-elements-shared-styles">
:host {
display: flex;
flex-wrap: nowrap;
position: relative;
line-height: 34px;
}
#checkbox-container {
position: relative;
}
.checkbox-item {
margin-left: 6px;
--paper-checkbox-checked-color: var(--h2-checkbox-group-checked-color, #0099FF);
}
</style>
[[label]]
<div id="checkbox-container">
<template is="dom-repeat" items="[[ _items ]]">
<paper-checkbox class="checkbox-item" checked="{{ item.checked }}" on-change="__checkedChangeHandler" value="[[ getValueByKey(item, attrForValue) ]]">
[[ getValueByKey(item, attrForLabel) ]]
</paper-checkbox>
</template>
<h2-button on-click="handlerClickTest" disabled>button</h2-button>
<div class="mask"></div>
</div>
`;
}
handlerClickTest(e) {
console.log(e);
}
static get properties() {
return {
/**
* The label of the Checkbox
*
* @attribute label
* @type {string}
*/
label: {
type: String
},
/**
* Candidates of the checkbox group.
* @attribute items
* @type {array}
*/
items: {
type: Array
},
_items: {
type: Array,
computed: '__computedInnerItems(items, value)'
},
/**
* The selected value of checkbox group.
* @attribute value
* @type {string}
*/
value: {
type: String,
notify: true
},
/**
* The selected value items of checkbox group.
* @attribute value
* @type {array}
*/
selectedValues: {
type: Array,
notify: true
},
/**
* Attribute name for value.
* @type {string}
* @default 'value'
*/
attrForValue: {
type: String,
value: "value"
},
/**
* Attribute name for label.
* @type {string}
* @default 'label'
*/
attrForLabel: {
type: String,
value: "label"
},
/**
* Set to true if the selection is required.
* @type {boolean}
* @default false
*/
required: {
type: Boolean,
value: false
}
};
}
static get is() {
return "h2-checkbox-group";
}
static get observers() {
return [
'__valueChanged(value, items)'
];
}
__computedInnerItems(items = [], value = "") {
const values = this.__parseValues(value);
return items.map(item =>
Object.assign({}, item, {checked: values.some(val => val === item[this.attrForValue] + '')}));
}
__checkedChangeHandler() {
const selectValues = this._items.filter(item => !!item.checked).map(item => item[this.attrForValue]);
this.value = selectValues.length > 0 ? selectValues.join(',') : undefined;
}
/**
* @private
*/
__valueChanged(value = "", items = []) {
const values = this.__parseValues(value);
this.selectedValues = items.filter(item => values.some(val => val === item[this.attrForValue] + ''));
}
/**
* @private
*/
__parseValues(value = "") {
return value.split(',').map(val => val.trim());
}
/**
* Validate, true if the select is set to be required and this.selectedValues.length > 0, or else false.
* @returns {boolean}
*/
validate() {
return this.required ? (this.selectedValues && this.selectedValues.length > 0) : true;
}
}
window.customElements.define(H2CheckboxGroup.is, H2CheckboxGroup);