primereact
Version:
PrimeReact is an open source UI library for React featuring a rich set of 90+ components, a theme designer, various theme alternatives such as Material, Bootstrap, Tailwind, premium templates and professional support. In addition, it integrates with Prime
2 lines (1 loc) • 3.35 kB
JavaScript
import*as e from"react";import{Tooltip as t}from"primereact/tooltip";import{ObjectUtils as n,DomHandler as r,classNames as o}from"primereact/utils";function l(){return l=Object.assign?Object.assign.bind():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},l.apply(this,arguments)}function u(e){if(Array.isArray(e))return e}function a(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=n){var r,o,l,u,a=[],i=!0,c=!1;try{if(l=(n=n.call(e)).next,0===t){if(Object(n)!==n)return;i=!1}else for(;!(i=(r=l.call(n)).done)&&(a.push(r.value),a.length!==t);i=!0);}catch(e){c=!0,o=e}finally{try{if(!i&&null!=n.return&&(u=n.return(),Object(u)!==u))return}finally{if(c)throw o}}return a}}function i(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,t){if(e){if("string"==typeof e)return i(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)?i(e,t):void 0}}function s(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var f={defaultProps:{__TYPE:"InputSwitch",checked:!1,className:null,disabled:!1,falseValue:!1,id:null,inputId:null,inputRef:null,name:null,onBlur:null,onChange:null,onFocus:null,style:null,tabIndex:null,tooltip:null,tooltipOptions:null,trueValue:!0,children:void 0},getProps:function(e){return n.getMergedProps(e,f.defaultProps)},getOtherProps:function(e){return n.getDiffProps(e,f.defaultProps)}},p=e.memo(e.forwardRef((function(i,p){var d,m,h=f.getProps(i),b=e.useState(!1),g=(m=2,u(d=b)||a(d,m)||c(d,m)||s()),y=g[0],v=g[1],P=e.useRef(null),I=e.useRef(h.inputRef),O=h.checked===h.trueValue,w=function(e){if(h.onChange){var t=O?h.falseValue:h.trueValue;h.onChange({originalEvent:e,value:t,stopPropagation:function(){},preventDefault:function(){},target:{name:h.name,id:h.id,value:t}})}};e.useImperativeHandle(p,(function(){return{props:h,focus:function(){return r.focus(I.current)},getElement:function(){return P.current},getInput:function(){return I.current}}})),e.useEffect((function(){n.combinedRefs(I,h.inputRef)}),[I,h.inputRef]);var E=n.isNotEmpty(h.tooltip),R=f.getOtherProps(h),k=n.reduceKeys(R,r.ARIA_PROPS),S=o("p-inputswitch p-component",{"p-inputswitch-checked":O,"p-disabled":h.disabled,"p-focus":y},h.className);return e.createElement(e.Fragment,null,e.createElement("div",l({ref:P,id:h.id,className:S,style:h.style},R,{onClick:function(e){h.disabled||(w(e),r.focus(I.current),e.preventDefault())},role:"checkbox","aria-checked":O}),e.createElement("div",{className:"p-hidden-accessible"},e.createElement("input",l({ref:I,type:"checkbox",id:h.inputId,name:h.name,checked:O,onChange:w,onFocus:function(e){v(!0),h.onFocus&&h.onFocus(e)},onBlur:function(e){v(!1),h.onBlur&&h.onBlur(e)},disabled:h.disabled,role:"switch",tabIndex:h.tabIndex,"aria-checked":O},k))),e.createElement("span",{className:"p-inputswitch-slider"})),E&&e.createElement(t,l({target:P,content:h.tooltip},h.tooltipOptions)))})));p.displayName="InputSwitch";export{p as InputSwitch};