blob: 3f9193a052d5b3f03f01e2ea3431229ede1a28c5 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001// Copyright 2020 The Chromium Authors
Copybara854996b2021-09-07 19:36:02 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import {LitElement, html, css} from 'lit-element';
6import 'elements/chops/chops-chip/chops-chip.js';
7
8/**
9 * `<chops-filter-chips>` displays a set of filter chips.
10 * https://material.io/components/chips/#filter-chips
11 */
12export class ChopsFilterChips extends LitElement {
13 /** @override */
14 static get properties() {
15 return {
16 options: {type: Array},
17 selected: {type: Object},
18 };
19 }
20
21 /** @override */
22 constructor() {
23 super();
24 /** @type {Array<string>} */
25 this.options = [];
26 /** @type {Object<string, boolean>} */
27 this.selected = {};
28 }
29
30 /** @override */
31 static get styles() {
32 return css`
33 :host {
34 display: inline-flex;
35 }
36 `;
37 }
38
39 /** @override */
40 render() {
41 return html`${this.options.map((option) => this._renderChip(option))}`;
42 }
43
44 /**
45 * Render a single chip.
46 * @param {string} option The text on the chip.
47 * @return {TemplateResult}
48 */
49 _renderChip(option) {
50 return html`
51 <chops-chip
52 @click=${this.select.bind(this, option)}
53 class=${this.selected[option] ? 'selected' : ''}
54 .thumbnail=${this.selected[option] ? 'check' : ''}>
55 ${option}
56 </chops-chip>
57 `;
58 }
59
60 /**
61 * Selects or unselects an option.
62 * @param {string} option The option to select or unselect.
63 * @fires Event#change
64 */
65 select(option) {
66 this.selected = {...this.selected, [option]: !this.selected[option]};
67 this.dispatchEvent(new Event('change'));
68 }
69}
70customElements.define('chops-filter-chips', ChopsFilterChips);