@auraxy/react-radio-checkbox
Version:
react radio checkbox component
10 lines (8 loc) • 12.1 kB
JavaScript
/**
* 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).CheckboxItemReactRadioCheckbox={},e.React)}(this,function(e,b){"use strict";var y="default"in b?b.default:b;function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function c(t,e){var n,r=Object.keys(t);return Object.getOwnPropertySymbols&&(n=Object.getOwnPropertySymbols(t),e&&(n=n.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),r.push.apply(r,n)),r}function g(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{};e%2?c(Object(n),!0).forEach(function(e){r(t,e,n[e])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):c(Object(n)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e))})}return t}function o(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 k(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=[],r=!0,c=!1,o=void 0;try{for(var a,i=e[Symbol.iterator]();!(r=(a=i.next()).done)&&(n.push(a.value),!t||n.length!==t);r=!0);}catch(e){c=!0,o=e}finally{try{r||null==i.return||i.return()}finally{if(c)throw o}}return n}}(e,t)||function(e,t){if(e){if("string"==typeof e)return o(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)?o(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 a(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}var t,w=(function(e){function a(){for(var e=[],t=0;t<arguments.length;t++){var n=arguments[t];if(n){var r,c=typeof n;if("string"==c||"number"==c)e.push(n);else if(Array.isArray(n))!n.length||(r=a.apply(null,n))&&e.push(r);else if("object"==c)if(n.toString===Object.prototype.toString)for(var o in n)i.call(n,o)&&n[o]&&e.push(o);else e.push(n.toString())}}return e.join(" ")}var i;i={}.hasOwnProperty,e.exports?(a.default=a,e.exports=a):window.classNames=a}(t={exports:{}}),t.exports);function C(e,t){var n=b.useRef(),r=b.useRef();r.current=t||function(e){return e};var c=b.useReducer(function(e){return e+1},0)[1],t=b.useState(function(){return r.current(e)})[0],o=b.useRef(t);return e!==n.current&&(o.current=r.current(e,o.current,n.current),n.current=e),[o.current,b.useCallback(function(e){o.current="function"==typeof e?e(o.current):e,c()},[c])]}var E=y.createElement("svg",{viewBox:"0 0 1024 1024",xmlns:"http://www.w3.org/2000/svg"},y.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"}),y.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"})),I=y.createElement("svg",{viewBox:"0 0 1024 1024",xmlns:"http://www.w3.org/2000/svg"},y.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"}),y.createElement("rect",{x:"262",y:"457",width:"500",height:"60",fill:"currentColor"})),O=y.createElement("svg",{viewBox:"0 0 1024 1024",xmlns:"http://www.w3.org/2000/svg"},y.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=y.createElement("svg",{viewBox:"0 0 1024 1024",xmlns:"http://www.w3.org/2000/svg"},y.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"}),y.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=y.createElement("svg",{viewBox:"0 0 1024 1024",version:"1.1",xmlns:"http://www.w3.org/2000/svg"},y.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"}),y.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 V(){function e(){return Math.random().toString(36)}return(e()+e()).replace(/0\./,"").slice(0,15).toUpperCase()}function z(e,t,n){var r=b.useRef((2<arguments.length&&void 0!==n?n:{}).immediate),c=b.useRef({preWatches:void 0,watcher:t});c.current.watcher=t,b.useLayoutEffect(function(){r.current&&c.current.watcher(e,c.current.preWatches),c.current.preWatches=e,r.current=!0},[e])}var M=function(){function u(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,u)}var e,t,n;return e=u,n=[{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,r,c,o,a,i=u.items[e]&&u.items[e].controllers;i&&(c=r=n=t=0,u.items[e].items.forEach(function(e){t+=1,e.disabled||(n+=1),e.checked&&(r+=1),e.checked&&!e.disabled&&(c+=1)}),o=0!==n&&n===c,a=0<r&&r<t,i.forEach(function(e){e.checked===o&&e.partial===a||(e.checked=o,e.partial=a,e.update(e.checked,e.partial))}))}},{key:"updateController",value:function(e,t,n){var r=n.disabled,c=n.checked,o=n.update;u.items[e]||(u.items[e]={controllers:[],items:[]});n=u.getController(e,t);if(n)return void 0!==r&&(n.disabled=r),void 0!==o&&(n.update=o),n.disabled||void 0===c||n.checked===c||(u.items[e].items.forEach(function(e){e.disabled||e.checked===c||(e.checked=c,e.update(e.checked))}),u.autoUpdateControlItem(e)),n;o={name:e,id:t,disabled:r||!1,checked:c||!1,partial:!1,update:o||function(){}};return u.items[e].controllers.push(o),u.autoUpdateControlItem(e),o}},{key:"updateItem",value:function(e,t,n){var r=n.value,c=n.disabled,o=n.checked,a=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!==r&&(n.value=r),void 0!==c&&(n.disabled=c),void 0!==a&&(n.update=a),void 0!==o&&n.checked!==o&&(n.checked=o,n.update(n.checked),i&&u.autoUpdateControlItem(e)),n;a={name:e,id:t,value:r,disabled:c||!1,checked:o||!1,update:a||function(){}};return u.items[e].items.push(a),i&&u.autoUpdateControlItem(e),a}},{key:"delItem",value:function(e,t){var n,r=u.items[e]&&u.items[e].items;!r||(n=Object.keys(r).find(function(e){return r[+e].id===t}))&&(r.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}):[]}}],(t=null)&&a(e.prototype,t),n&&a(e,n),u}();r(M,"items",{});var u=y.memo(function(e){var t=e.name,n=void 0===t?"$GLOBAL":t,r=e.className,c=e.value,o=e.disabled,a=e.checked,i=e.hideIcon,u=e.checkedIcon,l=void 0===u?e.fill?x:E:u,t=e.uncheckedIcon,u=void 0===t?O:t,s=e.beforeChange,d=e.onChange,t=e.label||e.children,f=b.useMemo(V,[]),a=k(C(!!a),2),m=a[0],h=a[1];return b.useLayoutEffect(function(){var t=!1;return M.updateItem(n,f,{update:function(e){t||h(e)}}),function(){M.delItem(n,f),t=!0}},[n,f,h]),z("".concat(c,"-").concat(o,"-").concat(m),function(){M.updateItem(n,f,{value:c,disabled:o,checked:m})},{immediate:!0}),y.createElement("div",{className:w("checkbox",r,{checked:m,disabled:o}),onClick:function(){o||Promise.resolve(!s||s(!m,g({},M.getItem(n,f)),M.getValues(n))).then(function(e){!e||(e=M.updateItem(n,f,{checked:!m})).checked!==m&&d&&d(g({},e),M.getValues(n))})}},!i&&y.createElement("span",{className:"icon"},m?l:u),"function"==typeof t?t(g(g({},e),{},{checked:m}),m):t)}),n=y.memo(function(e){var t=e.name,r=void 0===t?"$GLOBAL":t,n=e.className,c=e.disabled,o=e.checked,a=e.hideIcon,i=e.checkedIcon,u=void 0===i?e.fill?x:E:i,l=e.partialIcon,s=void 0===l?e.fill?j:I:l,t=e.uncheckedIcon,i=void 0===t?O:t,d=e.beforeChange,f=e.onChange,l=e.label||e.children,m=b.useMemo(V,[]),t=k(C(!!o),2),h=t[0],p=t[1],o=k(b.useState(!1),2),t=o[0],v=o[1];return b.useLayoutEffect(function(){var n=!1;return M.updateController(r,m,{update:function(e,t){n||(p(e),v(t))}}),function(){M.delItem(r,m),n=!0}},[r,m,p,v]),z("".concat(c,"-").concat(h),function(){M.updateController(r,m,{disabled:c,checked:h})},{immediate:!0}),y.createElement("div",{className:w("checkbox",n,{checked:h,partial:t,disabled:c}),onClick:function(){c||Promise.resolve(!d||d(!h,g({},M.getController(r,m)),M.getValues(r))).then(function(e){!e||(e=M.updateController(r,m,{checked:!h})).checked!==h&&f&&f(g({},e),M.getValues(r))})}},!a&&y.createElement("span",{className:"icon"},t?s:h?u:i),"function"==typeof l?l(g(g({},e),{},{checked:h}),h):l)}),i=y.memo(function(e){var t=e.name,n=e.items,r=e.value,c=e.onChange,o=e.className,a=e.hideIcon,i=b.useMemo(function(){return t||V()},[t]);return y.createElement("div",{className:w("checkbox-wrap",o)},n.map(function(e,t){return y.createElement(u,Object.assign({},e,{name:i,key:"".concat(e.value,"-").concat(t),hideIcon:a,checked:!!r&&r.includes(e.value),onChange:function(e,t){return c&&c(t)}}))}))});e.CheckboxController=n,e.Checkboxs=i,e.default=u,Object.defineProperty(e,"__esModule",{value:!0})});