UNPKG

@kelvininc/ui-components

Version:
223 lines (217 loc) 10.6 kB
import { r as registerInstance, c as createEvent, h, H as Host, g as getElement } from './index-D-JVwta2.js'; import { g as getSelectedSelectableOptions } from './select.helper-D_nHTFPl.js'; import { a as EComponentSize } from './components-DzZLIMy0.js'; import './lib-config-DwRzddFC.js'; import './action-button.types-DVds6a5Z.js'; import './absolute-time-picker-dropdown.types-CojoW2Y2.js'; import './icon.types-SVedE_O8.js'; import './summary-card.types-BcMhjKoS.js'; import './toaster.types-vhHhaF4Q.js'; import './tree-item.types-CBuzk8fR.js'; import './tag-alarm.types-DHk26cGe.js'; import './wizard.types-7ioMFMb5.js'; import { a as getCssStyle } from './utils-Cx1GRYZc.js'; import { g as getClassMap } from './css-class.helper-DKOxy79t.js'; import { a as DEFAULT_DROPDOWN_Z_INDEX } from './config-CJMopqu5.js'; import { m as merge } from './merge-uvn7Q6Uf.js'; import './string.helper-BMAMF_sW.js'; import './isEmpty-BAGi1PqI.js'; import './_Set-B7Zkvu4X.js'; import './_Map-B6Xd0L4K.js'; import './isObject-Dkd2PDJ-.js'; import './isNil-DjSNdVAB.js'; import './_baseMerge-CgbDsiSj.js'; import './identity-CK4jS9_E.js'; import './_MapCache-nTWrGhJJ.js'; const MINIMUM_SEARCHABLE_OPTIONS = 15; const BADGE_MINIMUM_OPTIONS = 2; const getDropdownDisplayValue = (options = {}, selectedOptions = {}) => { const [firstOption] = Object.values(getSelectedSelectableOptions(options, selectedOptions)); return firstOption === null || firstOption === void 0 ? void 0 : firstOption.label; }; const getBadgeLabelValue = (selectedOptions = {}) => { const selectedOptionsCount = Object.keys(selectedOptions).filter(key => selectedOptions[key]).length; if (selectedOptionsCount < BADGE_MINIMUM_OPTIONS) return; return `+${selectedOptionsCount - 1}`; }; const multiSelectDropdownCss = "@property --rotation{syntax:\"<angle>\";initial-value:0deg;inherits:false}@keyframes rotate-border{to{--rotation:360deg}}:host{--dropdown-max-height:400px;--dropdown-min-height:auto;--dropdown-max-width:auto;--dropdown-min-width:max-content}kv-dropdown[disabled=true]{pointer-events:none}"; const KvMultiSelectDropdown = class { constructor(hostRef) { registerInstance(this, hostRef); this.optionsSelected = createEvent(this, "optionsSelected", 7); this.searchChange = createEvent(this, "searchChange", 7); this.clearSelection = createEvent(this, "clearSelection", 7); this.selectAll = createEvent(this, "selectAll", 7); this.dismiss = createEvent(this, "dismiss", 7); this.clickOutside = createEvent(this, "clickOutside", 7); this.openStateChange = createEvent(this, "openStateChange", 3); /** @inheritdoc */ this.loading = false; /** @inheritdoc */ this.searchable = true; /** @inheritdoc */ this.selectionClearable = true; /** @inheritdoc */ this.helpText = []; /** @inheritdoc */ this.options = {}; /** @inheritdoc */ this.selectedOptions = {}; /** @inheritdoc */ this.inputSize = EComponentSize.Large; /** @inheritdoc */ this.customClass = ''; /** @inheritdoc */ this.selectionAll = true; /** @inheritdoc */ this.counter = true; /** @inheritdoc */ this.minSearchOptions = MINIMUM_SEARCHABLE_OPTIONS; /** @inheritdoc */ this.shortcuts = false; /** @inheritdoc */ this.clickOutsideClose = true; /** @inheritdoc */ this.actionElement = null; /** @inheritdoc */ this.zIndex = DEFAULT_DROPDOWN_Z_INDEX; /** @inheritdoc */ this.autoFocus = true; // eslint-disable-line @stencil-community/reserved-member-names this._badgeLabel = this.badge; this._isOpen = false; this.onOptionsSelected = ({ detail: newOptions }) => { this.optionsSelected.emit(newOptions); }; this.onSearchChange = ({ detail: searchValue }) => { this.setSearch(searchValue); }; this.onClearSelection = (event) => { event.stopPropagation(); this.clearSelection.emit(); this.calculateLabelValue(); }; this.onSelectAll = (event) => { event.stopPropagation(); this.selectAll.emit(); this.calculateLabelValue(); }; this.onDismiss = () => { this.setOpenState(false); this.dismiss.emit(); }; this.onOpenStateChange = ({ detail: state }) => { this.setOpenState(state); }; this.setOpenState = (state) => { if (state) { if (this.autoFocus) { this.focusSearchInput(); } } else { this.setSearch(''); } this.clearHighlightedOption(); this._isOpen = state; }; this.setSearch = (searchTerm) => { this._searchValue = searchTerm; this.searchChange.emit(searchTerm); }; this.clearHighlightedOption = () => { var _a; (_a = this.selectRef) === null || _a === void 0 ? void 0 : _a.clearHighlightedOption(); }; this.calculateLabelValue = () => { var _a; if ((_a = this.displayValue) === null || _a === void 0 ? void 0 : _a.length) { this._selectionDisplayValue = this.displayValue; } else { this._selectionDisplayValue = getDropdownDisplayValue(this.options, this.selectedOptions); } }; this.calculateBadgeValue = () => { var _a; if ((_a = this.badge) === null || _a === void 0 ? void 0 : _a.length) { this._badgeLabel = this.badge; } else { this._badgeLabel = getBadgeLabelValue(this.selectedOptions); } }; this.focusSearchInput = () => { setTimeout(() => { var _a; return (_a = this.selectRef) === null || _a === void 0 ? void 0 : _a.focusSearch(); }); }; } badgeChangeHandler() { this.calculateBadgeValue(); } labelValueHandler() { this.calculateLabelValue(); this.calculateBadgeValue(); } /** Focuses the search text field */ async focusSearch() { var _a; (_a = this.selectRef) === null || _a === void 0 ? void 0 : _a.focusSearch(); } componentWillLoad() { this.calculateLabelValue(); this.calculateBadgeValue(); } getMaxHeight() { var _a; const maxHeight = getCssStyle(this.el, '--dropdown-max-height'); return (_a = this.maxHeight) !== null && _a !== void 0 ? _a : maxHeight; } getMinHeight() { var _a; const minHeight = getCssStyle(this.el, '--dropdown-min-height'); return (_a = this.minHeight) !== null && _a !== void 0 ? _a : minHeight; } getMaxWidth() { var _a; const maxWidth = getCssStyle(this.el, '--dropdown-max-width'); return (_a = this.maxWidth) !== null && _a !== void 0 ? _a : maxWidth; } getMinWidth() { var _a; const minWidth = getCssStyle(this.el, '--dropdown-min-width'); return (_a = this.minWidth) !== null && _a !== void 0 ? _a : minWidth; } getInputConfig() { return merge({}, this.inputConfig, { label: this.label, value: this._selectionDisplayValue, valuePrefix: this.displayPrefix, loading: this.loading, icon: this.icon, inputDisabled: this.disabled, inputRequired: this.required, placeholder: this.placeholder, state: this.errorState, helpText: this.helpText, size: this.inputSize, badge: this._badgeLabel }); } connectedCallback() { if (this.autoFocus) { this.focusSearchInput(); } } render() { return (h(Host, { key: '6d810794e50de49938c3a5009607f79e3e3d34b8' }, h("kv-dropdown", { key: '722084e194cf88703a001d133bc72cc65cdc33c3', inputConfig: this.getInputConfig(), isOpen: this._isOpen, onOpenStateChange: this.onOpenStateChange, disabled: this.disabled, options: this.dropdownOptions, clickOutsideClose: this.clickOutsideClose, actionElement: this.actionElement, zIndex: this.zIndex }, h("slot", { key: '059969a5dbd7aafd786f8c6ec56bc29c988a6737', name: "dropdown-action", slot: "dropdown-action" }), h("div", { key: '84dd56799b74a3a490e57a2fcb1e7bce80f8cfef', class: getClassMap(this.customClass) }, h("kv-select-multi-options", { key: 'bd6464ecea791330a3d05258465a5d44fcb949ae', ref: element => (this.selectRef = element), options: this.options, filteredOptions: this.filteredOptions, selectedOptions: this.selectedOptions, noDataAvailableConfig: this.noDataAvailableConfig, noResultsFoundConfig: this.noResultsFoundConfig, searchable: this.searchable, minSearchOptions: this.minSearchOptions, searchValue: this._searchValue, selectionClearable: this.selectionClearable, clearSelectionLabel: this.clearSelectionLabel, selectionAll: this.selectionAll, selectAllLabel: this.selectAllLabel, searchPlaceholder: this.searchPlaceholder, maxHeight: this.getMaxHeight(), minHeight: this.getMinHeight(), maxWidth: this.getMaxWidth(), minWidth: this.getMinWidth(), counter: this.counter, shortcuts: this._isOpen && this.shortcuts, onSearchChange: this.onSearchChange, onClearSelection: this.onClearSelection, onOptionsSelected: this.onOptionsSelected, onSelectAll: this.onSelectAll, onDismiss: this.onDismiss, exportparts: "select" }, h("slot", { key: 'd9f0ebb156816f046db41d173b142da538ee988a', name: "select-header-actions", slot: "select-header-actions" }), h("slot", { key: '03db9174c99e5e1b7ae5532829279a1c643c526b', name: "select-header-label", slot: "select-header-label" }), h("slot", { key: '2d6fa524a65126a9f8e1823d621922ef889e1630', name: "no-data-available", slot: "no-data-available" }), h("slot", { key: '35e7840a53f248ace292b2c1da03812155fd612d', name: "no-results-found", slot: "no-results-found" })))))); } get el() { return getElement(this); } static get watchers() { return { "badge": ["badgeChangeHandler"], "options": ["labelValueHandler"], "selectedOptions": ["labelValueHandler"], "displayValue": ["labelValueHandler"] }; } }; KvMultiSelectDropdown.style = multiSelectDropdownCss; export { KvMultiSelectDropdown as kv_multi_select_dropdown };