CardPro.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import React, { useState } from 'react';
  2. import { Card, Divider, Typography, Button } from '@douyinfe/semi-ui';
  3. import PropTypes from 'prop-types';
  4. import { useIsMobile } from '../../../hooks/common/useIsMobile';
  5. import { IconEyeOpened, IconEyeClosed } from '@douyinfe/semi-icons';
  6. const { Text } = Typography;
  7. /**
  8. * CardPro 高级卡片组件
  9. *
  10. * 布局分为5个区域:
  11. * 1. 统计信息区域 (statsArea)
  12. * 2. 描述信息区域 (descriptionArea)
  13. * 3. 类型切换/标签区域 (tabsArea)
  14. * 4. 操作按钮区域 (actionsArea)
  15. * 5. 搜索表单区域 (searchArea)
  16. *
  17. * 支持三种布局类型:
  18. * - type1: 操作型 (如TokensTable) - 描述信息 + 操作按钮 + 搜索表单
  19. * - type2: 查询型 (如LogsTable) - 统计信息 + 搜索表单
  20. * - type3: 复杂型 (如ChannelsTable) - 描述信息 + 类型切换 + 操作按钮 + 搜索表单
  21. */
  22. const CardPro = ({
  23. type = 'type1',
  24. className = '',
  25. children,
  26. // 各个区域的内容
  27. statsArea,
  28. descriptionArea,
  29. tabsArea,
  30. actionsArea,
  31. searchArea,
  32. // 卡片属性
  33. shadows = 'always',
  34. bordered = false,
  35. // 自定义样式
  36. style,
  37. // 国际化函数
  38. t = (key) => key, // 默认函数,直接返回key
  39. ...props
  40. }) => {
  41. const isMobile = useIsMobile();
  42. const [showMobileActions, setShowMobileActions] = useState(false);
  43. // 切换移动端操作项显示状态
  44. const toggleMobileActions = () => {
  45. setShowMobileActions(!showMobileActions);
  46. };
  47. // 检查是否有需要在移动端隐藏的内容
  48. const hasMobileHideableContent = actionsArea || searchArea;
  49. // 渲染头部内容
  50. const renderHeader = () => {
  51. const hasContent = statsArea || descriptionArea || tabsArea || actionsArea || searchArea;
  52. if (!hasContent) return null;
  53. return (
  54. <div className="flex flex-col w-full">
  55. {/* 统计信息区域 - 用于type2 */}
  56. {type === 'type2' && statsArea && (
  57. <>
  58. {statsArea}
  59. </>
  60. )}
  61. {/* 描述信息区域 - 用于type1和type3 */}
  62. {(type === 'type1' || type === 'type3') && descriptionArea && (
  63. <>
  64. {descriptionArea}
  65. </>
  66. )}
  67. {/* 第一个分隔线 - 在描述信息或统计信息后面 */}
  68. {((type === 'type1' || type === 'type3') && descriptionArea) ||
  69. (type === 'type2' && statsArea) ? (
  70. <Divider margin="12px" />
  71. ) : null}
  72. {/* 类型切换/标签区域 - 主要用于type3 */}
  73. {type === 'type3' && tabsArea && (
  74. <>
  75. {tabsArea}
  76. </>
  77. )}
  78. {/* 移动端操作切换按钮 */}
  79. {isMobile && hasMobileHideableContent && (
  80. <>
  81. <div className="w-full mb-2">
  82. <Button
  83. onClick={toggleMobileActions}
  84. icon={showMobileActions ? <IconEyeClosed /> : <IconEyeOpened />}
  85. type="tertiary"
  86. size="small"
  87. block
  88. >
  89. {showMobileActions ? t('隐藏操作项') : t('显示操作项')}
  90. </Button>
  91. </div>
  92. </>
  93. )}
  94. {/* 操作按钮和搜索表单的容器 */}
  95. {/* 在移动端时根据showMobileActions状态控制显示,在桌面端时始终显示 */}
  96. {(!isMobile || showMobileActions) && (
  97. <div className="flex flex-col gap-2">
  98. {/* 操作按钮区域 - 用于type1和type3 */}
  99. {(type === 'type1' || type === 'type3') && actionsArea && (
  100. <div className="w-full">
  101. {actionsArea}
  102. </div>
  103. )}
  104. {/* 搜索表单区域 - 所有类型都可能有 */}
  105. {searchArea && (
  106. <div className="w-full">
  107. {searchArea}
  108. </div>
  109. )}
  110. </div>
  111. )}
  112. </div>
  113. );
  114. };
  115. const headerContent = renderHeader();
  116. return (
  117. <Card
  118. className={`table-scroll-card !rounded-2xl ${className}`}
  119. title={headerContent}
  120. shadows={shadows}
  121. bordered={bordered}
  122. style={style}
  123. {...props}
  124. >
  125. {children}
  126. </Card>
  127. );
  128. };
  129. CardPro.propTypes = {
  130. // 布局类型
  131. type: PropTypes.oneOf(['type1', 'type2', 'type3']),
  132. // 样式相关
  133. className: PropTypes.string,
  134. style: PropTypes.object,
  135. shadows: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
  136. bordered: PropTypes.bool,
  137. // 内容区域
  138. statsArea: PropTypes.node,
  139. descriptionArea: PropTypes.node,
  140. tabsArea: PropTypes.node,
  141. actionsArea: PropTypes.node,
  142. searchArea: PropTypes.node,
  143. // 表格内容
  144. children: PropTypes.node,
  145. // 国际化函数
  146. t: PropTypes.func,
  147. };
  148. export default CardPro;