UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

136 lines (135 loc) 5.56 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */ import { c as customElement } from "../../chunks/runtime.js"; import { css, html } from "lit"; import { createRef, ref } from "lit/directives/ref.js"; import { LitElement, createEvent, safeClassMap } from "@arcgis/lumina"; import { f as focusElementInGroup, b as slotChangeGetAssignedElements } from "../../chunks/dom.js"; import { u as useSetFocus } from "../../chunks/useSetFocus.js"; import { u as useInteractive } from "../../chunks/useInteractive.js"; const CSS = { container: "container" }; const styles = css`:host([disabled]){cursor:default;-webkit-user-select:none;user-select:none;opacity:var(--calcite-opacity-disabled)}:host([disabled]) *,:host([disabled]) ::slotted(*){pointer-events:none}:host{display:flex}.container{display:flex;flex-wrap:wrap;gap:var(--calcite-swatch-group-space, var(--calcite-spacing-sm))}:host([scale=s]) .container{gap:var(--calcite-swatch-group-space, var(--calcite-spacing-xs))}:host([hidden]){display:none}[hidden]{display:none}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}.interaction-container{display:contents}`; class SwatchGroup extends LitElement { constructor() { super(); this.items = []; this.slotRef = createRef(); this.focusSetter = useSetFocus()(this); this.interactiveContainer = useInteractive(this); this.disabled = false; this.scale = "m"; this.selectedItems = []; this.selectionMode = "none"; this.calciteSwatchGroupSelect = createEvent({ cancelable: false }); this.listen("calciteInternalSwatchKeyEvent", this.calciteInternalSwatchKeyEventListener); this.listen("calciteSwatchSelect", this.calciteSwatchSelectListener); this.listen("calciteInternalSwatchSelect", this.calciteInternalSwatchSelectListener); this.listen("calciteInternalSyncSelectedSwatches", this.calciteInternalSyncSelectedSwatches); } static { this.properties = { disabled: [7, {}, { reflect: true, type: Boolean }], label: 1, scale: [3, {}, { reflect: true }], selectedItems: [0, {}, { attribute: false }], selectionMode: [3, {}, { reflect: true }] }; } static { this.styles = styles; } async setFocus(options) { return this.focusSetter(() => this.el, options); } willUpdate(changes) { if (changes.has("selectionMode") && (this.hasUpdated || this.selectionMode !== "none")) { this.updateItems(); } } calciteInternalSwatchKeyEventListener(event) { if (event.composedPath().includes(this.el)) { const interactiveItems = this.items?.filter((el) => !el.disabled); switch (event.detail.key) { case "ArrowRight": focusElementInGroup(interactiveItems, event.detail.target, "next"); break; case "ArrowLeft": focusElementInGroup(interactiveItems, event.detail.target, "previous"); break; case "Home": focusElementInGroup(interactiveItems, event.detail.target, "first"); break; case "End": focusElementInGroup(interactiveItems, event.detail.target, "last"); break; } } event.stopPropagation(); } calciteSwatchSelectListener(event) { if (event.composedPath().includes(this.el)) { this.setSelectedItems(true, event.target); } event.stopPropagation(); } calciteInternalSwatchSelectListener(event) { if (event.composedPath().includes(this.el)) { this.setSelectedItems(false, event.target); } event.stopPropagation(); } calciteInternalSyncSelectedSwatches(event) { if (event.composedPath().includes(this.el)) { this.updateSelectedItems(); if (this.selectionMode === "single" && this.selectedItems.length > 1) { this.setSelectedItems(false, event.target); } } event.stopPropagation(); } updateItems(event) { const itemsFromSlot = this.slotRef.value?.assignedElements({ flatten: true }).filter((el) => el?.matches("calcite-swatch")); this.items = !event ? itemsFromSlot : slotChangeGetAssignedElements(event); if (this.items?.length < 1) { return; } this.items?.forEach((el) => { el.interactive = true; el.scale = this.scale; el.selectionMode = this.selectionMode; el.parentSwatchGroup = this.el; }); this.setSelectedItems(false); } updateSelectedItems() { this.selectedItems = this.items?.filter((el) => el.selected); } setSelectedItems(emit, elToMatch) { if (elToMatch) { this.items?.forEach((el) => { const matchingEl = elToMatch === el; switch (this.selectionMode) { case "multiple": if (matchingEl) { el.selected = !el.selected; } break; case "single": el.selected = matchingEl ? !el.selected : false; break; case "single-persist": el.selected = !!matchingEl; break; } }); } this.updateSelectedItems(); if (emit) { this.calciteSwatchGroupSelect.emit(); } } render() { const role = this.selectionMode === "none" || this.selectionMode === "multiple" ? "group" : "radiogroup"; const { disabled } = this; return this.interactiveContainer({ disabled, children: html`<div .ariaLabel=${this.label} class=${safeClassMap(CSS.container)} .role=${role}><slot @slotchange=${this.updateItems} ${ref(this.slotRef)}></slot></div>` }); } } customElement("calcite-swatch-group", SwatchGroup); export { SwatchGroup };