React权限控制(特殊组件&按钮)

react项目中需要实现一个按钮级权限,一开始想的是写一个 AuthButton 组件,然后直接判断权限就好了。 但是,没有这么容易,需求有些不一样...

  • 有一些 TextArea 也需要实现(根据用户权限判断)可查看但不可编辑;
  • Dropdown Menu 有些需要实现(根据用户权限判断)可查看但某些 Menu Item 不可编辑;
  • 等等一些其他的特殊组件

思路1 ------ 每种组件写一个 AuthComp 【太不好维护,每增加一种组件权限需要再写新的组件,组件多维护成本高】

思路2------写一个 AuthWrapper, 将 disabled 和 visible 参数透传进最后的 button 里【代码耦合度太高了,props 传递了太多层,需要修改多个组件,容易产生bug】

思路3 ------ 写一个 AuthWrapper,将每个需要权限控制的组件 / 按钮的外层套一个AuthWrapper的壳。 【优点】:AuthWrapper 专注做用户权限相关交互,内部的 children 就只放 UI 组件相关,交互行为与UI组件相独立,耦合度低。

最后,我决定用 方案3,但是有一个新的问题

js 复制代码
const MyWrapper = ({ children }) => {
  return visible ? children(disabled) : null
}

// 使用
<MyWrapper auth="home.detail.edit">
  {({ disabled }) => ( <button disabled={disabled}>按钮</button> )}
</MyWrapper>

要用 propsRender 的方式穿参,如果每个地方都这样写,那也太奇怪了吧!!

经过一番查询,找到了方法

js 复制代码
const MyWrapper = ({ children, auth }) => {
  return React.Children.map(children, child => { 
    // 将disabled属性传递给子组件 
    return React.cloneElement(child, { disabled }); 
});
}

但是 MenuItem 的 disbled 属性怎么办呢?继续递归处理。 完整的测试代码如下:(代码使用"码上掘金"编辑器编写,可以直接复制进编辑器运行)

js 复制代码
import React, { useState, useCallback } from 'react';
import ReactDom from 'react-dom';

const MyWrapper = ({ children, auth }) => {
  // 模拟权限列表,根据实际情况修改
  const permissionList = ['home.detail.edit', 'some.other.permission']; 
  
  // 检查是否有权限
  const hasPermission = (permission) => {
    return permissionList.includes(permission);
  };

  const isVisible = permissionList.includes(auth);
  const isEditable = permissionList.includes(auth);

  // 根据权限信息决定是否禁用按钮
  const disabled = !isEditable;

  // 处理子组件
  const handleChild = (child) => {
    const disabled = !hasPermission(auth);
    // 如果子组件是 <select> 元素
    if (child.type === 'select') {
      // 提取出 <select> 元素
      // const selectElement = React.Children.only(child);
      // 这一步是为了限制 select 整个组件的 disabled
      const selectElement = React.cloneElement(child, { disabled }) 
      // 处理 <option> 元素
      const options = React.Children.map(selectElement.props.children, option => {
        if (option.type === 'option' && option.props.auth) {
          // 根据权限信息设置是否禁用,hidden处理子元素的隐藏
          const optionDisabled = !hasPermission(option.props.auth);
          return React.cloneElement(option, { disabled: optionDisabled , hidden: option.props.value === 'opel'});
        }
        return option;
      });
      // 返回处理后的 <select> 元素
      return React.cloneElement(selectElement, null, options);
    } else {
      return React.cloneElement(child, { disabled });
    }
  };

  // return isVisible ? React.Children.map(children, handleChild) : null
  return React.Children.map(children, handleChild)
}

const Test = function () {
  const [count, setCount] = useState(0);

  const handleClick = useCallback(() => {
    setCount(count + 1);
  }, [count]);

  return (
    <div onClick={handleClick}>
      test
      <MyWrapper auth="home.detail.edit">
        <button>测试权限</button>
      </MyWrapper>
      <MyWrapper auth="home.detail.add">
        <button>测试权限不可</button>
      </MyWrapper>
      <MyWrapper auth="home.detail.edit">
        <input placeholder="测试可输入" />
      </MyWrapper>
      <MyWrapper auth="home.detail.add">
        <input placeholder="不可输入" />
      </MyWrapper>
      <MyWrapper auth="home.detail.edit">
        <select>
          <option value="volvo" auth="home.detail.edit">Volvo</option>
          <option value="saab" auth="home.detail.edit">Saab</option>
          <option value="opel" auth="home.detail.add">Opel</option>
          <option value="audi" auth="home.detail.add">Audi</option>
        </select>
      </MyWrapper>
      <MyWrapper auth="home.detail.add">
        <select>
          <option value="volvo" auth="home.detail.edit">Volvo</option>
          <option value="saab" auth="home.detail.edit">Saab</option>
          <option value="opel" auth="home.detail.add">Opel</option>
          <option value="audi" auth="home.detail.add">Audi</option>
        </select>
      </MyWrapper>
    </div>
  );
};

ReactDom.render(<Test />, document.getElementById('app'));
      
      
相关推荐
孟陬7 分钟前
Foobar 技术周刊 #003:黄仁勋的三段吃瓜和回归,抛开事实不谈(嘿嘿)让全人类 AGI 进程得以继续
前端·后端·程序员
涛涛ing12 分钟前
别再用 AI 写计算逻辑了:你的页面卡死,连鼠标都点不动
前端
OpenTiny社区1 小时前
当 AI 开始写 UI,我们缺的不是模型,是"设计词汇"
前端·github·ai编程
Amos_Web2 小时前
Rspack 源码解析(三十八):SubresourceIntegrityPlugin 如何跨四个插件协作生成 integrity
前端
Amos_Web2 小时前
通过 Rspack 源码学 Rust(八):struct 保存数据,impl 定义怎样使用数据
前端
anyup2 小时前
2027 年初,uni-app App 端停更倒计时,老项目该何去何从?
前端·vue.js·uni-app
JudithHuang4 小时前
Vue3 Vitest 单元测试进阶
前端·单元测试·log4j·vue
费曼学习法4 小时前
把 Agent 可观测性做进 Harness:从日志里只有 task finished 到全链路回放
前端
GuWenyue4 小时前
z-index 写到 9999 还是被盖住:把层叠上下文的 6 个反例全复现了一遍
前端·css·浏览器
htzyl2064 小时前
TypeScript:tsconfig配置误区及详情介绍,让你清清楚楚
前端·typescript