UNPKG

primevue

Version:

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![npm version](https://badge.fury.io/js/primevue.svg)](https://badge.fury.io/js/primevue)

666 lines (615 loc) 25 kB
'use strict'; var utils = require('primevue/utils'); var Ripple = require('primevue/ripple'); var vue = require('vue'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var Ripple__default = /*#__PURE__*/_interopDefaultLegacy(Ripple); var script = { emits: ['update:modelValue', 'before-show', 'before-hide', 'show', 'hide', 'change', 'filter'], props: { modelValue: null, options: Array, optionLabel: null, optionValue: null, optionDisabled: null, scrollHeight: { type: String, default: '200px' }, filter: Boolean, filterPlaceholder: String, filterLocale: String, editable: Boolean, placeholder: String, disabled: Boolean, dataKey: null, showClear: Boolean, inputId: String, tabindex: String, ariaLabelledBy: null, appendTo: { type: String, default: null }, emptyFilterMessage: { type: String, default: 'No results found' } }, data() { return { focused: false, filterValue: null, overlayVisible: false }; }, outsideClickListener: null, scrollHandler: null, resizeListener: null, searchTimeout: null, currentSearchChar: null, previousSearchChar: null, searchValue: null, overlay: null, beforeUnmount() { this.restoreAppend(); this.unbindOutsideClickListener(); this.unbindResizeListener(); if (this.scrollHandler) { this.scrollHandler.destroy(); this.scrollHandler = null; } this.overlay = null; }, methods: { getOptionLabel(option) { return this.optionLabel ? utils.ObjectUtils.resolveFieldData(option, this.optionLabel) : option; }, getOptionValue(option) { return this.optionValue ? utils.ObjectUtils.resolveFieldData(option, this.optionValue) : option; }, getOptionRenderKey(option) { return this.dataKey ? utils.ObjectUtils.resolveFieldData(option, this.dataKey) : this.getOptionLabel(option); }, isOptionDisabled(option) { return this.optionDisabled ? utils.ObjectUtils.resolveFieldData(option, this.optionDisabled) : false; }, getSelectedOption() { let selectedOption; if (this.modelValue != null && this.options) { for (let option of this.options) { if ((utils.ObjectUtils.equals(this.modelValue, this.getOptionValue(option), this.equalityKey))) { selectedOption = option; break; } } } return selectedOption; }, isSelected(option) { return utils.ObjectUtils.equals(this.modelValue, this.getOptionValue(option), this.equalityKey); }, getSelectedOptionIndex() { let selectedOptionIndex = -1; if (this.modelValue != null && this.visibleOptions) { for (let i = 0; i < this.visibleOptions.length; i++) { if ((utils.ObjectUtils.equals(this.modelValue, this.getOptionValue(this.visibleOptions[i]), this.equalityKey))) { selectedOptionIndex = i; break; } } } return selectedOptionIndex; }, show() { this.$emit('before-show'); this.overlayVisible = true; }, hide() { this.$emit('before-hide'); this.overlayVisible = false; }, onFocus() { this.focused = true; }, onBlur() { this.focused = false; }, onKeyDown(event) { switch(event.which) { //down case 40: this.onDownKey(event); break; //up case 38: this.onUpKey(event); break; //space case 32: if (!this.overlayVisible) { this.show(); event.preventDefault(); } break; //enter and escape case 13: case 27: if (this.overlayVisible) { this.hide(); event.preventDefault(); } break; //tab case 9: this.hide(); break; default: this.search(event); break; } }, onFilterKeyDown(event) { switch (event.which) { //down case 40: this.onDownKey(event); break; //up case 38: this.onUpKey(event); break; //enter and escape case 13: case 27: this.overlayVisible = false; event.preventDefault(); break; } }, onDownKey(event) { if (this.visibleOptions) { if (!this.overlayVisible && event.altKey) { this.show(); } else { let nextOption = this.findNextOption(this.getSelectedOptionIndex()); if (nextOption) { this.updateModel(event, this.getOptionValue(nextOption)); } } } event.preventDefault(); }, onUpKey(event) { if (this.visibleOptions) { let prevOption = this.findPrevOption(this.getSelectedOptionIndex()); if (prevOption) { this.updateModel(event, this.getOptionValue(prevOption)); } } event.preventDefault(); }, findNextOption(index) { let i = index + 1; if (i === this.visibleOptions.length) { return null; } let option = this.visibleOptions[i]; if (this.isOptionDisabled(option)) return this.findNextOption(i); else return option; }, findPrevOption(index) { let i = index - 1; if (i < 0) { return null; } let option = this.visibleOptions[i]; if (this.isOptionDisabled(option)) return this.findPrevOption(i); else return option; }, onClearClick(event) { this.updateModel(event, null); }, onClick(event) { if (this.disabled) { return; } if (utils.DomHandler.hasClass(event.target, 'p-dropdown-clear-icon') || event.target.tagName === 'INPUT') { return; } else if (!this.overlay || !this.overlay.contains(event.target)) { if (this.overlayVisible) this.hide(); else this.show(); this.$refs.focusInput.focus(); } }, onOptionSelect(event, option) { let value = this.getOptionValue(option); this.updateModel(event, value); this.$refs.focusInput.focus(); setTimeout(() => { this.hide(); }, 200); }, onEditableInput(event) { this.$emit('update:modelValue', event.target.value); }, onOverlayEnter() { this.overlay.style.zIndex = String(utils.DomHandler.generateZIndex()); this.appendContainer(); this.alignOverlay(); this.bindOutsideClickListener(); this.bindScrollListener(); this.bindResizeListener(); if (this.filter) { this.$refs.filterInput.focus(); } this.$emit('show'); }, onOverlayLeave() { this.unbindOutsideClickListener(); this.unbindScrollListener(); this.unbindResizeListener(); this.$emit('hide'); this.overlay = null; }, alignOverlay() { if (this.appendTo) { utils.DomHandler.absolutePosition(this.overlay, this.$el); this.overlay.style.minWidth = utils.DomHandler.getOuterWidth(this.$el) + 'px'; } else { utils.DomHandler.relativePosition(this.overlay, this.$el); } }, updateModel(event, value) { this.$emit('update:modelValue', value); this.$emit('change', {originalEvent: event, value: value}); }, bindOutsideClickListener() { if (!this.outsideClickListener) { this.outsideClickListener = (event) => { if (this.overlayVisible && this.overlay && !this.$el.contains(event.target) && !this.overlay.contains(event.target)) { this.hide(); } }; document.addEventListener('click', this.outsideClickListener); } }, unbindOutsideClickListener() { if (this.outsideClickListener) { document.removeEventListener('click', this.outsideClickListener); this.outsideClickListener = null; } }, bindScrollListener() { if (!this.scrollHandler) { this.scrollHandler = new utils.ConnectedOverlayScrollHandler(this.$refs.container, () => { if (this.overlayVisible) { this.hide(); } }); } this.scrollHandler.bindScrollListener(); }, unbindScrollListener() { if (this.scrollHandler) { this.scrollHandler.unbindScrollListener(); } }, bindResizeListener() { if (!this.resizeListener) { this.resizeListener = () => { if (this.overlayVisible) { this.hide(); } }; window.addEventListener('resize', this.resizeListener); } }, unbindResizeListener() { if (this.resizeListener) { window.removeEventListener('resize', this.resizeListener); this.resizeListener = null; } }, search(event) { if (!this.visibleOptions) { return; } if (this.searchTimeout) { clearTimeout(this.searchTimeout); } const char = String.fromCharCode(event.keyCode); this.previousSearchChar = this.currentSearchChar; this.currentSearchChar = char; if (this.previousSearchChar === this.currentSearchChar) this.searchValue = this.currentSearchChar; else this.searchValue = this.searchValue ? this.searchValue + char : char; let searchIndex = this.getSelectedOptionIndex(); let newOption = this.searchOption(++searchIndex); if (newOption) { this.updateModel(event, this.getOptionValue(newOption)); } this.searchTimeout = setTimeout(() => { this.searchValue = null; }, 250); }, searchOption(index) { let option; if (this.searchValue) { option = this.searchOptionInRange(index, this.visibleOptions.length); if (!option) { option = this.searchOptionInRange(0, index); } } return option; }, searchOptionInRange(start, end) { for (let i = start; i < end; i++) { let opt = this.visibleOptions[i]; let label = this.getOptionLabel(opt).toLocaleLowerCase(this.filterLocale); if (label.startsWith(this.searchValue.toLocaleLowerCase(this.filterLocale))) { return opt; } } return null; }, appendContainer() { if (this.appendTo) { if (this.appendTo === 'body') document.body.appendChild(this.overlay); else document.getElementById(this.appendTo).appendChild(this.overlay); } }, restoreAppend() { if (this.overlay && this.appendTo) { if (this.appendTo === 'body') document.body.removeChild(this.overlay); else document.getElementById(this.appendTo).removeChild(this.overlay); } }, onFilterChange(event) { this.$emit('filter', {originalEvent: event, value: event.target.value}); if (this.overlayVisible) { this.alignOverlay(); } }, overlayRef(el) { this.overlay = el; } }, computed: { visibleOptions() { if (this.filterValue && this.filterValue.trim().length > 0) return this.options.filter(option => this.getOptionLabel(option).toLocaleLowerCase(this.filterLocale).indexOf(this.filterValue.toLocaleLowerCase(this.filterLocale)) > -1); else return this.options; }, containerClass() { return [ 'p-dropdown p-component p-inputwrapper', { 'p-disabled': this.disabled, 'p-dropdown-clearable': this.showClear && !this.disabled, 'p-focus': this.focused, 'p-inputwrapper-filled': this.modelValue, 'p-inputwrapper-focus': this.focused || this.overlayVisible } ]; }, labelClass() { return [ 'p-dropdown-label p-inputtext', { 'p-placeholder': this.label === this.placeholder, 'p-dropdown-label-empty': !this.$slots['value'] && (this.label === 'p-emptylabel' || this.label.length === 0) } ]; }, label() { let selectedOption = this.getSelectedOption(); if (selectedOption) return this.getOptionLabel(selectedOption); else return this.placeholder||'p-emptylabel'; }, editableInputValue() { let selectedOption = this.getSelectedOption(); if (selectedOption) return this.getOptionLabel(selectedOption); else return this.modelValue; }, equalityKey() { return this.optionValue ? null : this.dataKey; } }, directives: { 'ripple': Ripple__default['default'] } }; const _hoisted_1 = { class: "p-hidden-accessible" }; const _hoisted_2 = /*#__PURE__*/vue.createVNode("span", { class: "p-dropdown-trigger-icon pi pi-chevron-down" }, null, -1); const _hoisted_3 = { key: 0, class: "p-dropdown-header" }; const _hoisted_4 = { class: "p-dropdown-filter-container" }; const _hoisted_5 = /*#__PURE__*/vue.createVNode("span", { class: "p-dropdown-filter-icon pi pi-search" }, null, -1); const _hoisted_6 = { class: "p-dropdown-items", role: "listbox" }; const _hoisted_7 = { key: 0, class: "p-dropdown-empty-message" }; function render(_ctx, _cache, $props, $setup, $data, $options) { const _directive_ripple = vue.resolveDirective("ripple"); return (vue.openBlock(), vue.createBlock("div", { ref: "container", class: $options.containerClass, onClick: _cache[11] || (_cache[11] = $event => ($options.onClick($event))) }, [ vue.createVNode("div", _hoisted_1, [ vue.createVNode("input", { ref: "focusInput", type: "text", id: $props.inputId, readonly: "", disabled: $props.disabled, onFocus: _cache[1] || (_cache[1] = (...args) => ($options.onFocus && $options.onFocus(...args))), onBlur: _cache[2] || (_cache[2] = (...args) => ($options.onBlur && $options.onBlur(...args))), onKeydown: _cache[3] || (_cache[3] = (...args) => ($options.onKeyDown && $options.onKeyDown(...args))), tabindex: $props.tabindex, "aria-haspopup": "listbox", "aria-expanded": $data.overlayVisible, "aria-labelledby": $props.ariaLabelledBy }, null, 40, ["id", "disabled", "tabindex", "aria-expanded", "aria-labelledby"]) ]), ($props.editable) ? (vue.openBlock(), vue.createBlock("input", { key: 0, type: "text", class: "p-dropdown-label p-inputtext", disabled: $props.disabled, onFocus: _cache[4] || (_cache[4] = (...args) => ($options.onFocus && $options.onFocus(...args))), onBlur: _cache[5] || (_cache[5] = (...args) => ($options.onBlur && $options.onBlur(...args))), placeholder: $props.placeholder, value: $options.editableInputValue, onInput: _cache[6] || (_cache[6] = (...args) => ($options.onEditableInput && $options.onEditableInput(...args))), "aria-haspopup": "listbox", "aria-expanded": $data.overlayVisible }, null, 40, ["disabled", "placeholder", "value", "aria-expanded"])) : vue.createCommentVNode("", true), (!$props.editable) ? (vue.openBlock(), vue.createBlock("span", { key: 1, class: $options.labelClass }, [ vue.renderSlot(_ctx.$slots, "value", { value: $props.modelValue, placeholder: $props.placeholder }, () => [ vue.createTextVNode(vue.toDisplayString($options.label), 1) ]) ], 2)) : vue.createCommentVNode("", true), ($props.showClear && $props.modelValue != null) ? (vue.openBlock(), vue.createBlock("i", { key: 2, class: "p-dropdown-clear-icon pi pi-times", onClick: _cache[7] || (_cache[7] = $event => ($options.onClearClick($event))) })) : vue.createCommentVNode("", true), vue.createVNode("div", { class: "p-dropdown-trigger", role: "button", "aria-haspopup": "listbox", "aria-expanded": $data.overlayVisible }, [ _hoisted_2 ], 8, ["aria-expanded"]), vue.createVNode(vue.Transition, { name: "p-connected-overlay", onEnter: $options.onOverlayEnter, onLeave: $options.onOverlayLeave }, { default: vue.withCtx(() => [ ($data.overlayVisible) ? (vue.openBlock(), vue.createBlock("div", { key: 0, ref: $options.overlayRef, class: "p-dropdown-panel p-component" }, [ ($props.filter) ? (vue.openBlock(), vue.createBlock("div", _hoisted_3, [ vue.createVNode("div", _hoisted_4, [ vue.withDirectives(vue.createVNode("input", { type: "text", ref: "filterInput", "onUpdate:modelValue": _cache[8] || (_cache[8] = $event => ($data.filterValue = $event)), autoComplete: "off", class: "p-dropdown-filter p-inputtext p-component", placeholder: $props.filterPlaceholder, onKeydown: _cache[9] || (_cache[9] = (...args) => ($options.onFilterKeyDown && $options.onFilterKeyDown(...args))), onInput: _cache[10] || (_cache[10] = (...args) => ($options.onFilterChange && $options.onFilterChange(...args))) }, null, 40, ["placeholder"]), [ [vue.vModelText, $data.filterValue] ]), _hoisted_5 ]) ])) : vue.createCommentVNode("", true), vue.createVNode("div", { class: "p-dropdown-items-wrapper", style: {'max-height': $props.scrollHeight} }, [ vue.createVNode("ul", _hoisted_6, [ (vue.openBlock(true), vue.createBlock(vue.Fragment, null, vue.renderList($options.visibleOptions, (option, i) => { return vue.withDirectives((vue.openBlock(), vue.createBlock("li", { class: ['p-dropdown-item', {'p-highlight': $options.isSelected(option), 'p-disabled': $options.isOptionDisabled(option)}], "aria-label": $options.getOptionLabel(option), key: $options.getOptionRenderKey(option), onClick: $event => ($options.onOptionSelect($event, option)), role: "option", "aria-selected": $options.isSelected(option) }, [ vue.renderSlot(_ctx.$slots, "option", { option: option, index: i }, () => [ vue.createTextVNode(vue.toDisplayString($options.getOptionLabel(option)), 1) ]) ], 10, ["aria-label", "onClick", "aria-selected"])), [ [_directive_ripple] ]) }), 128)), ($data.filterValue && (!$options.visibleOptions || ($options.visibleOptions && $options.visibleOptions.length === 0))) ? (vue.openBlock(), vue.createBlock("li", _hoisted_7, vue.toDisplayString($props.emptyFilterMessage), 1)) : vue.createCommentVNode("", true) ]) ], 4) ], 512)) : vue.createCommentVNode("", true) ]), _: 1 }, 8, ["onEnter", "onLeave"]) ], 2)) } function styleInject(css, ref) { if ( ref === void 0 ) ref = {}; var insertAt = ref.insertAt; if (!css || typeof document === 'undefined') { return; } var head = document.head || document.getElementsByTagName('head')[0]; var style = document.createElement('style'); style.type = 'text/css'; if (insertAt === 'top') { if (head.firstChild) { head.insertBefore(style, head.firstChild); } else { head.appendChild(style); } } else { head.appendChild(style); } if (style.styleSheet) { style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); } } var css_248z = "\n.p-dropdown {\n display: -webkit-inline-box;\n display: -ms-inline-flexbox;\n display: inline-flex;\n cursor: pointer;\n position: relative;\n -webkit-user-select: none;\n -moz-user-select: none;\n -ms-user-select: none;\n user-select: none;\n}\n.p-dropdown-clear-icon {\n position: absolute;\n top: 50%;\n margin-top: -.5rem;\n}\n.p-dropdown-trigger {\n display: -webkit-box;\n display: -ms-flexbox;\n display: flex;\n -webkit-box-align: center;\n -ms-flex-align: center;\n align-items: center;\n -webkit-box-pack: center;\n -ms-flex-pack: center;\n justify-content: center;\n -ms-flex-negative: 0;\n flex-shrink: 0;\n}\n.p-dropdown-label {\n display: block;\n white-space: nowrap;\n overflow: hidden;\n -webkit-box-flex: 1;\n -ms-flex: 1 1 auto;\n flex: 1 1 auto;\n width: 1%;\n text-overflow: ellipsis;\n cursor: pointer;\n}\n.p-dropdown-label-empty {\n overflow: hidden;\n visibility: hidden;\n}\ninput.p-dropdown-label {\n cursor: default;\n}\n.p-dropdown .p-dropdown-panel {\n min-width: 100%;\n}\n.p-dropdown-panel {\n position: absolute;\n}\n.p-dropdown-items-wrapper {\n overflow: auto;\n}\n.p-dropdown-item {\n cursor: pointer;\n font-weight: normal;\n white-space: nowrap;\n position: relative;\n overflow: hidden;\n}\n.p-dropdown-items {\n margin: 0;\n padding: 0;\n list-style-type: none;\n}\n.p-dropdown-filter {\n width: 100%;\n}\n.p-dropdown-filter-container {\n position: relative;\n}\n.p-dropdown-filter-icon {\n position: absolute;\n top: 50%;\n margin-top: -.5rem;\n}\n.p-fluid .p-dropdown {\n display: -webkit-box;\n display: -ms-flexbox;\n display: flex;\n}\n.p-fluid .p-dropdown .p-dropdown-label {\n width: 1%;\n}\n"; styleInject(css_248z); script.render = render; module.exports = script;