@kelvininc/ui-components
Version:
Kelvin UI Components
322 lines (311 loc) • 19.4 kB
JavaScript
'use strict';
var index = require('./index-rNNWWpit.js');
var wizard_types = require('./wizard.types-C9Yhv1tt.js');
var cssClass_helper = require('./css-class.helper-h-CvPc5n.js');
var utils = require('./utils-Ck_7CWOx.js');
var isEmpty = require('./isEmpty-CqcsgK-A.js');
var select_helper = require('./select.helper-CAIqBeiV.js');
var config = require('./config-dyyw7PBD.js');
var merge = require('./merge-DKeNMXHS.js');
require('./isNil-B-fGcnNC.js');
require('./isObject-COPdF2vE.js');
require('./string.helper-DxzCWS0Y.js');
require('./_baseMerge-Bq8HwF_X.js');
require('./identity-Dz5mxHaJ.js');
const inputWrapperCss = "@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{display:block}.input-container{background-color:var(--transparent);padding:0 var(--spacing-2xl);height:32px;display:flex;align-items:center}.input-container kv-icon{visibility:hidden}.input-container--content-hidden:hover{background-color:var(--background-interactive-slider-list-hover);cursor:pointer}.input-container--content-hidden:hover kv-icon{--icon-color:var(--icon-surface-neutral-primary);visibility:visible}.input-container .label-container{font-family:Proxima Nova;font-weight:400;font-size:12px;line-height:16px;letter-spacing:0;display:flex;align-items:center;justify-content:space-between;width:100%;color:var(--text-container-neutral-default)}.input-container .slot-container{width:100%}";
const KvInputWrapper = class {
constructor(hostRef) {
index.registerInstance(this, hostRef);
this.contentClick = index.createEvent(this, "contentClick", 7);
/** @inheritdoc */
this.contentHidden = false;
/** @inheritdoc */
this.icon = wizard_types.EIconName.Edit;
this.handleContentClick = () => {
if (!this.contentVisible && !this.contentHidden) {
this.contentClick.emit(this.contentVisible);
}
};
}
render() {
return (index.h(index.Host, { key: '80b3abbba9ee9bccd764818b64922b10a053ebb0' }, index.h("div", { key: '180134974d4de7dabbb1096f7cf1ae718da8d011', class: {
'input-container': true,
'input-container--content-hidden': !this.contentVisible && !this.contentHidden
}, onClick: this.handleContentClick }, this.contentVisible && !this.contentHidden ? (index.h("div", { class: "slot-container" }, index.h("slot", null))) : (index.h("div", { class: "label-container" }, index.h("div", { class: "label" }, this.label), index.h("kv-icon", { name: this.icon }))))));
}
};
KvInputWrapper.style = inputWrapperCss;
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.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}}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}.counter{display:flex;align-items:center;color:var(--text-container-neutral-subtle);font-family:Proxima Nova;font-weight:400;font-size:10px;line-height:14px;letter-spacing:0}";
const KvSingleSelectDropdown = class {
constructor(hostRef) {
index.registerInstance(this, hostRef);
this.optionSelected = index.createEvent(this, "optionSelected", 7);
this.searchChange = index.createEvent(this, "searchChange", 7);
this.clearSelection = index.createEvent(this, "clearSelection", 7);
this.dismiss = index.createEvent(this, "dismiss", 7);
this.clickOutside = index.createEvent(this, "clickOutside", 7);
this.optionCreated = index.createEvent(this, "optionCreated", 7);
this.openStateChange = index.createEvent(this, "openStateChange", 3);
/** @inheritdoc */
this.isOpen = false;
/** @inheritdoc */
this.loading = false;
/** @inheritdoc */
this.required = false;
/** @inheritdoc */
this.errorState = wizard_types.EValidationState.None;
/** @inheritdoc */
this.helpText = [];
/** @inheritdoc */
this.disabled = false;
/** @inheritdoc */
this.inputSize = wizard_types.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.showShortcuts = false;
/** @inheritdoc */
this.zIndex = config.DEFAULT_DROPDOWN_Z_INDEX;
/** @inheritdoc */
this.canAddItems = false;
/** @inheritdoc */
this.autoFocus = true; // eslint-disable-line @stencil-community/reserved-member-names
this.rebuildScheduled = false;
this.scheduleRebuild = () => {
if (this.rebuildScheduled)
return;
this.rebuildScheduled = true;
queueMicrotask(() => {
this.rebuildScheduled = false;
this.buildSelectionOptions();
this.calculateLabelValue();
});
};
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.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 = select_helper.getFlattenSelectOptions(selectOptions);
this.selectOptions = {
total: selectOptions,
filtered: filteredSelectOptions,
flatten: selectFlattenOptions
};
};
}
optionsChangeHandler() {
this.scheduleRebuild();
}
selectedOptionChangeHandler() {
this.validateSelectedOptionValue();
this.calculateLabelValue();
}
displayValueChangeHandler() {
this.calculateLabelValue();
}
filterOptionsChangeHandler() {
this.scheduleRebuild();
}
/** 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 = utils.getCssStyle(this.el, '--dropdown-max-height');
return (_a = this.maxHeight) !== null && _a !== void 0 ? _a : maxHeight;
}
getMinHeight() {
var _a;
const minHeight = utils.getCssStyle(this.el, '--dropdown-min-height');
return (_a = this.minHeight) !== null && _a !== void 0 ? _a : minHeight;
}
getMaxWidth() {
var _a;
const maxWidth = utils.getCssStyle(this.el, '--dropdown-max-width');
return (_a = this.maxWidth) !== null && _a !== void 0 ? _a : maxWidth;
}
getMinWidth() {
var _a;
const minWidth = utils.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 (index.h(index.Host, { key: '4b7590ba6e30ba2d3650f012e0df5e1cd69b8e65' }, index.h("kv-dropdown", { key: '961c15e0dff4d88d69de41912048de270903ee98', inputConfig: this.getInputConfig(), isOpen: this.isOpen, onOpenStateChange: this.onOpenStateChange, disabled: this.disabled, options: this.dropdownOptions, clickOutsideClose: this.clickOutsideClose, actionElement: this.actionElement, zIndex: this.zIndex }, index.h("slot", { key: '6144ec1787cc225746b824e3ca8fec10ad565233', name: "right-slot", slot: "right-slot" }), index.h("slot", { key: 'a4433167c06ec6c70b815777a42a33b64897106b', name: "left-slot", slot: "left-slot" }), index.h("slot", { key: 'cad85f7a8e681778945b8f76b3bd0f7f99b13bfb', name: "dropdown-action", slot: "dropdown-action" }), index.h("div", { key: '1459e958549adf73649fd62478357be30f71fb43', class: Object.assign(Object.assign({}, cssClass_helper.getClassMap(this.customClass)), { 'single-select-dropdown-slot': true }) }, index.h("kv-select-multi-options", { key: 'bde933426b770f432564ef3cfb6a16106f8ad9c8', 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, showShortcuts: this.showShortcuts, onSearchChange: this.onSearchChange, onClearSelection: this.onClearSelection, onOptionSelected: this.onOptionSelected, onOptionCreated: this.onOptionCreated, onDismiss: this.onDismiss, canAddItems: this.canAddItems, exportparts: "select,select-option-icon" }, index.h("slot", { key: '18d8a952d27eead433350ec7c7031fd42c4b2547', name: "create-new-option", slot: "create-new-option" }), index.h("slot", { key: 'df8750ee1178a798e054bdb81cc6d453f0601ccd', name: "select-header-actions", slot: "select-header-actions" }), index.h("slot", { key: '0929d5fe3bf2cba2722c4541e146330036efceaf', name: "select-header-label", slot: "select-header-label" }), index.h("slot", { key: '2edefef48f3b7e92055c9b2eb9c02c5475bf15bf', name: "no-data-available", slot: "no-data-available" }), index.h("slot", { key: '5732c9c22db1cf77fc978caae33e9eb9a36b7959', name: "no-results-found", slot: "no-results-found" }), index.h("slot", { key: '3f50b627592700132253bca262ea5be21d7f853c', name: "select-footer", slot: "select-footer" })), index.h("slot", { key: '8cdbc0340ea2b532f6a965ca51ae4b9ca5f35093' })))));
}
get el() { return index.getElement(this); }
static get watchers() { return {
"options": ["optionsChangeHandler"],
"selectedOption": ["selectedOptionChangeHandler"],
"displayValue": ["displayValueChangeHandler"],
"filteredOptions": ["filterOptionsChangeHandler"]
}; }
};
KvSingleSelectDropdown.style = singleSelectDropdownCss;
const timePickerSelectOptionCss = "@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{--time-picker-select-option-padding:0 var(--spacing-2xl);--time-picker-select-option-background-color:transparent;--time-picker-select-option-height:32px}.select-option{height:var(--time-picker-select-option-height);padding:var(--time-picker-select-option-padding);display:flex;align-items:center;user-select:none;cursor:pointer;background-color:var(--time-picker-select-option-background-color);transition:background-color 100ms linear}.select-option .text-container{display:flex;justify-content:space-between;align-items:center;min-width:0;width:100%}.select-option .text-container .item-label{font-family:Proxima Nova;font-weight:400;font-size:14px;line-height:20px;letter-spacing:0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;transition:font-weight 100ms linear;color:var(--text-interactive-slider-list-default)}.select-option .text-container .item-description{font-family:Proxima Nova;font-weight:400;font-size:12px;line-height:16px;letter-spacing:0;color:var(--text-on-color-neutral-tertiary)}.select-option:hover{background-color:var(--background-interactive-slider-list-hover)}.select-option.selected{background-color:var(--background-interactive-slider-list-selected)}.select-option.selected .item-label{font-family:Proxima Nova;font-weight:600;font-size:14px;line-height:20px;letter-spacing:0;color:var(--text-interactive-slider-list-selected)}.select-option.selected .item-description{font-family:Proxima Nova;font-weight:600;font-size:12px;line-height:16px;letter-spacing:0;transition:font-weight 100ms linear;color:var(--text-interactive-slider-list-default)}";
const KvTimePickerSelectOption = class {
constructor(hostRef) {
index.registerInstance(this, hostRef);
this.itemSelected = index.createEvent(this, "itemSelected", 7);
/** @inheritdoc */
this.selected = false;
/** @inheritdoc */
this.customAttributes = {};
this.onItemClick = () => {
this.itemSelected.emit(this.value);
};
}
render() {
return (index.h(index.Host, { key: '4a2b79e3adb3e6757e03dd81e4409f4a027d136a' }, index.h("div", Object.assign({ key: '157d96b50d7afb36a0111e15bc378ac665030a06', class: {
'select-option': true,
'selected': this.selected
}, onClick: this.onItemClick }, this.customAttributes), index.h("div", { key: '84efbd90d44e01b82160a083861e1e3e332c43c6', class: "text-container" }, index.h("div", { key: '56c57554ddb71c79048377a167ea2ae305750ca6', class: "item-label" }, this.label), index.h("div", { key: '8fb93bf958520780be1a51b45e41c7d0708b6ccc', class: "item-description" }, this.description)))));
}
};
KvTimePickerSelectOption.style = timePickerSelectOptionCss;
exports.kv_input_wrapper = KvInputWrapper;
exports.kv_single_select_dropdown = KvSingleSelectDropdown;
exports.kv_time_picker_select_option = KvTimePickerSelectOption;