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