index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import { unit } from '@ant-design/cssinjs';
  2. import { FastColor } from '@ant-design/fast-color';
  3. import { clearFix, resetComponent } from '../../style';
  4. import { genStyleHooks, mergeToken } from '../../theme/internal';
  5. import genBorderedStyle from './bordered';
  6. import genEllipsisStyle from './ellipsis';
  7. import genEmptyStyle from './empty';
  8. import genExpandStyle from './expand';
  9. import genFilterStyle from './filter';
  10. import genFixedStyle from './fixed';
  11. import genPaginationStyle from './pagination';
  12. import genRadiusStyle from './radius';
  13. import genRtlStyle from './rtl';
  14. import genSelectionStyle from './selection';
  15. import genSizeStyle from './size';
  16. import genSorterStyle from './sorter';
  17. import genStickyStyle from './sticky';
  18. import genSummaryStyle from './summary';
  19. import genVirtualStyle from './virtual';
  20. const genTableStyle = token => {
  21. const {
  22. componentCls,
  23. fontWeightStrong,
  24. tablePaddingVertical,
  25. tablePaddingHorizontal,
  26. tableExpandColumnWidth,
  27. lineWidth,
  28. lineType,
  29. tableBorderColor,
  30. tableFontSize,
  31. tableBg,
  32. tableRadius,
  33. tableHeaderTextColor,
  34. motionDurationMid,
  35. tableHeaderBg,
  36. tableHeaderCellSplitColor,
  37. tableFooterTextColor,
  38. tableFooterBg,
  39. calc
  40. } = token;
  41. const tableBorder = `${unit(lineWidth)} ${lineType} ${tableBorderColor}`;
  42. return {
  43. [`${componentCls}-wrapper`]: Object.assign(Object.assign({
  44. clear: 'both',
  45. maxWidth: '100%',
  46. // fix https://github.com/ant-design/ant-design/issues/46177
  47. ['--rc-virtual-list-scrollbar-bg']: token.tableScrollBg
  48. }, clearFix()), {
  49. [componentCls]: Object.assign(Object.assign({}, resetComponent(token)), {
  50. fontSize: tableFontSize,
  51. background: tableBg,
  52. borderRadius: `${unit(tableRadius)} ${unit(tableRadius)} 0 0`,
  53. // https://github.com/ant-design/ant-design/issues/47486
  54. scrollbarColor: `${token.tableScrollThumbBg} ${token.tableScrollBg}`
  55. }),
  56. // https://github.com/ant-design/ant-design/issues/17611
  57. table: {
  58. width: '100%',
  59. textAlign: 'start',
  60. borderRadius: `${unit(tableRadius)} ${unit(tableRadius)} 0 0`,
  61. borderCollapse: 'separate',
  62. borderSpacing: 0
  63. },
  64. // ============================= Cell ==============================
  65. [`
  66. ${componentCls}-cell,
  67. ${componentCls}-thead > tr > th,
  68. ${componentCls}-tbody > tr > th,
  69. ${componentCls}-tbody > tr > td,
  70. tfoot > tr > th,
  71. tfoot > tr > td
  72. `]: {
  73. position: 'relative',
  74. padding: `${unit(tablePaddingVertical)} ${unit(tablePaddingHorizontal)}`,
  75. overflowWrap: 'break-word'
  76. },
  77. // ============================ Title =============================
  78. [`${componentCls}-title`]: {
  79. padding: `${unit(tablePaddingVertical)} ${unit(tablePaddingHorizontal)}`
  80. },
  81. // ============================ Header ============================
  82. [`${componentCls}-thead`]: {
  83. [`
  84. > tr > th,
  85. > tr > td
  86. `]: {
  87. position: 'relative',
  88. color: tableHeaderTextColor,
  89. fontWeight: fontWeightStrong,
  90. textAlign: 'start',
  91. background: tableHeaderBg,
  92. borderBottom: tableBorder,
  93. transition: `background ${motionDurationMid} ease`,
  94. "&[colspan]:not([colspan='1'])": {
  95. textAlign: 'center'
  96. },
  97. [`&:not(:last-child):not(${componentCls}-selection-column):not(${componentCls}-row-expand-icon-cell):not([colspan])::before`]: {
  98. position: 'absolute',
  99. top: '50%',
  100. insetInlineEnd: 0,
  101. width: 1,
  102. height: '1.6em',
  103. backgroundColor: tableHeaderCellSplitColor,
  104. transform: 'translateY(-50%)',
  105. transition: `background-color ${motionDurationMid}`,
  106. content: '""'
  107. }
  108. },
  109. '> tr:not(:last-child) > th[colspan]': {
  110. borderBottom: 0
  111. }
  112. },
  113. // ============================ Body ============================
  114. [`${componentCls}-tbody`]: {
  115. '> tr': {
  116. '> th, > td': {
  117. transition: `background ${motionDurationMid}, border-color ${motionDurationMid}`,
  118. borderBottom: tableBorder,
  119. // ========================= Nest Table ===========================
  120. [`
  121. > ${componentCls}-wrapper:only-child,
  122. > ${componentCls}-expanded-row-fixed > ${componentCls}-wrapper:only-child
  123. `]: {
  124. [componentCls]: {
  125. marginBlock: unit(calc(tablePaddingVertical).mul(-1).equal()),
  126. marginInline: `${unit(calc(tableExpandColumnWidth).sub(tablePaddingHorizontal).equal())}
  127. ${unit(calc(tablePaddingHorizontal).mul(-1).equal())}`,
  128. [`${componentCls}-tbody > tr:last-child > td`]: {
  129. borderBottomWidth: 0,
  130. '&:first-child, &:last-child': {
  131. borderRadius: 0
  132. }
  133. }
  134. }
  135. }
  136. },
  137. '> th': {
  138. position: 'relative',
  139. color: tableHeaderTextColor,
  140. fontWeight: fontWeightStrong,
  141. textAlign: 'start',
  142. background: tableHeaderBg,
  143. borderBottom: tableBorder,
  144. transition: `background ${motionDurationMid} ease`
  145. }
  146. }
  147. },
  148. // ============================ Footer ============================
  149. [`${componentCls}-footer`]: {
  150. padding: `${unit(tablePaddingVertical)} ${unit(tablePaddingHorizontal)}`,
  151. color: tableFooterTextColor,
  152. background: tableFooterBg
  153. }
  154. })
  155. };
  156. };
  157. export const prepareComponentToken = token => {
  158. const {
  159. colorFillAlter,
  160. colorBgContainer,
  161. colorTextHeading,
  162. colorFillSecondary,
  163. colorFillContent,
  164. controlItemBgActive,
  165. controlItemBgActiveHover,
  166. padding,
  167. paddingSM,
  168. paddingXS,
  169. colorBorderSecondary,
  170. borderRadiusLG,
  171. controlHeight,
  172. colorTextPlaceholder,
  173. fontSize,
  174. fontSizeSM,
  175. lineHeight,
  176. lineWidth,
  177. colorIcon,
  178. colorIconHover,
  179. opacityLoading,
  180. controlInteractiveSize
  181. } = token;
  182. const colorFillSecondarySolid = new FastColor(colorFillSecondary).onBackground(colorBgContainer).toHexString();
  183. const colorFillContentSolid = new FastColor(colorFillContent).onBackground(colorBgContainer).toHexString();
  184. const colorFillAlterSolid = new FastColor(colorFillAlter).onBackground(colorBgContainer).toHexString();
  185. const baseColorAction = new FastColor(colorIcon);
  186. const baseColorActionHover = new FastColor(colorIconHover);
  187. const expandIconHalfInner = controlInteractiveSize / 2 - lineWidth;
  188. const expandIconSize = expandIconHalfInner * 2 + lineWidth * 3;
  189. return {
  190. headerBg: colorFillAlterSolid,
  191. headerColor: colorTextHeading,
  192. headerSortActiveBg: colorFillSecondarySolid,
  193. headerSortHoverBg: colorFillContentSolid,
  194. bodySortBg: colorFillAlterSolid,
  195. rowHoverBg: colorFillAlterSolid,
  196. rowSelectedBg: controlItemBgActive,
  197. rowSelectedHoverBg: controlItemBgActiveHover,
  198. rowExpandedBg: colorFillAlter,
  199. cellPaddingBlock: padding,
  200. cellPaddingInline: padding,
  201. cellPaddingBlockMD: paddingSM,
  202. cellPaddingInlineMD: paddingXS,
  203. cellPaddingBlockSM: paddingXS,
  204. cellPaddingInlineSM: paddingXS,
  205. borderColor: colorBorderSecondary,
  206. headerBorderRadius: borderRadiusLG,
  207. footerBg: colorFillAlterSolid,
  208. footerColor: colorTextHeading,
  209. cellFontSize: fontSize,
  210. cellFontSizeMD: fontSize,
  211. cellFontSizeSM: fontSize,
  212. headerSplitColor: colorBorderSecondary,
  213. fixedHeaderSortActiveBg: colorFillSecondarySolid,
  214. headerFilterHoverBg: colorFillContent,
  215. filterDropdownMenuBg: colorBgContainer,
  216. filterDropdownBg: colorBgContainer,
  217. expandIconBg: colorBgContainer,
  218. selectionColumnWidth: controlHeight,
  219. stickyScrollBarBg: colorTextPlaceholder,
  220. stickyScrollBarBorderRadius: 100,
  221. expandIconMarginTop: (fontSize * lineHeight - lineWidth * 3) / 2 - Math.ceil((fontSizeSM * 1.4 - lineWidth * 3) / 2),
  222. headerIconColor: baseColorAction.clone().setA(baseColorAction.a * opacityLoading).toRgbString(),
  223. headerIconHoverColor: baseColorActionHover.clone().setA(baseColorActionHover.a * opacityLoading).toRgbString(),
  224. expandIconHalfInner,
  225. expandIconSize,
  226. expandIconScale: controlInteractiveSize / expandIconSize
  227. };
  228. };
  229. const zIndexTableFixed = 2;
  230. // ============================== Export ==============================
  231. export default genStyleHooks('Table', token => {
  232. const {
  233. colorTextHeading,
  234. colorSplit,
  235. colorBgContainer,
  236. controlInteractiveSize: checkboxSize,
  237. headerBg,
  238. headerColor,
  239. headerSortActiveBg,
  240. headerSortHoverBg,
  241. bodySortBg,
  242. rowHoverBg,
  243. rowSelectedBg,
  244. rowSelectedHoverBg,
  245. rowExpandedBg,
  246. cellPaddingBlock,
  247. cellPaddingInline,
  248. cellPaddingBlockMD,
  249. cellPaddingInlineMD,
  250. cellPaddingBlockSM,
  251. cellPaddingInlineSM,
  252. borderColor,
  253. footerBg,
  254. footerColor,
  255. headerBorderRadius,
  256. cellFontSize,
  257. cellFontSizeMD,
  258. cellFontSizeSM,
  259. headerSplitColor,
  260. fixedHeaderSortActiveBg,
  261. headerFilterHoverBg,
  262. filterDropdownBg,
  263. expandIconBg,
  264. selectionColumnWidth,
  265. stickyScrollBarBg,
  266. calc
  267. } = token;
  268. const tableToken = mergeToken(token, {
  269. tableFontSize: cellFontSize,
  270. tableBg: colorBgContainer,
  271. tableRadius: headerBorderRadius,
  272. tablePaddingVertical: cellPaddingBlock,
  273. tablePaddingHorizontal: cellPaddingInline,
  274. tablePaddingVerticalMiddle: cellPaddingBlockMD,
  275. tablePaddingHorizontalMiddle: cellPaddingInlineMD,
  276. tablePaddingVerticalSmall: cellPaddingBlockSM,
  277. tablePaddingHorizontalSmall: cellPaddingInlineSM,
  278. tableBorderColor: borderColor,
  279. tableHeaderTextColor: headerColor,
  280. tableHeaderBg: headerBg,
  281. tableFooterTextColor: footerColor,
  282. tableFooterBg: footerBg,
  283. tableHeaderCellSplitColor: headerSplitColor,
  284. tableHeaderSortBg: headerSortActiveBg,
  285. tableHeaderSortHoverBg: headerSortHoverBg,
  286. tableBodySortBg: bodySortBg,
  287. tableFixedHeaderSortActiveBg: fixedHeaderSortActiveBg,
  288. tableHeaderFilterActiveBg: headerFilterHoverBg,
  289. tableFilterDropdownBg: filterDropdownBg,
  290. tableRowHoverBg: rowHoverBg,
  291. tableSelectedRowBg: rowSelectedBg,
  292. tableSelectedRowHoverBg: rowSelectedHoverBg,
  293. zIndexTableFixed,
  294. zIndexTableSticky: calc(zIndexTableFixed).add(1).equal({
  295. unit: false
  296. }),
  297. tableFontSizeMiddle: cellFontSizeMD,
  298. tableFontSizeSmall: cellFontSizeSM,
  299. tableSelectionColumnWidth: selectionColumnWidth,
  300. tableExpandIconBg: expandIconBg,
  301. tableExpandColumnWidth: calc(checkboxSize).add(calc(token.padding).mul(2)).equal(),
  302. tableExpandedRowBg: rowExpandedBg,
  303. // Dropdown
  304. tableFilterDropdownWidth: 120,
  305. tableFilterDropdownHeight: 264,
  306. tableFilterDropdownSearchWidth: 140,
  307. // Virtual Scroll Bar
  308. tableScrollThumbSize: 8,
  309. // Mac scroll bar size
  310. tableScrollThumbBg: stickyScrollBarBg,
  311. tableScrollThumbBgHover: colorTextHeading,
  312. tableScrollBg: colorSplit
  313. });
  314. return [genTableStyle(tableToken), genPaginationStyle(tableToken), genSummaryStyle(tableToken), genSorterStyle(tableToken), genFilterStyle(tableToken), genBorderedStyle(tableToken), genRadiusStyle(tableToken), genExpandStyle(tableToken), genSummaryStyle(tableToken), genEmptyStyle(tableToken), genSelectionStyle(tableToken), genFixedStyle(tableToken), genStickyStyle(tableToken), genEllipsisStyle(tableToken), genSizeStyle(tableToken), genRtlStyle(tableToken), genVirtualStyle(tableToken)];
  315. }, prepareComponentToken, {
  316. unitless: {
  317. expandIconScale: true
  318. }
  319. });