CardPro.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. Copyright (C) 2025 QuantumNous
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial licensing, please contact support@quantumnous.com
  14. */
  15. import React, { useState } from 'react';
  16. import { Card, Divider, Typography, Button } from '@douyinfe/semi-ui';
  17. import PropTypes from 'prop-types';
  18. import { useIsMobile } from '../../../hooks/common/useIsMobile';
  19. import { IconEyeOpened, IconEyeClosed } from '@douyinfe/semi-icons';
  20. const { Text } = Typography;
  21. /**
  22. * CardPro 高级卡片组件
  23. *
  24. * 布局分为5个区域:
  25. * 1. 统计信息区域 (statsArea)
  26. * 2. 描述信息区域 (descriptionArea)
  27. * 3. 类型切换/标签区域 (tabsArea)
  28. * 4. 操作按钮区域 (actionsArea)
  29. * 5. 搜索表单区域 (searchArea)
  30. *
  31. * 支持三种布局类型:
  32. * - type1: 操作型 (如TokensTable) - 描述信息 + 操作按钮 + 搜索表单
  33. * - type2: 查询型 (如LogsTable) - 统计信息 + 搜索表单
  34. * - type3: 复杂型 (如ChannelsTable) - 描述信息 + 类型切换 + 操作按钮 + 搜索表单
  35. */
  36. const CardPro = ({
  37. type = 'type1',
  38. className = '',
  39. children,
  40. // 各个区域的内容
  41. statsArea,
  42. descriptionArea,
  43. tabsArea,
  44. actionsArea,
  45. searchArea,
  46. // 卡片属性
  47. shadows = 'always',
  48. bordered = false,
  49. // 自定义样式
  50. style,
  51. // 国际化函数
  52. t = (key) => key, // 默认函数,直接返回key
  53. ...props
  54. }) => {
  55. const isMobile = useIsMobile();
  56. const [showMobileActions, setShowMobileActions] = useState(false);
  57. // 切换移动端操作项显示状态
  58. const toggleMobileActions = () => {
  59. setShowMobileActions(!showMobileActions);
  60. };
  61. // 检查是否有需要在移动端隐藏的内容
  62. const hasMobileHideableContent = actionsArea || searchArea;
  63. // 渲染头部内容
  64. const renderHeader = () => {
  65. const hasContent = statsArea || descriptionArea || tabsArea || actionsArea || searchArea;
  66. if (!hasContent) return null;
  67. return (
  68. <div className="flex flex-col w-full">
  69. {/* 统计信息区域 - 用于type2 */}
  70. {type === 'type2' && statsArea && (
  71. <>
  72. {statsArea}
  73. </>
  74. )}
  75. {/* 描述信息区域 - 用于type1和type3 */}
  76. {(type === 'type1' || type === 'type3') && descriptionArea && (
  77. <>
  78. {descriptionArea}
  79. </>
  80. )}
  81. {/* 第一个分隔线 - 在描述信息或统计信息后面 */}
  82. {((type === 'type1' || type === 'type3') && descriptionArea) ||
  83. (type === 'type2' && statsArea) ? (
  84. <Divider margin="12px" />
  85. ) : null}
  86. {/* 类型切换/标签区域 - 主要用于type3 */}
  87. {type === 'type3' && tabsArea && (
  88. <>
  89. {tabsArea}
  90. </>
  91. )}
  92. {/* 移动端操作切换按钮 */}
  93. {isMobile && hasMobileHideableContent && (
  94. <>
  95. <div className="w-full mb-2">
  96. <Button
  97. onClick={toggleMobileActions}
  98. icon={showMobileActions ? <IconEyeClosed /> : <IconEyeOpened />}
  99. type="tertiary"
  100. size="small"
  101. block
  102. >
  103. {showMobileActions ? t('隐藏操作项') : t('显示操作项')}
  104. </Button>
  105. </div>
  106. </>
  107. )}
  108. {/* 操作按钮和搜索表单的容器 */}
  109. <div
  110. className={`flex flex-col gap-2 ${isMobile && !showMobileActions ? 'hidden' : ''}`}
  111. >
  112. {/* 操作按钮区域 - 用于type1和type3 */}
  113. {(type === 'type1' || type === 'type3') && actionsArea && (
  114. <div className="w-full">
  115. {actionsArea}
  116. </div>
  117. )}
  118. {/* 搜索表单区域 - 所有类型都可能有 */}
  119. {searchArea && (
  120. <div className="w-full">
  121. {searchArea}
  122. </div>
  123. )}
  124. </div>
  125. </div>
  126. );
  127. };
  128. const headerContent = renderHeader();
  129. return (
  130. <Card
  131. className={`table-scroll-card !rounded-2xl ${className}`}
  132. title={headerContent}
  133. shadows={shadows}
  134. bordered={bordered}
  135. style={style}
  136. {...props}
  137. >
  138. {children}
  139. </Card>
  140. );
  141. };
  142. CardPro.propTypes = {
  143. // 布局类型
  144. type: PropTypes.oneOf(['type1', 'type2', 'type3']),
  145. // 样式相关
  146. className: PropTypes.string,
  147. style: PropTypes.object,
  148. shadows: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
  149. bordered: PropTypes.bool,
  150. // 内容区域
  151. statsArea: PropTypes.node,
  152. descriptionArea: PropTypes.node,
  153. tabsArea: PropTypes.node,
  154. actionsArea: PropTypes.node,
  155. searchArea: PropTypes.node,
  156. // 表格内容
  157. children: PropTypes.node,
  158. // 国际化函数
  159. t: PropTypes.func,
  160. };
  161. export default CardPro;