CardPro.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. * 布局分为6个区域:
  25. * 1. 统计信息区域 (statsArea)
  26. * 2. 描述信息区域 (descriptionArea)
  27. * 3. 类型切换/标签区域 (tabsArea)
  28. * 4. 操作按钮区域 (actionsArea)
  29. * 5. 搜索表单区域 (searchArea)
  30. * 6. 分页区域 (paginationArea) - 固定在卡片底部
  31. *
  32. * 支持三种布局类型:
  33. * - type1: 操作型 (如TokensTable) - 描述信息 + 操作按钮 + 搜索表单
  34. * - type2: 查询型 (如LogsTable) - 统计信息 + 搜索表单
  35. * - type3: 复杂型 (如ChannelsTable) - 描述信息 + 类型切换 + 操作按钮 + 搜索表单
  36. */
  37. const CardPro = ({
  38. type = 'type1',
  39. className = '',
  40. children,
  41. // 各个区域的内容
  42. statsArea,
  43. descriptionArea,
  44. tabsArea,
  45. actionsArea,
  46. searchArea,
  47. paginationArea, // 新增分页区域
  48. // 卡片属性
  49. shadows = 'always',
  50. bordered = false,
  51. // 自定义样式
  52. style,
  53. // 国际化函数
  54. t = (key) => key,
  55. ...props
  56. }) => {
  57. const isMobile = useIsMobile();
  58. const [showMobileActions, setShowMobileActions] = useState(false);
  59. const toggleMobileActions = () => {
  60. setShowMobileActions(!showMobileActions);
  61. };
  62. const hasMobileHideableContent = actionsArea || searchArea;
  63. const renderHeader = () => {
  64. const hasContent = statsArea || descriptionArea || tabsArea || actionsArea || searchArea;
  65. if (!hasContent) return null;
  66. return (
  67. <div className="flex flex-col w-full">
  68. {/* 统计信息区域 - 用于type2 */}
  69. {type === 'type2' && statsArea && (
  70. <>
  71. {statsArea}
  72. </>
  73. )}
  74. {/* 描述信息区域 - 用于type1和type3 */}
  75. {(type === 'type1' || type === 'type3') && descriptionArea && (
  76. <>
  77. {descriptionArea}
  78. </>
  79. )}
  80. {/* 第一个分隔线 - 在描述信息或统计信息后面 */}
  81. {((type === 'type1' || type === 'type3') && descriptionArea) ||
  82. (type === 'type2' && statsArea) ? (
  83. <Divider margin="12px" />
  84. ) : null}
  85. {/* 类型切换/标签区域 - 主要用于type3 */}
  86. {type === 'type3' && tabsArea && (
  87. <>
  88. {tabsArea}
  89. </>
  90. )}
  91. {/* 移动端操作切换按钮 */}
  92. {isMobile && hasMobileHideableContent && (
  93. <>
  94. <div className="w-full mb-2">
  95. <Button
  96. onClick={toggleMobileActions}
  97. icon={showMobileActions ? <IconEyeClosed /> : <IconEyeOpened />}
  98. type="tertiary"
  99. size="small"
  100. block
  101. >
  102. {showMobileActions ? t('隐藏操作项') : t('显示操作项')}
  103. </Button>
  104. </div>
  105. </>
  106. )}
  107. {/* 操作按钮和搜索表单的容器 */}
  108. <div
  109. className={`flex flex-col gap-2 ${isMobile && !showMobileActions ? 'hidden' : ''}`}
  110. >
  111. {/* 操作按钮区域 - 用于type1和type3 */}
  112. {(type === 'type1' || type === 'type3') && actionsArea && (
  113. Array.isArray(actionsArea) ? (
  114. actionsArea.map((area, idx) => (
  115. <React.Fragment key={idx}>
  116. {idx !== 0 && <Divider />}
  117. <div className="w-full">
  118. {area}
  119. </div>
  120. </React.Fragment>
  121. ))
  122. ) : (
  123. <div className="w-full">
  124. {actionsArea}
  125. </div>
  126. )
  127. )}
  128. {/* 当同时存在操作区和搜索区时,插入分隔线 */}
  129. {(actionsArea && searchArea) && <Divider />}
  130. {/* 搜索表单区域 - 所有类型都可能有 */}
  131. {searchArea && (
  132. <div className="w-full">
  133. {searchArea}
  134. </div>
  135. )}
  136. </div>
  137. </div>
  138. );
  139. };
  140. const headerContent = renderHeader();
  141. // 渲染分页区域
  142. const renderFooter = () => {
  143. if (!paginationArea) return null;
  144. return (
  145. <div
  146. className={`flex w-full pt-4 border-t ${isMobile ? 'justify-center' : 'justify-between items-center'}`}
  147. style={{ borderColor: 'var(--semi-color-border)' }}
  148. >
  149. {paginationArea}
  150. </div>
  151. );
  152. };
  153. const footerContent = renderFooter();
  154. return (
  155. <Card
  156. className={`table-scroll-card !rounded-2xl ${className}`}
  157. title={headerContent}
  158. footer={footerContent}
  159. shadows={shadows}
  160. bordered={bordered}
  161. style={style}
  162. {...props}
  163. >
  164. {children}
  165. </Card>
  166. );
  167. };
  168. CardPro.propTypes = {
  169. // 布局类型
  170. type: PropTypes.oneOf(['type1', 'type2', 'type3']),
  171. // 样式相关
  172. className: PropTypes.string,
  173. style: PropTypes.object,
  174. shadows: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
  175. bordered: PropTypes.bool,
  176. // 内容区域
  177. statsArea: PropTypes.node,
  178. descriptionArea: PropTypes.node,
  179. tabsArea: PropTypes.node,
  180. actionsArea: PropTypes.oneOfType([
  181. PropTypes.node,
  182. PropTypes.arrayOf(PropTypes.node),
  183. ]),
  184. searchArea: PropTypes.node,
  185. paginationArea: PropTypes.node,
  186. // 表格内容
  187. children: PropTypes.node,
  188. // 国际化函数
  189. t: PropTypes.func,
  190. };
  191. export default CardPro;