我想创建一个高阶组件来处理它的子级根据传递的permissions
属性呈现的方式。
这是我现在所拥有的:
import React from "react";
const PERMISSION_VIEW = 1;
const PERMISSION_EDIT = 1 << 1;
// A High-Order Component that adds visual feedback according to the
// permissions prop that gets passed to it
function withPermissions(Component) {
const wrapper = (props, ref) => {
const { permissions } = props;
// Do not apply any permissions handling if we don't
// pass any permissions
if (permissions === undefined) {
return <Component {...props} forwardedRef={ref} />;
}
const propChanges = [];
let afterElements = [];
if ((permissions & PERMISSION_VIEW) === 0) {
// We'll assume that the data is already filtered from the server
afterElements.push(
<span key={0}>You do not have permissions to view this field</span>
);
}
if ((permissions & PERMISSION_EDIT) === 0) {
afterElements.push(
<span key={1}>You do not have permissions to edit this field</span>
);
propChanges.push({ readOnly: true, disabled: true });
}
props = Object.assign({}, props, ...propChanges);
return (
<React.Fragment>
<Component {...props} forwardedRef={ref} /> {afterElements}
</React.Fragment>
);
};
// Give this component a more helpful display name in DevTools.
// e.g. "ForwardRef(logProps(MyComponent))"
const name = Component.displayName || Component.name;
wrapper.displayName = `withPermissions(${name})`;
return React.forwardRef(wrapper);
}
这是一个使用示例
function Data(props) {
return props.value || "";
}
Data = withPermissions(Data);
const ref = React.createRef();
const App = () => <Data permissions={0} ref={ref} value="111" />;
console.log(App);
ReactDOM.render(<App />, document.getElementById("root"));
这是有效的,但我想做的是根据组件的类型有额外的行为
- 例如,如果它是一个
input
元素并且没有编辑权限,则创建该字段readonly
- 如果它是一个
textarea
元素并且没有查看权限,则创建该字段readonly
- 如果它是一个链接并且没有查看权限,请删除它的
href
道具等......
这件事甚至可能吗?有没有更好的方法来解决这个问题?