@yanxlg/react-permission-test
Version:
58 lines • 2.07 kB
JavaScript
/**
* @disc:React 权限管理组件及自动化测试支持组件
* @type:Component
* @author:yanxinaliang
* @time:2018/9/4 11:14
*/
var __extends = this && this.__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 __());
};
}();
import * as React from "react";
import * as ReactDOM from 'react-dom';
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);
export { ReactPermissionTest };