2 lines
5.2 KiB
JavaScript
2 lines
5.2 KiB
JavaScript
import t,{Component as o}from"react";import{classNames as e,IconUtils as n}from"primereact/utils";import{tip as r}from"primereact/tooltip";import{Ripple as i}from"primereact/ripple";function p(t,o){if(!(t instanceof o))throw new TypeError("Cannot call a class as a function")}function s(t,o){for(var e=0;e<o.length;e++){var n=o[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function l(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function c(t,o){return c=Object.setPrototypeOf||function(t,o){return t.__proto__=o,t},c(t,o)}function u(t,o){if("function"!=typeof o&&null!==o)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(o&&o.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),o&&c(t,o)}function a(t){return a="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},a(t)}function f(t,o){if(o&&("object"===a(o)||"function"==typeof o))return o;if(void 0!==o)throw new TypeError("Derived constructors may only return object or undefined");return l(t)}function h(t){return h=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)},h(t)}function b(t,o,e){return o in t?Object.defineProperty(t,o,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[o]=e,t}function y(t,o){var e=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);o&&(n=n.filter((function(o){return Object.getOwnPropertyDescriptor(t,o).enumerable}))),e.push.apply(e,n)}return e}function d(t){for(var o=1;o<arguments.length;o++){var e=null!=arguments[o]?arguments[o]:{};o%2?y(Object(e),!0).forEach((function(o){b(t,o,e[o])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(e)):y(Object(e)).forEach((function(o){Object.defineProperty(t,o,Object.getOwnPropertyDescriptor(e,o))}))}return t}function g(){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 m=function(c){u(w,o);var a,b,y,m,O,v=(a=w,b=g(),function(){var t,o=h(a);if(b){var e=h(this).constructor;t=Reflect.construct(o,arguments,e)}else t=o.apply(this,arguments);return f(this,t)});function w(t){var o;return p(this,w),(o=v.call(this,t)).toggle=o.toggle.bind(l(o)),o.onKeyDown=o.onKeyDown.bind(l(o)),o}return y=w,(m=[{key:"toggle",value:function(t){!this.props.disabled&&this.props.onChange&&this.props.onChange({originalEvent:t,value:!this.props.checked,stopPropagation:function(){},preventDefault:function(){},target:{name:this.props.name,id:this.props.id,value:!this.props.checked}})}},{key:"onKeyDown",value:function(t){"Enter"===t.key&&(this.toggle(t),t.preventDefault())}},{key:"hasLabel",value:function(){return this.props.onLabel&&this.props.onLabel.length>0&&this.props.offLabel&&this.props.offLabel.length>0}},{key:"hasIcon",value:function(){return this.props.onIcon&&this.props.onIcon.length>0&&this.props.offIcon&&this.props.offIcon.length>0}},{key:"getLabel",value:function(){return this.hasLabel()?this.props.checked?this.props.onLabel:this.props.offLabel:" "}},{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(d({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=r({target:this.container,content:this.props.tooltip,options:this.props.tooltipOptions})}},{key:"render",value:function(){var o=this,r=e("p-button p-togglebutton p-component",{"p-button-icon-only":this.hasIcon()&&!this.hasLabel(),"p-highlight":this.props.checked,"p-disabled":this.props.disabled},this.props.className),p=null,s=this.hasIcon(),l=this.getLabel(),c=this.props.checked?this.props.onIcon:this.props.offIcon;return s&&(p=e("p-button-icon p-c",{"p-button-icon-left":"left"===this.props.iconPos&&l,"p-button-icon-right":"right"===this.props.iconPos&&l})),t.createElement("div",{ref:function(t){return o.container=t},id:this.props.id,className:r,style:this.props.style,onClick:this.toggle,onFocus:this.props.onFocus,onBlur:this.props.onBlur,onKeyDown:this.onKeyDown,tabIndex:!this.props.disabled&&this.props.tabIndex,"aria-labelledby":this.props.ariaLabelledBy},s&&n.getJSXIcon(c,{className:p},{props:this.props}),t.createElement("span",{className:"p-button-label"},l),t.createElement(i,null))}}])&&s(y.prototype,m),O&&s(y,O),Object.defineProperty(y,"prototype",{writable:!1}),w}();b(m,"defaultProps",{id:null,onIcon:null,offIcon:null,onLabel:"Yes",offLabel:"No",iconPos:"left",style:null,className:null,checked:!1,tabIndex:0,tooltip:null,tooltipOptions:null,ariaLabelledBy:null,onChange:null,onFocus:null,onBlur:null});export{m as ToggleButton};
|