/
index.html
64 lines (55 loc) · 2.18 KB
/
index.html
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
<!DOCTYPE html>
<html dir="ltr" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0" />
<title>Stencil Component Starter</title>
<script type="module" src="build/ui-chips.esm.js"></script>
<script nomodule src="build/ui-chips.js"></script>
</head>
<body>
<ui-chips name="sustainability" type="filter"></ui-chips>
<ui-chips name="numberOfPurchased" type="choice"></ui-chips>
</body>
<script>
// refer to the HTML element
var sustainabilityOptionsEl = document.getElementsByName('sustainability')[0];
var numberOfPurchasedOptionsEl = document.getElementsByName('numberOfPurchased')[0];
// initialize data
var sustainabilityOptions = [
{disabled: true, label: 'low', value: 1},
{label: 'medium', value: 2},
{label: 'high', value: 3}
];
var numberOfPurchasedOptions = [
{label: 1, selected: true}, // you can also preselect chip option like this
{label: 2},
{label: '2+'}
];
// populate data
sustainabilityOptionsEl.options = sustainabilityOptions;
numberOfPurchasedOptionsEl.options = numberOfPurchasedOptions;
console.log('sustainabilityOptions: ',sustainabilityOptions)
// programatically select item
sustainabilityOptionsEl.select(2)
// listen to events
document.addEventListener('uiChipsSelect', function(evt){
selectedItemEl.innerHTML = JSON.stringify(evt.detail);
});
// conditionally select a chip (remember to set selectable="false" on <Uiw-chips-group /> component)
document.addEventListener('uiChipsClick', function (evt) {
if (evt.detail.groupName !== 'numberOfPurchased') {
return
}
if (!youHaveEnoughMoneyCheckedEl.checked && evt.detail.label !== 1) {
if (evt.detail.label !== 1) {
numberOfPurchasedOptionsEl.error = 'Error: insufficient fund'
numberOfPurchasedOptionsEl.select(0)
}
} else {
numberOfPurchasedOptionsEl.error = ''
numberOfPurchasedOptionsEl.select(evt.detail.index)
}
})
</script>
</html>