UNPKG

@yanxlg/react-permission-test

Version:
74 lines (66 loc) 2.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ReactPermissionTest = undefined; var _react = require("react"); var React = _interopRequireWildcard(_react); var _reactDom = require("react-dom"); var ReactDOM = _interopRequireWildcard(_reactDom); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj["default"] = obj; return newObj; } } /** * @disc:React 权限管理组件及自动化测试支持组件 * @type:Component * @author:yanxinaliang * @time:2018/9/4 11:14 */ var __extends = undefined && undefined.__extends || function () { var extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; } || function (d, b) { for (var p in b) { if (b.hasOwnProperty(p)) d[p] = b[p]; } }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; }(); var ReactPermissionTest = /** @class */function (_super) { __extends(ReactPermissionTest, _super); function ReactPermissionTest() { return _super !== null && _super.apply(this, arguments) || this; } ReactPermissionTest.prototype.hasPermission = function () { var _a = this.props, permissions = _a.permissions, permission = _a.permission; if (permissions && permission) { return permissions.find(function (per) { return per === permission; }); } return true; }; ReactPermissionTest.prototype.componentDidMount = function () { // id 添加 var id = this.props.id; // 如果没有id或者没有权限则放弃 var hasPermission = this.hasPermission(); if (!id || !hasPermission) return; var dom = ReactDOM.findDOMNode(this); // Element | null | Text if (null !== dom && void 0 !== dom.tagName) { // 标签元素 dom.setAttribute("test-id", id); } }; ReactPermissionTest.prototype.render = function () { var hasPermission = this.hasPermission(); return hasPermission ? this.props.children : null; }; return ReactPermissionTest; }(React.Component); exports.ReactPermissionTest = ReactPermissionTest;