Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import {LitElement, html, css} from 'lit-element'; |
| 6 | import '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 | */ |
| 12 | export 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 | } |
| 70 | customElements.define('chops-filter-chips', ChopsFilterChips); |