UNPKG

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.72 kB
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 a(){return a=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},a.apply(this,arguments)}function i(e){if(Array.isArray(e))return e}function u(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=n){var r,o,a,i,u=[],c=!0,l=!1;try{if(a=(n=n.call(e)).next,0===t){if(Object(n)!==n)return;c=!1}else for(;!(c=(r=a.call(n)).done)&&(u.push(r.value),u.length!==t);c=!0);}catch(e){l=!0,o=e}finally{try{if(!c&&null!=n.return&&(i=n.return(),Object(i)!==i))return}finally{if(l)throw o}}return u}}function c(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 l(e,t){if(e){if("string"==typeof e)return c(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)?c(e,t):void 0}}function d(){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 s={defaultProps:{__TYPE:"RadioButton",id:null,inputRef:null,inputId:null,name:null,value:null,checked:!1,style:null,className:null,disabled:!1,required:!1,tabIndex:null,tooltip:null,tooltipOptions:null,onChange:null,children:void 0},getProps:function(e){return n.getMergedProps(e,s.defaultProps)},getOtherProps:function(e){return n.getDiffProps(e,s.defaultProps)}},f=e.memo(e.forwardRef((function(c,f){var p,m,h=s.getProps(c),b=e.useState(!1),g=(m=2,i(p=b)||u(p,m)||l(p,m)||d()),v=g[0],y=g[1],k=e.useRef(null),E=e.useRef(h.inputRef),P=function(e){O(e)},O=function(e){if(!h.disabled&&h.onChange){var t=h.checked,n=e.target instanceof HTMLDivElement,o=e.target===E.current&&e.target.checked!==t,a=n&&r.hasClass(k.current,"p-radiobutton-checked")===t&&!t;if(o||a){var i=!t;h.onChange({originalEvent:e,value:h.value,checked:i,stopPropagation:function(){},preventDefault:function(){},target:{type:"radio",name:h.name,id:h.id,value:h.value,checked:i}}),a&&(E.current.checked=i)}r.focus(E.current),e.preventDefault()}};e.useEffect((function(){E.current&&(E.current.checked=h.checked)}),[h.checked]),e.useEffect((function(){n.combinedRefs(E,h.inputRef)}),[E,h.inputRef]),e.useImperativeHandle(f,(function(){return{props:h,select:P,focus:function(){return r.focus(E.current)},getElement:function(){return k.current},getInput:function(){return E.current}}}));var R=n.isNotEmpty(h.tooltip),I=s.getOtherProps(h),j=n.reduceKeys(I,r.ARIA_PROPS),A=o("p-radiobutton p-component",{"p-radiobutton-checked":h.checked,"p-radiobutton-disabled":h.disabled,"p-radiobutton-focused":v},h.className),N=o("p-radiobutton-box",{"p-highlight":h.checked,"p-disabled":h.disabled,"p-focus":v});return e.createElement(e.Fragment,null,e.createElement("div",a({ref:k,id:h.id,className:A,style:h.style},I,{onClick:O}),e.createElement("div",{className:"p-hidden-accessible"},e.createElement("input",a({ref:E,id:h.inputId,type:"radio",name:h.name,defaultChecked:h.checked,onFocus:function(){y(!0)},onBlur:function(){y(!1)},onKeyDown:function(e){"Space"!==e.code&&" "!==e.key||O(e)},disabled:h.disabled,required:h.required,tabIndex:h.tabIndex},j))),e.createElement("div",{className:N},e.createElement("div",{className:"p-radiobutton-icon"}))),R&&e.createElement(t,a({target:k,content:h.tooltip},h.tooltipOptions)))})));f.displayName="RadioButton";export{f as RadioButton};