index.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import React, { ReactNode } from "react";
  2. import { Button } from "antd";
  3. import { useSelector } from "react-redux";
  4. import { RootState } from "@/store";
  5. type Props = {
  6. children?: ReactNode;
  7. id: number;
  8. danger?: boolean; //设置危险按钮
  9. block?: boolean; //将按钮宽度调整为其父宽度的选项
  10. disabled?: boolean; //设置按钮失效状态
  11. ghost?: boolean; //ghost
  12. href?: string; //点击跳转的地址,指定此属性 button 的行为和 a 链接一致
  13. htmlType?: "button" | "submit" | "reset" | undefined; //设置 button 原生的 type 值,可选值请参考 HTML 标准
  14. icon?: ReactNode; //设置按钮的图标组件
  15. loading?: boolean; //设置按钮载入状态
  16. shape?: "default" | "circle" | "round"; //设置按钮形状
  17. size?: "large" | "middle" | "small"; //设置按钮大小
  18. target?: string; //相当于 a 链接的 target 属性,href 存在时生效
  19. type?: "primary" | "ghost" | "dashed" | "link" | "text" | "default"; //设置按钮类型
  20. onClick?: () => void; //点击按钮时的回调
  21. };
  22. // console.log('有权限的按钮集合',buttonArr);
  23. function AuthButton({
  24. children,
  25. id,
  26. danger = false,
  27. block = false,
  28. disabled = false,
  29. ghost = false,
  30. href,
  31. htmlType = "button",
  32. icon,
  33. loading = false,
  34. shape = "default",
  35. size = "middle",
  36. target,
  37. type = "default",
  38. onClick,
  39. }: Props) {
  40. const buttonArr = useSelector(
  41. (state: RootState) => state.loginStore.authButtonArr
  42. );
  43. return buttonArr.some((v: any) => v.id === id) || id === -1 ? (
  44. <Button
  45. danger={danger}
  46. block={block}
  47. disabled={disabled}
  48. ghost={ghost}
  49. href={href}
  50. htmlType={htmlType}
  51. icon={icon}
  52. loading={loading}
  53. shape={shape}
  54. size={size}
  55. target={target}
  56. type={type}
  57. onClick={onClick}
  58. >
  59. {children}
  60. </Button>
  61. ) : null;
  62. }
  63. const MemoAuthButton = React.memo(AuthButton);
  64. export default MemoAuthButton;