2 lines
16 KiB
JavaScript
2 lines
16 KiB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("primereact/api"),n=require("primereact/utils"),r=require("primereact/ripple"),o=require("primereact/inputtext"),i=require("primereact/tooltip"),l=require("primereact/virtualscroller");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var s=a(e);function p(){return p=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},p.apply(this,arguments)}function u(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function c(e){if(Array.isArray(e))return u(e)}function f(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function h(e,t){if(e){if("string"==typeof e)return u(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?u(e,t):void 0}}function d(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function v(e){return c(e)||f(e)||h(e)||d()}function y(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function b(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function m(e,t,n){return t&&b(e.prototype,t),n&&b(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e}function O(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function g(e,t){return g=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},g(e,t)}function k(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&g(e,t)}function E(e){return E="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},E(e)}function S(e,t){if(t&&("object"===E(t)||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return O(e)}function j(e){return j=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},j(e)}function C(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function w(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}var x=function(t){k(a,e.Component);var o,i,l=(o=a,i=w(),function(){var e,t=j(o);if(i){var n=j(this).constructor;e=Reflect.construct(t,arguments,n)}else e=t.apply(this,arguments);return S(this,e)});function a(e){var t;return y(this,a),(t=l.call(this,e)).onClick=t.onClick.bind(O(t)),t.onTouchEnd=t.onTouchEnd.bind(O(t)),t.onKeyDown=t.onKeyDown.bind(O(t)),t}return m(a,[{key:"onClick",value:function(e){this.props.onClick&&this.props.onClick({originalEvent:e,option:this.props.option}),e.preventDefault()}},{key:"onTouchEnd",value:function(e){this.props.onTouchEnd&&this.props.onTouchEnd({originalEvent:e,option:this.props.option})}},{key:"onKeyDown",value:function(e){var t=e.currentTarget;switch(e.which){case 40:var n=this.findNextItem(t);n&&n.focus(),e.preventDefault();break;case 38:var r=this.findPrevItem(t);r&&r.focus(),e.preventDefault();break;case 13:this.onClick(e),e.preventDefault()}}},{key:"findNextItem",value:function(e){var t=e.nextElementSibling;return t?n.DomHandler.hasClass(t,"p-disabled")||n.DomHandler.hasClass(t,"p-listbox-item-group")?this.findNextItem(t):t:null}},{key:"findPrevItem",value:function(e){var t=e.previousElementSibling;return t?n.DomHandler.hasClass(t,"p-disabled")||n.DomHandler.hasClass(t,"p-listbox-item-group")?this.findPrevItem(t):t:null}},{key:"render",value:function(){var e=n.classNames("p-listbox-item",{"p-highlight":this.props.selected,"p-disabled":this.props.disabled},this.props.option.className),t=this.props.template?n.ObjectUtils.getJSXElement(this.props.template,this.props.option):this.props.label;return s.default.createElement("li",{className:e,onClick:this.onClick,onTouchEnd:this.onTouchEnd,onKeyDown:this.onKeyDown,tabIndex:this.props.tabIndex,"aria-label":this.props.label,key:this.props.label,role:"option","aria-selected":this.props.selected},t,s.default.createElement(r.Ripple,null))}}]),a}();function D(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}C(x,"defaultProps",{option:null,label:null,selected:!1,disabled:!1,tabIndex:null,onClick:null,onTouchEnd:null,template:null});var T=function(t){k(l,e.Component);var n,r,i=(n=l,r=D(),function(){var e,t=j(n);if(r){var o=j(this).constructor;e=Reflect.construct(t,arguments,o)}else e=t.apply(this,arguments);return S(this,e)});function l(e){var t;return y(this,l),(t=i.call(this,e)).onFilter=t.onFilter.bind(O(t)),t}return m(l,[{key:"onFilter",value:function(e){this.props.onFilter&&this.props.onFilter({originalEvent:e,value:e.target.value})}},{key:"render",value:function(){return s.default.createElement("div",{className:"p-listbox-header"},s.default.createElement("div",{className:"p-listbox-filter-container"},s.default.createElement(o.InputText,{type:"text",value:this.props.filter,onChange:this.onFilter,className:"p-listbox-filter",disabled:this.props.disabled,placeholder:this.props.filterPlaceholder}),s.default.createElement("span",{className:"p-listbox-filter-icon pi pi-search"})))}}]),l}();function F(e,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!n){if(Array.isArray(e)||(n=L(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var r=0,o=function(){};return{s:o,n:function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,l=!0,a=!1;return{s:function(){n=n.call(e)},n:function(){var e=n.next();return l=e.done,e},e:function(e){a=!0,i=e},f:function(){try{l||null==n.return||n.return()}finally{if(a)throw i}}}}function L(e,t){if(e){if("string"==typeof e)return P(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?P(e,t):void 0}}function P(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function V(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function I(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?V(Object(n),!0).forEach((function(t){C(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):V(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function R(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}C(T,"defaultProps",{filter:null,filterPlaceholder:null,disabled:!1,onFilter:null});var K=function(r){k(c,e.Component);var o,a,u=(o=c,a=R(),function(){var e,t=j(o);if(a){var n=j(this).constructor;e=Reflect.construct(t,arguments,n)}else e=t.apply(this,arguments);return S(this,e)});function c(e){var t;return y(this,c),(t=u.call(this,e)).state={},t.props.onFilterValueChange||(t.state.filterValue=""),t.onFilter=t.onFilter.bind(O(t)),t.onOptionSelect=t.onOptionSelect.bind(O(t)),t.onOptionTouchEnd=t.onOptionTouchEnd.bind(O(t)),t}return m(c,[{key:"componentDidMount",value:function(){this.props.tooltip&&this.renderTooltip()}},{key:"componentDidUpdate",value:function(e){e.tooltip===this.props.tooltip&&e.tooltipOptions===this.props.tooltipOptions||(this.tooltip?this.tooltip.update(I({content:this.props.tooltip},this.props.tooltipOptions||{})):this.renderTooltip())}},{key:"componentWillUnmount",value:function(){this.tooltip&&(this.tooltip.destroy(),this.tooltip=null)}},{key:"renderTooltip",value:function(){this.tooltip=i.tip({target:this.element,content:this.props.tooltip,options:this.props.tooltipOptions})}},{key:"getFilterValue",value:function(){return(this.props.onFilterValueChange?this.props.filterValue:this.state.filterValue)||""}},{key:"onOptionSelect",value:function(e){var t=e.option;this.props.disabled||this.isOptionDisabled(t)||(this.props.multiple?this.onOptionSelectMultiple(e.originalEvent,t):this.onOptionSelectSingle(e.originalEvent,t),this.optionTouched=!1)}},{key:"onOptionTouchEnd",value:function(){this.props.disabled||(this.optionTouched=!0)}},{key:"onOptionSelectSingle",value:function(e,t){var n=this.isSelected(t),r=!1,o=null;!this.optionTouched&&this.props.metaKeySelection?n?(e.metaKey||e.ctrlKey)&&(o=null,r=!0):(o=this.getOptionValue(t),r=!0):(o=n?null:this.getOptionValue(t),r=!0);r&&this.updateModel(e,o)}},{key:"onOptionSelectMultiple",value:function(e,t){var n=this.isSelected(t),r=!1,o=null;if(!this.optionTouched&&this.props.metaKeySelection){var i=e.metaKey||e.ctrlKey;n?(o=i?this.removeOption(t):[this.getOptionValue(t)],r=!0):(o=[].concat(v(o=i&&this.props.value||[]),[this.getOptionValue(t)]),r=!0)}else o=n?this.removeOption(t):[].concat(v(this.props.value||[]),[this.getOptionValue(t)]),r=!0;r&&this.props.onChange({originalEvent:e,value:o,stopPropagation:function(){},preventDefault:function(){},target:{name:this.props.name,id:this.props.id,value:o}})}},{key:"onFilter",value:function(e){var t=e.value;this.props.onFilterValueChange?this.props.onFilterValueChange({originalEvent:e.originalEvent,value:t}):this.setState({filterValue:t})}},{key:"updateModel",value:function(e,t){this.props.onChange&&this.props.onChange({originalEvent:e,value:t,stopPropagation:function(){},preventDefault:function(){},target:{name:this.props.name,id:this.props.id,value:t}})}},{key:"removeOption",value:function(e){var t=this;return this.props.value.filter((function(r){return!n.ObjectUtils.equals(r,t.getOptionValue(e),t.props.dataKey)}))}},{key:"isSelected",value:function(e){var t=!1,r=this.getOptionValue(e);if(this.props.multiple){if(this.props.value){var o,i=F(this.props.value);try{for(i.s();!(o=i.n()).done;){if(n.ObjectUtils.equals(o.value,r,this.props.dataKey)){t=!0;break}}}catch(e){i.e(e)}finally{i.f()}}}else t=n.ObjectUtils.equals(this.props.value,r,this.props.dataKey);return t}},{key:"filter",value:function(e){var t=this.getFilterValue().trim().toLocaleLowerCase(this.props.filterLocale);return this.getOptionLabel(e).toLocaleLowerCase(this.props.filterLocale).indexOf(t)>-1}},{key:"hasFilter",value:function(){var e=this.getFilterValue();return e&&e.trim().length>0}},{key:"getOptionLabel",value:function(e){return this.props.optionLabel?n.ObjectUtils.resolveFieldData(e,this.props.optionLabel):e&&void 0!==e.label?e.label:e}},{key:"getOptionValue",value:function(e){return this.props.optionValue?n.ObjectUtils.resolveFieldData(e,this.props.optionValue):e&&void 0!==e.value?e.value:e}},{key:"getOptionRenderKey",value:function(e){return this.props.dataKey?n.ObjectUtils.resolveFieldData(e,this.props.dataKey):this.getOptionLabel(e)}},{key:"isOptionDisabled",value:function(e){return this.props.optionDisabled?n.ObjectUtils.isFunction(this.props.optionDisabled)?this.props.optionDisabled(e):n.ObjectUtils.resolveFieldData(e,this.props.optionDisabled):!(!e||void 0===e.disabled)&&e.disabled}},{key:"getOptionGroupRenderKey",value:function(e){return n.ObjectUtils.resolveFieldData(e,this.props.optionGroupLabel)}},{key:"getOptionGroupLabel",value:function(e){return n.ObjectUtils.resolveFieldData(e,this.props.optionGroupLabel)}},{key:"getOptionGroupChildren",value:function(e){return n.ObjectUtils.resolveFieldData(e,this.props.optionGroupChildren)}},{key:"getVisibleOptions",value:function(){if(this.hasFilter()){var e=this.getFilterValue().trim().toLocaleLowerCase(this.props.filterLocale),n=this.props.filterBy?this.props.filterBy.split(","):[this.props.optionLabel||"label"];if(this.props.optionGroupLabel){var r,o=[],i=F(this.props.options);try{for(i.s();!(r=i.n()).done;){var l=r.value,a=t.FilterService.filter(this.getOptionGroupChildren(l),n,e,this.props.filterMatchMode,this.props.filterLocale);a&&a.length&&o.push(I(I({},l),{items:a}))}}catch(e){i.e(e)}finally{i.f()}return o}return t.FilterService.filter(this.props.options,n,e,this.props.filterMatchMode,this.props.filterLocale)}return this.props.options}},{key:"renderGroupChildren",value:function(e){var t=this;return this.getOptionGroupChildren(e).map((function(e,n){var r=t.getOptionLabel(e),o=n+"_"+t.getOptionRenderKey(e),i=t.isOptionDisabled(e),l=i?null:t.props.tabIndex||0;return s.default.createElement(x,{key:o,label:r,option:e,template:t.props.itemTemplate,selected:t.isSelected(e),onClick:t.onOptionSelect,onTouchEnd:t.onOptionTouchEnd,tabIndex:l,disabled:i})}))}},{key:"renderItem",value:function(e,t){if(this.props.optionGroupLabel){var r=this.props.optionGroupTemplate?n.ObjectUtils.getJSXElement(this.props.optionGroupTemplate,e,t):this.getOptionGroupLabel(e),o=this.renderGroupChildren(e),i=t+"_"+this.getOptionGroupRenderKey(e);return s.default.createElement(s.default.Fragment,{key:i},s.default.createElement("li",{className:"p-listbox-item-group"},r),o)}var l=this.getOptionLabel(e),a=t+"_"+this.getOptionRenderKey(e),p=this.isOptionDisabled(e),u=p?null:this.props.tabIndex||0;return s.default.createElement(x,{key:a,label:l,option:e,template:this.props.itemTemplate,selected:this.isSelected(e),onClick:this.onOptionSelect,onTouchEnd:this.onOptionTouchEnd,tabIndex:u,disabled:p})}},{key:"renderItems",value:function(e){var t=this;return e&&e.length?e.map((function(e,n){return t.renderItem(e,n)})):null}},{key:"renderList",value:function(e){var t=this;if(this.props.virtualScrollerOptions){var r=I(I({},this.props.virtualScrollerOptions),{items:e,onLazyLoad:function(e){return t.props.virtualScrollerOptions.onLazyLoad(I(I({},e),{filter:t.getFilterValue()}))},itemTemplate:function(e,n){return e&&t.renderItem(e,n.index)},contentTemplate:function(e){var r=n.classNames("p-listbox-list",e.className);return s.default.createElement("ul",{ref:e.contentRef,className:r,role:"listbox","aria-multiselectable":t.props.multiple},e.children)}});return s.default.createElement(l.VirtualScroller,p({ref:function(e){return t.virtualScrollerRef=e}},r))}var o=this.renderItems(e);return s.default.createElement("ul",{className:"p-listbox-list",role:"listbox","aria-multiselectable":this.props.multiple},o)}},{key:"render",value:function(){var e,t=this,r=n.classNames("p-listbox p-component",{"p-disabled":this.props.disabled},this.props.className),o=n.classNames("p-listbox-list-wrapper",this.props.listClassName),i=this.getVisibleOptions(),l=this.renderList(i);return this.props.filter&&(e=s.default.createElement(T,{filter:this.getFilterValue(),onFilter:this.onFilter,disabled:this.props.disabled,filterPlaceholder:this.props.filterPlaceholder})),s.default.createElement("div",{ref:function(e){return t.element=e},id:this.props.id,className:r,style:this.props.style},e,s.default.createElement("div",{ref:function(e){return t.wrapper=e},className:o,style:this.props.listStyle},l))}}]),c}();C(K,"defaultProps",{id:null,value:null,options:null,optionLabel:null,optionValue:null,optionDisabled:null,optionGroupLabel:null,optionGroupChildren:null,optionGroupTemplate:null,itemTemplate:null,style:null,listStyle:null,listClassName:null,className:null,virtualScrollerOptions:null,disabled:null,dataKey:null,multiple:!1,metaKeySelection:!1,filter:!1,filterBy:null,filterValue:null,filterMatchMode:"contains",filterPlaceholder:null,filterLocale:void 0,tabIndex:0,tooltip:null,tooltipOptions:null,ariaLabelledBy:null,onChange:null,onFilterValueChange:null}),exports.ListBox=K;
|