UNPKG

@auraxy/react-radio-checkbox

Version:

react radio checkbox component

10 lines (8 loc) 16.8 kB
/** * Bundle of @auraxy/react-radio-checkbox * Generated: 2021-05-18 * Version: 2.4.7 * License: MIT * Author: 2631541504@qq.com */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e=e||self).ReactRadioCheckbox={},e.React)}(this,function(e,b){"use strict";var k="default"in b?b.default:b;function c(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function r(t,e){var n,c=Object.keys(t);return Object.getOwnPropertySymbols&&(n=Object.getOwnPropertySymbols(t),e&&(n=n.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),c.push.apply(c,n)),c}function g(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{};e%2?r(Object(n),!0).forEach(function(e){c(t,e,n[e])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):r(Object(n)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e))})}return t}function a(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,c=new Array(t);n<t;n++)c[n]=e[n];return c}function y(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(e)){var n=[],c=!0,r=!1,a=void 0;try{for(var o,i=e[Symbol.iterator]();!(c=(o=i.next()).done)&&(n.push(o.value),!t||n.length!==t);c=!0);}catch(e){r=!0,a=e}finally{try{c||null==i.return||i.return()}finally{if(r)throw a}}return n}}(e,t)||function(e,t){if(e){if("string"==typeof e)return a(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Map"===(n="Object"===n&&e.constructor?e.constructor.name:n)||"Set"===n?Array.from(n):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?a(e,t):void 0}}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){for(var n=0;n<t.length;n++){var c=t[n];c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(e,c.key,c)}}function n(e,t,n){return t&&o(e.prototype,t),n&&o(e,n),e}var i,w=(function(e){function o(){for(var e=[],t=0;t<arguments.length;t++){var n=arguments[t];if(n){var c,r=typeof n;if("string"==r||"number"==r)e.push(n);else if(Array.isArray(n))!n.length||(c=o.apply(null,n))&&e.push(c);else if("object"==r)if(n.toString===Object.prototype.toString)for(var a in n)i.call(n,a)&&n[a]&&e.push(a);else e.push(n.toString())}}return e.join(" ")}var i;i={}.hasOwnProperty,e.exports?(o.default=o,e.exports=o):window.classNames=o}(i={exports:{}}),i.exports);function C(e,t){var n=b.useRef(),c=b.useRef();c.current=t||function(e){return e};var r=b.useReducer(function(e){return e+1},0)[1],t=b.useState(function(){return c.current(e)})[0],a=b.useRef(t);return e!==n.current&&(a.current=c.current(e,a.current,n.current),n.current=e),[a.current,b.useCallback(function(e){a.current="function"==typeof e?e(a.current):e,r()},[r])]}var v=k.createElement("svg",{viewBox:"0 0 1024 1024",xmlns:"http://www.w3.org/2000/svg"},k.createElement("path",{d:"M510.44839467 32.38975573c-264.06866667 0-478.03735467 213.96759573-478.03735467 478.03735467s213.96868693 478.03844587 478.03735467 478.03844587S988.48574933 774.49468587 988.48574933 510.42601813 774.5170624 32.38975573 510.44839467 32.38975573L510.44839467 32.38975573zM510.44839467 948.70451627c-241.54610667 0-438.27740587-196.50098667-438.27740587-438.27849707 0-241.54610667 196.49989547-438.27740587 438.27740587-438.27740587 241.54610667 0 438.27740587 196.50098667 438.27740586 438.27740587C948.72580053 751.97321707 751.99450133 948.70451627 510.44839467 948.70451627L510.44839467 948.70451627zM510.44839467 948.70451627",fill:"currentColor"})),p=k.createElement("svg",{viewBox:"0 0 1024 1024",xmlns:"http://www.w3.org/2000/svg"},k.createElement("path",{d:"M510.44839467 32.38975573c-264.06866667 0-478.03735467 213.96759573-478.03735467 478.03735467s213.96868693 478.03844587 478.03735467 478.03844587S988.48574933 774.49468587 988.48574933 510.42601813 774.5170624 32.38975573 510.44839467 32.38975573L510.44839467 32.38975573zM510.44839467 948.70451627c-241.54610667 0-438.27740587-196.50098667-438.27740587-438.27849707 0-241.54610667 196.49989547-438.27740587 438.27740587-438.27740587 241.54610667 0 438.27740587 196.50098667 438.27740586 438.27740587C948.72580053 751.97321707 751.99450133 948.70451627 510.44839467 948.70451627L510.44839467 948.70451627zM510.44839467 948.70451627",fill:"currentColor"}),k.createElement("path",{d:"M510.44839467 142.70598507c-203.16803093 0-367.72003413 164.5520032-367.72003414 367.72003413 0 203.1614816 164.5520032 367.72003413 367.72003414 367.72003413 203.1614816 0 367.72003413-164.55855147 367.72003413-367.72003413C878.1684288 307.2579872 713.60987733 142.70598507 510.44839467 142.70598507L510.44839467 142.70598507zM510.44839467 142.70598507",fill:"currentColor"})),I=k.createElement("svg",{viewBox:"0 0 1024 1024",xmlns:"http://www.w3.org/2000/svg"},k.createElement("path",{d:"M828.406 90.125H195.594c-58.219 0-105.469 47.25-105.469 105.469v632.812c0 58.219 47.25 105.469 105.469 105.469h632.812c58.219 0 105.469-47.25 105.469-105.469V195.594c0-58.219-47.25-105.469-105.469-105.469z m52.735 738.281c0 29.16-23.57 52.735-52.735 52.735H195.594c-29.11 0-52.735-23.575-52.735-52.735V195.594c0-29.11 23.625-52.735 52.735-52.735h632.812c29.16 0 52.735 23.625 52.735 52.735v632.812z",fill:"currentColor"}),k.createElement("path",{d:"M421.529 709.56a36.281 36.281 0 0 1-27.553-12.67L205.175 476.614a36.285 36.285 0 0 1 55.1-47.229L425.264 621.87l342.161-298.48a36.29 36.29 0 0 1 47.71 54.687L445.386 700.62a36.323 36.323 0 0 1-23.857 8.94z",fill:"currentColor"})),E=k.createElement("svg",{viewBox:"0 0 1024 1024",xmlns:"http://www.w3.org/2000/svg"},k.createElement("path",{d:"M849.5 90.125H174.5C128.09375 90.125 90.125 128.09375 90.125 174.5v675C90.125 895.90625 128.09375 933.875 174.5 933.875h675c46.40625 0 84.375-37.96875 84.375-84.375V174.5C933.875 128.09375 895.90625 90.125 849.5 90.125z m42.1875 759.375c0 23.26657104-18.92092896 42.1875-42.1875 42.1875H174.5c-23.26657104 0-42.1875-18.92092896-42.1875-42.1875V174.5c0-23.26657104 18.92092896-42.1875 42.1875-42.1875h675c23.26657104 0 42.1875 18.92092896 42.1875 42.1875v675z",fill:"currentColor"}),k.createElement("rect",{x:"262",y:"457",width:"500",height:"60",fill:"currentColor"})),O=k.createElement("svg",{viewBox:"0 0 1024 1024",xmlns:"http://www.w3.org/2000/svg"},k.createElement("path",{d:"M828.406 90.125H195.594c-58.219 0-105.469 47.25-105.469 105.469v632.812c0 58.219 47.25 105.469 105.469 105.469h632.812c58.219 0 105.469-47.25 105.469-105.469V195.594c0-58.219-47.25-105.469-105.469-105.469z m52.735 738.281c0 29.16-23.57 52.735-52.735 52.735H195.594c-29.11 0-52.735-23.575-52.735-52.735V195.594c0-29.11 23.625-52.735 52.735-52.735h632.812c29.16 0 52.735 23.625 52.735 52.735v632.812z",fill:"currentColor"})),x=k.createElement("svg",{viewBox:"0 0 1024 1024",xmlns:"http://www.w3.org/2000/svg"},k.createElement("path",{d:"M828.406 90.125H195.594c-58.219 0-105.469 47.25-105.469 105.469v632.812c0 58.219 47.25 105.469 105.469 105.469h632.812c58.219 0 105.469-47.25 105.469-105.469V195.594c0-58.219-47.25-105.469-105.469-105.469z m52.735 738.281c0 29.16-23.57 52.735-52.735 52.735H195.594c-29.11 0-52.735-23.575-52.735-52.735V195.594c0-29.11 23.625-52.735 52.735-52.735h632.812c29.16 0 52.735 23.625 52.735 52.735v632.812z",fill:"currentColor"}),k.createElement("path",{d:"M810.666667 128H213.333333c-47.146667 0-85.333333 38.186667-85.333333 85.333333v597.333334c0 47.146667 38.186667 85.333333 85.333333 85.333333h597.333334c47.146667 0 85.333333-38.186667 85.333333-85.333333V213.333333c0-47.146667-38.186667-85.333333-85.333333-85.333333zM426.666667 725.333333L213.333333 512l60.373334-60.373333L426.666667 604.586667l323.626666-323.626667L810.666667 341.333333 426.666667 725.333333z",fill:"currentColor"})),j=k.createElement("svg",{viewBox:"0 0 1024 1024",version:"1.1",xmlns:"http://www.w3.org/2000/svg"},k.createElement("path",{d:"M828.406 90.125H195.594c-58.219 0-105.469 47.25-105.469 105.469v632.812c0 58.219 47.25 105.469 105.469 105.469h632.812c58.219 0 105.469-47.25 105.469-105.469V195.594c0-58.219-47.25-105.469-105.469-105.469z m52.735 738.281c0 29.16-23.57 52.735-52.735 52.735H195.594c-29.11 0-52.735-23.575-52.735-52.735V195.594c0-29.11 23.625-52.735 52.735-52.735h632.812c29.16 0 52.735 23.625 52.735 52.735v632.812z",fill:"currentColor"}),k.createElement("path",{d:"M170.666667 128h682.666666a42.666667 42.666667 0 0 1 42.666667 42.666667v682.666666a42.666667 42.666667 0 0 1-42.666667 42.666667H170.666667a42.666667 42.666667 0 0 1-42.666667-42.666667V170.666667a42.666667 42.666667 0 0 1 42.666667-42.666667z m128 341.333333v85.333334h426.666666v-85.333334H298.666667z",fill:"currentColor"}));function M(){function e(){return Math.random().toString(36)}return(e()+e()).replace(/0\./,"").slice(0,15).toUpperCase()}function V(e,t,n){var c=b.useRef((2<arguments.length&&void 0!==n?n:{}).immediate),r=b.useRef({preWatches:void 0,watcher:t});r.current.watcher=t,b.useLayoutEffect(function(){c.current&&r.current.watcher(e,r.current.preWatches),r.current.preWatches=e,c.current=!0},[e])}var L=function(){function u(){t(this,u)}return n(u,null,[{key:"getItem",value:function(e,t){if(u.items[e])return u.items[e].find(function(e){return e.id===t})}},{key:"updateItem",value:function(e,t,n){var c=n.value,r=n.disabled,a=n.checked,o=n.update;u.items[e]||(u.items[e]=[]);var i,n=u.getItem(e,t);return n?(void 0!==c&&(n.value=c),void 0!==o&&(n.update=o),void 0!==a&&n.checked!==a&&(i=void 0,a&&u.items[e].forEach(function(e){e.checked&&(e.disabled?i=e:(e.checked=!1,e.update(e.checked)))}),i?console.warn("react-radio-checkbox(RadioItem): radio component of field '".concat(e,"' has a disabled checked item(value: ").concat(i.value,")")):(n.checked=a,n.update(n.checked)))):u.items[e].push(n={name:e,id:t,value:c,disabled:r||!1,checked:a||!1,update:o||function(){}}),n}},{key:"delItem",value:function(e,t){var n=u.items[e];!n||(e=Object.keys(n).find(function(e){return n[+e].id===t}))&&n.splice(+e,1)}},{key:"getValue",value:function(e){if(u.items[e]){e=u.items[e].find(function(e){return e.checked});return e&&e.value}}}]),u}();c(L,"items",{});var l=k.memo(function(e){var t=e.name,n=void 0===t?"$GLOBAL":t,c=e.className,r=e.value,a=e.disabled,o=e.checked,i=e.hideIcon,u=e.checkedIcon,l=void 0===u?p:u,t=e.uncheckedIcon,u=void 0===t?v:t,d=e.beforeChange,s=e.onChange,t=e.label||e.children,f=b.useMemo(M,[]),o=y(C(!!o),2),m=o[0],h=o[1];return b.useLayoutEffect(function(){var t=!1;return L.updateItem(n,f,{update:function(e){t||h(e)}}),function(){L.delItem(n,f),t=!0}},[n,f,h]),V("".concat(r,"-").concat(a,"-").concat(m),function(){L.updateItem(n,f,{value:r,disabled:a,checked:m})},{immediate:!0}),k.createElement("div",{className:w("radio",c,{checked:m,disabled:a}),onClick:function(){a||m||Promise.resolve(!d||d(!m,g({},L.getItem(n,f)),L.getValue(n))).then(function(e){!e||(e=L.updateItem(n,f,{checked:!m})).checked!==m&&s&&s(g({},e),L.getValue(n))})}},!i&&k.createElement("span",{className:"icon"},m?l:u),"function"==typeof t?t(g(g({},e),{},{checked:m}),m):t)}),u=k.memo(function(e){var t=e.name,n=e.items,c=e.value,r=e.onChange,a=e.className,o=e.hideIcon,i=e.disabled,u=b.useMemo(function(){return t||M()},[t]);return k.createElement("div",{className:w("radio-wrap",a)},n.map(function(e,t){return k.createElement(l,Object.assign({},e,{name:u,key:"".concat(e.value,"-").concat(t),hideIcon:o,checked:c===e.value,onChange:function(e,t){return r&&r(t)},disabled:i||e.disabled}))}))}),z=function(){function u(){t(this,u)}return n(u,null,[{key:"getController",value:function(e,t){if(u.items[e])return u.items[e].controllers.find(function(e){return e.id===t})}},{key:"getItem",value:function(e,t){if(u.items[e])return u.items[e].items.find(function(e){return e.id===t})}},{key:"autoUpdateControlItem",value:function(e){var t,n,c,r,a,o,i=u.items[e]&&u.items[e].controllers;i&&(r=c=n=t=0,u.items[e].items.forEach(function(e){t+=1,e.disabled||(n+=1),e.checked&&(c+=1),e.checked&&!e.disabled&&(r+=1)}),a=0!==n&&n===r,o=0<c&&c<t,i.forEach(function(e){e.checked===a&&e.partial===o||(e.checked=a,e.partial=o,e.update(e.checked,e.partial))}))}},{key:"updateController",value:function(e,t,n){var c=n.disabled,r=n.checked,a=n.update;u.items[e]||(u.items[e]={controllers:[],items:[]});n=u.getController(e,t);if(n)return void 0!==c&&(n.disabled=c),void 0!==a&&(n.update=a),n.disabled||void 0===r||n.checked===r||(u.items[e].items.forEach(function(e){e.disabled||e.checked===r||(e.checked=r,e.update(e.checked))}),u.autoUpdateControlItem(e)),n;a={name:e,id:t,disabled:c||!1,checked:r||!1,partial:!1,update:a||function(){}};return u.items[e].controllers.push(a),u.autoUpdateControlItem(e),a}},{key:"updateItem",value:function(e,t,n){var c=n.value,r=n.disabled,a=n.checked,o=n.update,i=!(3<arguments.length&&void 0!==arguments[3])||arguments[3];u.items[e]||(u.items[e]={controllers:[],items:[]});n=u.getItem(e,t);if(n)return void 0!==c&&(n.value=c),void 0!==r&&(n.disabled=r),void 0!==o&&(n.update=o),void 0!==a&&n.checked!==a&&(n.checked=a,n.update(n.checked),i&&u.autoUpdateControlItem(e)),n;o={name:e,id:t,value:c,disabled:r||!1,checked:a||!1,update:o||function(){}};return u.items[e].items.push(o),i&&u.autoUpdateControlItem(e),o}},{key:"delItem",value:function(e,t){var n,c=u.items[e]&&u.items[e].items;!c||(n=Object.keys(c).find(function(e){return c[+e].id===t}))&&(c.splice(+n,1),u.autoUpdateControlItem(e))}},{key:"getValues",value:function(e){e=u.items[e]&&u.items[e].items;return e?e.filter(function(e){return e.checked}).map(function(e){return e.value}):[]}}]),u}();c(z,"items",{});var d=k.memo(function(e){var t=e.name,n=void 0===t?"$GLOBAL":t,c=e.className,r=e.value,a=e.disabled,o=e.checked,i=e.hideIcon,u=e.checkedIcon,l=void 0===u?e.fill?x:I:u,t=e.uncheckedIcon,u=void 0===t?O:t,d=e.beforeChange,s=e.onChange,t=e.label||e.children,f=b.useMemo(M,[]),o=y(C(!!o),2),m=o[0],h=o[1];return b.useLayoutEffect(function(){var t=!1;return z.updateItem(n,f,{update:function(e){t||h(e)}}),function(){z.delItem(n,f),t=!0}},[n,f,h]),V("".concat(r,"-").concat(a,"-").concat(m),function(){z.updateItem(n,f,{value:r,disabled:a,checked:m})},{immediate:!0}),k.createElement("div",{className:w("checkbox",c,{checked:m,disabled:a}),onClick:function(){a||Promise.resolve(!d||d(!m,g({},z.getItem(n,f)),z.getValues(n))).then(function(e){!e||(e=z.updateItem(n,f,{checked:!m})).checked!==m&&s&&s(g({},e),z.getValues(n))})}},!i&&k.createElement("span",{className:"icon"},m?l:u),"function"==typeof t?t(g(g({},e),{},{checked:m}),m):t)}),s=k.memo(function(e){var t=e.name,c=void 0===t?"$GLOBAL":t,n=e.className,r=e.disabled,a=e.checked,o=e.hideIcon,i=e.checkedIcon,u=void 0===i?e.fill?x:I:i,l=e.partialIcon,d=void 0===l?e.fill?j:E:l,t=e.uncheckedIcon,i=void 0===t?O:t,s=e.beforeChange,f=e.onChange,l=e.label||e.children,m=b.useMemo(M,[]),t=y(C(!!a),2),h=t[0],v=t[1],a=y(b.useState(!1),2),t=a[0],p=a[1];return b.useLayoutEffect(function(){var n=!1;return z.updateController(c,m,{update:function(e,t){n||(v(e),p(t))}}),function(){z.delItem(c,m),n=!0}},[c,m,v,p]),V("".concat(r,"-").concat(h),function(){z.updateController(c,m,{disabled:r,checked:h})},{immediate:!0}),k.createElement("div",{className:w("checkbox",n,{checked:h,partial:t,disabled:r}),onClick:function(){r||Promise.resolve(!s||s(!h,g({},z.getController(c,m)),z.getValues(c))).then(function(e){!e||(e=z.updateController(c,m,{checked:!h})).checked!==h&&f&&f(g({},e),z.getValues(c))})}},!o&&k.createElement("span",{className:"icon"},t?d:h?u:i),"function"==typeof l?l(g(g({},e),{},{checked:h}),h):l)}),f=k.memo(function(e){var t=e.name,n=e.items,c=e.value,r=e.onChange,a=e.className,o=e.hideIcon,i=b.useMemo(function(){return t||M()},[t]);return k.createElement("div",{className:w("checkbox-wrap",a)},n.map(function(e,t){return k.createElement(d,Object.assign({},e,{name:i,key:"".concat(e.value,"-").concat(t),hideIcon:o,checked:!!c&&c.includes(e.value),onChange:function(e,t){return r&&r(t)}}))}))});e.CheckboxController=s,e.CheckboxItem=d,e.Checkboxs=f,e.RadioItem=l,e.Radios=u,e.Switch=function(e){var t=e.className,n=e.value,c=e.disabled,r=e.activeValue,a=void 0===r||r,r=e.inactiveValue,o=void 0!==r&&r,i=e.beforeChange,u=e.onChange,n=y(C(n),2),l=n[0],d=n[1];return k.createElement("div",{className:w("switch-wrap",t,{activated:l===a,disabled:c}),onClick:function(){var t;c||(t=l===a?o:a,Promise.resolve(!i||i(t)).then(function(e){e&&(d(t),u&&u(t))}))}},k.createElement("div",{className:"switch"}))},Object.defineProperty(e,"__esModule",{value:!0})});