UNPKG

@kelvininc/ui-components

Version:
216 lines (210 loc) 10.5 kB
import { r as registerInstance, c as createEvent, h, H as Host, g as getElement } from './index-BOTigrTZ.js'; import { b as getSelectedSelectableOptions } from './select.helper-Be3m0bpR.js'; import { c as EComponentSize } from './wizard.types-COrzvkrr.js'; import { a as getCssStyle } from './utils-Douu2-ri.js'; import { a as getClassMap } from './css-class.helper-DCOsOAOy.js'; import { a as DEFAULT_DROPDOWN_Z_INDEX } from './config-DfTrc9q6.js'; import { m as merge } from './merge-D2bAicdu.js'; import './string.helper-rZ6JCPsX.js'; import './isEmpty-DYR6-7ab.js'; import './isObject-Dkd2PDJ-.js'; import './isNil-DjSNdVAB.js'; import './_baseMerge-ECDD_Loo.js'; import './identity-CK4jS9_E.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}}kv-dropdown-base:not(.hydrated)>[slot=list]{display:none}: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.showShortcuts = 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 /** @inheritdoc */ this.hideBadge = false; 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, hideBadge: this.hideBadge }); } connectedCallback() { if (this.autoFocus) { this.focusSearchInput(); } } render() { return (h(Host, { key: '635437fbd5b3d05180802259bec9f21acf43baa7' }, h("kv-dropdown", { key: '038128f47273668a5eed7bf065bbe43fd9c0d7c2', 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: 'df7cd0ed16946f0afd1f0b8ac8b307f1e2d99573', name: "dropdown-action", slot: "dropdown-action" }), h("div", { key: '5e7b7026e86157714bef7bf6a23ed6c9b9c3342c', class: getClassMap(this.customClass) }, h("kv-select-multi-options", { key: 'd5a54b9c37e501dceb6583c57af16e55ef3cb0cb', 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, showShortcuts: this.showShortcuts, maxSelectable: this.maxSelectable, onSearchChange: this.onSearchChange, onClearSelection: this.onClearSelection, onOptionsSelected: this.onOptionsSelected, onSelectAll: this.onSelectAll, onDismiss: this.onDismiss, exportparts: "select" }, h("slot", { key: '7c5b61e9bcb287981b8c55bb3a8a5ba8fcfacc25', name: "select-header-actions", slot: "select-header-actions" }), h("slot", { key: '5d963c20803edf6a0fa679d6110fd76f02cca4e0', name: "select-header-label", slot: "select-header-label" }), h("slot", { key: '96ff302ecab3e6eff537dbe5b06405b868c00c7e', name: "no-data-available", slot: "no-data-available" }), h("slot", { key: '5b62441d40392e8f444f213cdcc36de8435c2330', name: "no-results-found", slot: "no-results-found" }), h("slot", { key: '771dd2cfb0ed95f77f96530ef221f4d86db0cb8f', name: "select-footer", slot: "select-footer" })))))); } 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 };