UNPKG

@kelvininc/ui-components

Version:
461 lines (455 loc) 21.6 kB
import { p as proxyCustomElement, H, d as createEvent, h, e as Host } from './p-D6GMjtmE.js'; import { d as EValidationState } from './p-DBphUUgi.js'; import { a as EComponentSize } from './p-BRgmvbuh.js'; import './p-BcMhjKoS.js'; import './p-BQDwJ0uF.js'; import { g as getClassMap } from './p-DKOxy79t.js'; import { b as getCssStyle } from './p-MyVVnlD9.js'; import { i as isEmpty } from './p-BZNGlO8m.js'; import { e as getFlattenSelectOptions } from './p-BEfnHAoy.js'; import { a as DEFAULT_DROPDOWN_Z_INDEX } from './p-BEvgYojK.js'; import { d as defineCustomElement$n } from './p-CsQJZTqg.js'; import { d as defineCustomElement$m } from './p-C7UGguL_.js'; import { d as defineCustomElement$l } from './p-BOySlV0g.js'; import { d as defineCustomElement$k } from './p-BAnY2f0R.js'; import { d as defineCustomElement$j } from './p-BJSKhyhi.js'; import { d as defineCustomElement$i } from './p-DVrmG2A8.js'; import { d as defineCustomElement$h } from './p-z6Xl-2s2.js'; import { d as defineCustomElement$g } from './p-BYJ5hjZN.js'; import { d as defineCustomElement$f } from './p-Bu3_3dFw.js'; import { d as defineCustomElement$e } from './p-B41PGLQm.js'; import { d as defineCustomElement$d } from './p-BMp30lyC.js'; import { d as defineCustomElement$c } from './p-Bi2u2KUW.js'; import { d as defineCustomElement$b } from './p-BIWUEm1f.js'; import { d as defineCustomElement$a } from './p-DrAjMjwv.js'; import { d as defineCustomElement$9 } from './p-6evrDshf.js'; import { d as defineCustomElement$8 } from './p-pSs-P0wB.js'; import { d as defineCustomElement$7 } from './p-CKO4h8cf.js'; import { d as defineCustomElement$6 } from './p-BGPTa9b0.js'; import { d as defineCustomElement$5 } from './p-C97qgeMO.js'; import { d as defineCustomElement$4 } from './p-DMity2tp.js'; import { d as defineCustomElement$3 } from './p-GHnCHOez.js'; import { d as defineCustomElement$2 } from './p-BuaFqlfN.js'; import { d as defineCustomElement$1 } from './p-C8hKWhkk.js'; import { m as merge } from './p-DZUGWoQz.js'; const SINGLE_SELECT_CLEAR_SELECTION_LABEL = 'Clear all'; const MINIMUM_SEARCHABLE_OPTIONS = 15; const INVALID_VALUE_ERROR = `Empty string "" is an invalid value for the dropdown, if you wish to deselect the dropdown please set the value to undefined.`; const EMPTY_STRING = ''; const getDropdownDisplayIcon = (selectedOption, flattenOptions = {}) => { if (!selectedOption || !flattenOptions[selectedOption]) { return; } const option = flattenOptions[selectedOption]; return option.icon; }; const getDropdownCustomCss = (selectedOption, flattenOptions = {}) => { if (!selectedOption || !flattenOptions[selectedOption]) { return; } const option = flattenOptions[selectedOption]; return option.customStyle; }; const buildSingleSelectOptions = (options = {}) => Object.values(options).reduce((accumulator, option) => { accumulator[option.value] = Object.assign(Object.assign({}, option), { selectable: isEmpty(option.options), options: buildSingleSelectOptions(option.options), togglable: false }); return accumulator; }, {}); const singleSelectDropdownCss = "@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}.counter{display:flex;align-items:center;color:var(--kv-neutral-4, #bebebe);font-family:var(--kv-primary-font, \"proxima-nova\", sans-serif, \"Arial\");font-size:10px;font-weight:400;font-stretch:normal;font-style:normal;line-height:15px;letter-spacing:normal;text-transform:none}"; const KvSingleSelectDropdown = /*@__PURE__*/ proxyCustomElement(class KvSingleSelectDropdown extends H { constructor() { super(); this.__registerHost(); this.optionSelected = createEvent(this, "optionSelected", 7); this.searchChange = createEvent(this, "searchChange", 7); this.clearSelection = createEvent(this, "clearSelection", 7); this.dismiss = createEvent(this, "dismiss", 7); this.clickOutside = createEvent(this, "clickOutside", 7); this.optionCreated = createEvent(this, "optionCreated", 7); this.openStateChange = createEvent(this, "openStateChange", 3); /** @inheritdoc */ this.isOpen = false; /** @inheritdoc */ this.loading = false; /** @inheritdoc */ this.required = false; /** @inheritdoc */ this.errorState = EValidationState.None; /** @inheritdoc */ this.helpText = []; /** @inheritdoc */ this.disabled = false; /** @inheritdoc */ this.inputSize = EComponentSize.Large; /** @inheritdoc */ this.customClass = ''; /** @inheritdoc */ this.clickOutsideClose = true; /** @inheritdoc */ this.actionElement = null; /** @inheritdoc */ this.options = {}; /** @inheritdoc */ this.searchable = true; /** @inheritdoc */ this.clearSelectionLabel = SINGLE_SELECT_CLEAR_SELECTION_LABEL; /** @inheritdoc */ this.minSearchOptions = MINIMUM_SEARCHABLE_OPTIONS; /** @inheritdoc */ this.shortcuts = false; /** @inheritdoc */ this.zIndex = DEFAULT_DROPDOWN_Z_INDEX; /** @inheritdoc */ this.canAddItems = false; /** @inheritdoc */ this.autoFocus = true; // eslint-disable-line @stencil-community/reserved-member-names this.onOptionSelected = (event) => { event.stopPropagation(); const { detail: selectedOptionKey } = event; this.selectOption(selectedOptionKey); this.highlightedOption = selectedOptionKey; }; this.onDismiss = () => { this.setOpenState(false); this.dismiss.emit(); }; this.onSearchChange = ({ detail: searchTerm }) => { this.setSearch(searchTerm); }; this.onOpenStateChange = ({ detail: state }) => { this.setOpenState(state); }; this.onClearSelection = () => { this.optionSelected.emit(undefined); this.clearSelection.emit(); this.calculateLabelValue(); }; this.onOptionCreated = ({ detail: newOptionKey }) => { this.optionCreated.emit(newOptionKey); }; this.selectOption = (selectedOption) => { this.optionSelected.emit(selectedOption); this.setOpenState(false); }; this.focusSearchInput = () => { setTimeout(() => { var _a; return (_a = this.selectRef) === null || _a === void 0 ? void 0 : _a.focusSearch(); }); }; this.setOpenState = (state) => { if (state) { if (this.autoFocus) { this.focusSearchInput(); } } else { this.setSearch(''); this.closeCreatePopup(); } this.highlightedOption = undefined; this.isOpen = state; this.openStateChange.emit(state); this.clearHighlightedOption(); }; this.setSearch = (searchTerm) => { this._searchValue = searchTerm; this.searchChange.emit(searchTerm); }; this.getInputConfig = () => { var _a, _b; return merge({}, this.inputConfig, { label: this.label, value: this._selectionDisplayValue, valuePrefix: this.displayPrefix, loading: this.loading, inputDisabled: this.disabled, inputRequired: this.required, placeholder: this.placeholder, state: this.errorState, helpText: this.helpText, size: this.inputSize, badge: this.badge, isDirty: this.selectedOption && this.options && ((_a = this.options[this.selectedOption]) === null || _a === void 0 ? void 0 : _a.isDirty), icon: (_b = getDropdownDisplayIcon(this.selectedOption, this.selectOptions.flatten)) !== null && _b !== void 0 ? _b : this.icon, customStyle: getDropdownCustomCss(this.selectedOption, this.selectOptions.flatten) }); }; this.clearHighlightedOption = () => { var _a; (_a = this.selectRef) === null || _a === void 0 ? void 0 : _a.clearHighlightedOption(); }; this.closeCreatePopup = () => { var _a; (_a = this.selectRef) === null || _a === void 0 ? void 0 : _a.closeCreatePopup(); }; this.calculateLabelValue = () => { var _a, _b; if ((_a = this.displayValue) === null || _a === void 0 ? void 0 : _a.length) { this._selectionDisplayValue = this.displayValue; return; } if (this.selectedOption && ((_b = this.selectOptions) === null || _b === void 0 ? void 0 : _b.flatten[this.selectedOption])) { this._selectionDisplayValue = this.selectOptions.flatten[this.selectedOption].label; return; } this._selectionDisplayValue = undefined; }; this.validateSelectedOptionValue = () => { if (this.selectedOption === EMPTY_STRING) { throw new Error(INVALID_VALUE_ERROR); } }; this.buildSelectionOptions = () => { const selectOptions = buildSingleSelectOptions(this.options); const filteredSelectOptions = this.filteredOptions ? buildSingleSelectOptions(this.filteredOptions) : undefined; const selectFlattenOptions = getFlattenSelectOptions(selectOptions); this.selectOptions = { total: selectOptions, filtered: filteredSelectOptions, flatten: selectFlattenOptions }; }; } optionsChangeHandler() { this.buildSelectionOptions(); this.calculateLabelValue(); } selectedOptionChangeHandler() { this.validateSelectedOptionValue(); this.calculateLabelValue(); } displayValueChangeHandler() { this.calculateLabelValue(); } filterOptionsChangeHandler() { this.buildSelectionOptions(); } /** Focuses the search text field */ async focusSearch() { var _a; (_a = this.selectRef) === null || _a === void 0 ? void 0 : _a.focusSearch(); } componentWillLoad() { this.validateSelectedOptionValue(); this.buildSelectionOptions(); this.calculateLabelValue(); } 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; } get selectedOptions() { return this.selectedOption ? { [this.selectedOption]: true } : {}; } connectedCallback() { if (this.autoFocus) { this.focusSearchInput(); } } render() { return (h(Host, { key: '42468b7e01463d2164da2196c8777335a00e06d1' }, h("kv-dropdown", { key: 'e1448bb20d328d2ed1bf0e0a3733168d0c935b68', 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: '8fb643e464da9639138375e164805b4f1525fdc2', name: "right-slot", slot: "right-slot" }), h("slot", { key: '4a9e635dbfc58fe53aa26d817868ff2553b7efab', name: "left-slot", slot: "left-slot" }), h("slot", { key: '34cc6d055912da304b36cf4a2a4ff8959211157d', name: "dropdown-action", slot: "dropdown-action" }), h("div", { key: '7c8ab415460a4fa10bc20c3b713a7c84c6ea9dae', class: Object.assign(Object.assign({}, getClassMap(this.customClass)), { 'single-select-dropdown-slot': true }) }, h("kv-select-multi-options", { key: '16de85ee0268dc5e5e3c4961bfbb4e06926135d9', ref: element => (this.selectRef = element), options: this.selectOptions.total, filteredOptions: this.selectOptions.filtered, 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, createInputPlaceholder: this.createInputPlaceholder, createOptionPlaceholder: this.createOptionPlaceholder, 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, onOptionSelected: this.onOptionSelected, onOptionCreated: this.onOptionCreated, onDismiss: this.onDismiss, canAddItems: this.canAddItems, exportparts: "select" }, h("slot", { key: '0a4e95bba41dafc150c41b8edf4f7fd45a4a56db', name: "create-new-option", slot: "create-new-option" }), h("slot", { key: 'f5740278d58167ef91ca55df464d74e23d48541b', name: "select-header-actions", slot: "select-header-actions" }), h("slot", { key: 'b95856d3090d020ab615a88cd72e3d5089b7e595', name: "select-header-label", slot: "select-header-label" }), h("slot", { key: '2978b448b588189e4d08c7b781e35c000e378ad0', name: "no-data-available", slot: "no-data-available" }), h("slot", { key: '1dacdab7c23ec33191ed41e63b76f6410dc14ff3', name: "no-results-found", slot: "no-results-found" })), h("slot", { key: 'a06c50835184723a155a2520a308119e581db587' }))))); } get el() { return this; } static get watchers() { return { "options": ["optionsChangeHandler"], "selectedOption": ["selectedOptionChangeHandler"], "displayValue": ["displayValueChangeHandler"], "filteredOptions": ["filterOptionsChangeHandler"] }; } static get style() { return singleSelectDropdownCss; } }, [4, "kv-single-select-dropdown", { "placeholder": [513], "isOpen": [1540, "is-open"], "loading": [516], "icon": [513], "required": [516], "label": [513], "displayValue": [513, "display-value"], "displayPrefix": [513, "display-prefix"], "badge": [513], "errorState": [513, "error-state"], "helpText": [513, "help-text"], "disabled": [516], "inputSize": [513, "input-size"], "dropdownOptions": [16, "dropdown-options"], "customClass": [513, "custom-class"], "clickOutsideClose": [516, "click-outside-close"], "actionElement": [16, "action-element"], "options": [16], "selectedOption": [513, "selected-option"], "filteredOptions": [16, "filtered-options"], "noDataAvailableConfig": [16, "no-data-available-config"], "noResultsFoundConfig": [16, "no-results-found-config"], "searchable": [516], "searchPlaceholder": [513, "search-placeholder"], "selectionClearable": [516, "selection-clearable"], "clearSelectionLabel": [513, "clear-selection-label"], "minHeight": [513, "min-height"], "maxHeight": [513, "max-height"], "minWidth": [513, "min-width"], "maxWidth": [513, "max-width"], "selectionAll": [516, "selection-all"], "selectAllLabel": [513, "select-all-label"], "counter": [516], "minSearchOptions": [514, "min-search-options"], "shortcuts": [516], "zIndex": [2, "z-index"], "canAddItems": [516, "can-add-items"], "createInputPlaceholder": [513, "create-input-placeholder"], "createOptionPlaceholder": [513, "create-option-placeholder"], "inputConfig": [16, "input-config"], "autoFocus": [516, "auto-focus"], "_searchValue": [32], "_selectionDisplayValue": [32], "selectOptions": [32], "highlightedOption": [32], "focusSearch": [64] }, undefined, { "options": ["optionsChangeHandler"], "selectedOption": ["selectedOptionChangeHandler"], "displayValue": ["displayValueChangeHandler"], "filteredOptions": ["filterOptionsChangeHandler"] }]); function defineCustomElement() { if (typeof customElements === "undefined") { return; } const components = ["kv-single-select-dropdown", "kv-action-button", "kv-action-button-icon", "kv-badge", "kv-checkbox", "kv-dirty-dot", "kv-dropdown", "kv-dropdown-base", "kv-form-help-text", "kv-form-label", "kv-icon", "kv-illustration", "kv-illustration-message", "kv-portal", "kv-search", "kv-select", "kv-select-create-option", "kv-select-multi-options", "kv-select-option", "kv-select-shortcuts-label", "kv-text-field", "kv-tooltip", "kv-tooltip-text", "kv-virtualized-list"]; components.forEach(tagName => { switch (tagName) { case "kv-single-select-dropdown": if (!customElements.get(tagName)) { customElements.define(tagName, KvSingleSelectDropdown); } break; case "kv-action-button": if (!customElements.get(tagName)) { defineCustomElement$n(); } break; case "kv-action-button-icon": if (!customElements.get(tagName)) { defineCustomElement$m(); } break; case "kv-badge": if (!customElements.get(tagName)) { defineCustomElement$l(); } break; case "kv-checkbox": if (!customElements.get(tagName)) { defineCustomElement$k(); } break; case "kv-dirty-dot": if (!customElements.get(tagName)) { defineCustomElement$j(); } break; case "kv-dropdown": if (!customElements.get(tagName)) { defineCustomElement$i(); } break; case "kv-dropdown-base": if (!customElements.get(tagName)) { defineCustomElement$h(); } break; case "kv-form-help-text": if (!customElements.get(tagName)) { defineCustomElement$g(); } break; case "kv-form-label": if (!customElements.get(tagName)) { defineCustomElement$f(); } break; case "kv-icon": if (!customElements.get(tagName)) { defineCustomElement$e(); } break; case "kv-illustration": if (!customElements.get(tagName)) { defineCustomElement$d(); } break; case "kv-illustration-message": if (!customElements.get(tagName)) { defineCustomElement$c(); } break; case "kv-portal": if (!customElements.get(tagName)) { defineCustomElement$b(); } break; case "kv-search": if (!customElements.get(tagName)) { defineCustomElement$a(); } break; case "kv-select": if (!customElements.get(tagName)) { defineCustomElement$9(); } break; case "kv-select-create-option": if (!customElements.get(tagName)) { defineCustomElement$8(); } break; case "kv-select-multi-options": if (!customElements.get(tagName)) { defineCustomElement$7(); } break; case "kv-select-option": if (!customElements.get(tagName)) { defineCustomElement$6(); } break; case "kv-select-shortcuts-label": if (!customElements.get(tagName)) { defineCustomElement$5(); } break; case "kv-text-field": if (!customElements.get(tagName)) { defineCustomElement$4(); } break; case "kv-tooltip": if (!customElements.get(tagName)) { defineCustomElement$3(); } break; case "kv-tooltip-text": if (!customElements.get(tagName)) { defineCustomElement$2(); } break; case "kv-virtualized-list": if (!customElements.get(tagName)) { defineCustomElement$1(); } break; } }); } defineCustomElement(); export { KvSingleSelectDropdown as K, defineCustomElement as d };