CardPro.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. theme='outline'
  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. Array.isArray(actionsArea) ? (
  115. actionsArea.map((area, idx) => (
  116. <React.Fragment key={idx}>
  117. {idx !== 0 && <Divider />}
  118. <div className="w-full">
  119. {area}
  120. </div>
  121. </React.Fragment>
  122. ))
  123. ) : (
  124. <div className="w-full">
  125. {actionsArea}
  126. </div>
  127. )
  128. )}
  129. {/* 当同时存在操作区和搜索区时,插入分隔线 */}
  130. {(actionsArea && searchArea) && <Divider />}
  131. {/* 搜索表单区域 - 所有类型都可能有 */}
  132. {searchArea && (
  133. <div className="w-full">
  134. {searchArea}
  135. </div>
  136. )}
  137. </div>
  138. </div>
  139. );
  140. };
  141. const headerContent = renderHeader();
  142. // 渲染分页区域
  143. const renderFooter = () => {
  144. if (!paginationArea) return null;
  145. return (
  146. <div
  147. className={`flex w-full pt-4 border-t ${isMobile ? 'justify-center' : 'justify-between items-center'}`}
  148. style={{ borderColor: 'var(--semi-color-border)' }}
  149. >
  150. {paginationArea}
  151. </div>
  152. );
  153. };
  154. const footerContent = renderFooter();
  155. return (
  156. <Card
  157. className={`table-scroll-card !rounded-2xl ${className}`}
  158. title={headerContent}
  159. footer={footerContent}
  160. shadows={shadows}
  161. bordered={bordered}
  162. style={style}
  163. {...props}
  164. >
  165. {children}
  166. </Card>
  167. );
  168. };
  169. CardPro.propTypes = {
  170. // 布局类型
  171. type: PropTypes.oneOf(['type1', 'type2', 'type3']),
  172. // 样式相关
  173. className: PropTypes.string,
  174. style: PropTypes.object,
  175. shadows: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
  176. bordered: PropTypes.bool,
  177. // 内容区域
  178. statsArea: PropTypes.node,
  179. descriptionArea: PropTypes.node,
  180. tabsArea: PropTypes.node,
  181. actionsArea: PropTypes.oneOfType([
  182. PropTypes.node,
  183. PropTypes.arrayOf(PropTypes.node),
  184. ]),
  185. searchArea: PropTypes.node,
  186. paginationArea: PropTypes.node,
  187. // 表格内容
  188. children: PropTypes.node,
  189. // 国际化函数
  190. t: PropTypes.func,
  191. };
  192. export default CardPro;