@easytool/react-permission
Version:
Easy to handle react component permission.
3 lines (2 loc) • 4.47 kB
JavaScript
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("react"),require("@babel/runtime/helpers/slicedToArray"),require("@babel/runtime/helpers/typeof"),require("prop-types")):"function"==typeof define&&define.amd?define(["exports","react","@babel/runtime/helpers/slicedToArray","@babel/runtime/helpers/typeof","prop-types"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self).ReactPermission={},e.React,e._slicedToArray,e._typeof,e.PropTypes)}(this,(function(e,r,n,t,o){"use strict";function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=i(r),u=i(n),l=i(t),f=i(o),s=a.default.createContext(null);function c(e){return Object.prototype.toString.call(e).match(/^\[object\s(.*)\]$/)[1]}function d(e){return"array"===c(e).toLowerCase()}function p(e){return"string"===c(e).toLowerCase()}function m(e){return"number"===c(e).toLowerCase()}function h(e){return!!e&&("object"===l.default(e)||"function"==typeof e)&&"function"==typeof e.then}function y(e){var r=!1;if(null==e)r=!0;else if((d(e)||p(e))&&0===e.length)r=!0;else if(function(e){return"object"===c(e).toLowerCase()}(e)){var n=!1;for(var t in e)if(e.hasOwnProperty(t)){n=!0;break}n||(r=!0)}else m(e)&&isNaN(e)&&(r=!0);return r}function v(e){return p(e)?e.trim():e}var b=60103;if("function"==typeof Symbol&&Symbol.for){var g=Symbol.for;b=g("react.element"),g("react.portal"),g("react.fragment"),g("react.strict_mode"),g("react.profiler"),g("react.provider"),g("react.context"),g("react.forward_ref"),g("react.suspense"),g("react.suspense_list"),g("react.memo"),g("react.lazy"),g("react.block"),g("react.server.block"),g("react.fundamental"),g("react.scope"),g("react.opaque.id"),g("react.debug_trace_mode"),g("react.offscreen"),g("react.legacy_hidden")}function P(e){return a.default.isValidElement(e)&&(null==e?void 0:e.$$typeof)===b}function j(e){var r=[];return d(e)?r=e:m(e)?r.push(e):p(e)&&(e.includes(",")?r=e.split(","):r.push(e)),r}function _(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return"permission__".concat(r)}function E(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];if(0===e.length)return!0;if(e.length>r.length)return!1;for(var n=function(n){var t=e[n];if(!r.some((function(e){return v(t)==v(e)})))return{v:!1}},t=0;t<e.length;t++){var o=n(t);if("object"===l.default(o))return o.v}return!0}function C(e,n,t){var o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0;if(!e)return null;if(P(e)){if(T(e,n,t)){var i=e.props.children;if(0===r.Children.count(i))return e;var u=C(i,n,t,o),l=x(u,i),f=a.default.cloneElement(e,{key:e.key||_(e,o)},l);return f}if(y(n)&&h(t.hasPermission))return null;var s=w(e,["onDeny","deny","data-ondeny","data-deny"]);return L(e,s||t.onDeny,o)}if(d(e)){var c=[];return r.Children.forEach(e,(function(e,r){var o=C(e,n,t,r);o&&c.push(o)})),c}return e}function T(e,r,n){var t=n.comparePermission,o=w(e,["permission","permissions","data-permission","data-permissions"]);return!!y(o)||!y(r)&&t(j(o),j(r))}function w(){for(var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],n=0;n<r.length;n++){var t=e.props[r[n]];if(t||0===t)return t}return null}function x(e,r){return e&&0!==e.length?1===e.length&&1===r.length&&(e=e[0]):e=null,e}function L(e,r,n){if(!r)return null;if(a.default.isValidElement(r))return r;if("function"==typeof r){var t=r(e,n);if(a.default.isValidElement(t))return t;if(t)throw"onDeny return a invalid react element."}return null}function O(e,r){var n=e.children,t=e.hasPermission,o=e.onLoad;return y(r)&&h(t)&&a.default.isValidElement(o)?o:C(n,r,e)}function k(e){var n=Object.assign({hasPermission:null,comparePermission:E,onLoad:null,onDeny:null,onError:null},r.useContext(s),e),t=r.useState(h(n.hasPermission)?null:n.hasPermission),o=u.default(t,2),i=o[0],a=o[1];return r.useEffect((function(){h(n.hasPermission)?n.hasPermission.then((function(e){return a(e)}),n.onError):a(n.hasPermission)}),[n.hasPermission]),O(n,i)}k.propTypes={hasPermission:f.default.oneOfType([f.default.string,f.default.number,f.default.array,f.default.func,f.default.object]),comparePermission:f.default.func,onLoad:f.default.object,onDeny:f.default.oneOfType([f.default.func,f.default.object]),onError:f.default.func},e.PermissionContext=s,e.default=k,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=index.umd.js.map