2 lines
5.3 KiB
JavaScript
2 lines
5.3 KiB
JavaScript
import t,{Component as e}from"react";import{classNames as n}from"primereact/utils";import{tip as o}from"primereact/tooltip";function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(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 p(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function l(t,e){return l=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t},l(t,e)}function a(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&&l(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 u(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 p(t)}function c(t){return c=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)},c(t)}function f(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function y(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 h(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{};e%2?y(Object(n),!0).forEach((function(e){f(t,e,n[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):y(Object(n)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e))}))}return t}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}}var b=function(l){a(O,e);var s,f,y,b,v,m=(s=O,f=d(),function(){var t,e=c(s);if(f){var n=c(this).constructor;t=Reflect.construct(e,arguments,n)}else t=e.apply(this,arguments);return u(this,t)});function O(t){var e;return r(this,O),(e=m.call(this,t)).clear=e.clear.bind(p(e)),e.onStarKeyDown=e.onStarKeyDown.bind(p(e)),e.onCancelKeyDown=e.onCancelKeyDown.bind(p(e)),e}return y=O,(b=[{key:"rate",value:function(t,e){this.props.readOnly||this.props.disabled||!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}}),t.preventDefault()}},{key:"clear",value:function(t){this.props.readOnly||this.props.disabled||!this.props.onChange||this.props.onChange({originalEvent:t,value:null,stopPropagation:function(){},preventDefault:function(){},target:{name:this.props.name,id:this.props.id,value:null}}),t.preventDefault()}},{key:"shouldComponentUpdate",value:function(t,e){return t.value!==this.props.value||t.disabled!==this.props.disabled}},{key:"onStarKeyDown",value:function(t,e){"Enter"===t.key&&this.rate(t,e)}},{key:"onCancelKeyDown",value:function(t){"Enter"===t.key&&this.clear(t)}},{key:"getFocusIndex",value:function(){return this.props.disabled||this.props.readOnly?null:0}},{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(h({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=o({target:this.element,content:this.props.tooltip,options:this.props.tooltipOptions})}},{key:"renderStars",value:function(){for(var e=this,o=[],r=0;r<this.props.stars;r++)o[r]=r+1;return o.map((function(o){var r=n("p-rating-icon",{"pi pi-star":!e.props.value||o>e.props.value,"pi pi-star-fill":o<=e.props.value});return t.createElement("span",{className:r,onClick:function(t){return e.rate(t,o)},key:o,tabIndex:e.getFocusIndex(),onKeyDown:function(t){return e.onStarKeyDown(t,o)}})}))}},{key:"renderCancelIcon",value:function(){return this.props.cancel?t.createElement("span",{className:"p-rating-icon p-rating-cancel pi pi-ban",onClick:this.clear,tabIndex:this.getFocusIndex(),onKeyDown:this.onCancelKeyDown}):null}},{key:"render",value:function(){var e=this,o=n("p-rating",{"p-disabled":this.props.disabled,"p-rating-readonly":this.props.readOnly},this.props.className),r=this.renderCancelIcon(),i=this.renderStars();return t.createElement("div",{ref:function(t){return e.element=t},id:this.props.id,className:o,style:this.props.style},r,i)}}])&&i(y.prototype,b),v&&i(y,v),Object.defineProperty(y,"prototype",{writable:!1}),O}();f(b,"defaultProps",{id:null,value:null,disabled:!1,readOnly:!1,stars:5,cancel:!0,style:null,className:null,tooltip:null,tooltipOptions:null,onChange:null});export{b as Rating};
|