廓形仪rn版本-技术调研
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
3.5 KiB

  1. import React, { createContext, useMemo, useContext } from 'react';
  2. import {
  3. tableStyle,
  4. tableHeaderStyle,
  5. tableBodyStyle,
  6. tableFooterStyle,
  7. tableHeadStyle,
  8. tableRowStyleStyle,
  9. tableDataStyle,
  10. tableCaptionStyle,
  11. } from './styles';
  12. const TableHeaderContext = createContext<{
  13. isHeaderRow: boolean;
  14. }>({
  15. isHeaderRow: false,
  16. });
  17. const TableFooterContext = createContext<{
  18. isFooterRow: boolean;
  19. }>({
  20. isFooterRow: false,
  21. });
  22. const Table = React.forwardRef<HTMLTableElement, React.ComponentProps<'table'>>(
  23. function Table({ className, ...props }, ref) {
  24. return (
  25. <table
  26. ref={ref}
  27. className={tableStyle({ class: className })}
  28. {...props}
  29. />
  30. );
  31. }
  32. );
  33. const TableHeader = React.forwardRef<
  34. HTMLTableSectionElement,
  35. React.ComponentProps<'thead'>
  36. >(function TableHeader({ className, ...props }, ref) {
  37. const contextValue = useMemo(() => {
  38. return {
  39. isHeaderRow: true,
  40. };
  41. }, []);
  42. return (
  43. <TableHeaderContext.Provider value={contextValue}>
  44. <thead
  45. ref={ref}
  46. className={tableHeaderStyle({ class: className })}
  47. {...props}
  48. />
  49. </TableHeaderContext.Provider>
  50. );
  51. });
  52. const TableBody = React.forwardRef<
  53. HTMLTableSectionElement,
  54. React.ComponentProps<'tbody'>
  55. >(function TableBody({ className, ...props }, ref) {
  56. return (
  57. <tbody
  58. ref={ref}
  59. className={tableBodyStyle({ class: className })}
  60. {...props}
  61. />
  62. );
  63. });
  64. const TableFooter = React.forwardRef<
  65. HTMLTableSectionElement,
  66. React.ComponentProps<'tfoot'>
  67. >(function TableFooter({ className, ...props }, ref) {
  68. const contextValue = useMemo(() => {
  69. return {
  70. isFooterRow: true,
  71. };
  72. }, []);
  73. return (
  74. <TableFooterContext.Provider value={contextValue}>
  75. <tfoot
  76. ref={ref}
  77. className={tableFooterStyle({ class: className })}
  78. {...props}
  79. />
  80. </TableFooterContext.Provider>
  81. );
  82. });
  83. const TableHead = React.forwardRef<
  84. HTMLTableCellElement,
  85. React.ComponentProps<'th'>
  86. >(function TableHead({ className, ...props }, ref) {
  87. return (
  88. <th ref={ref} className={tableHeadStyle({ class: className })} {...props} />
  89. );
  90. });
  91. const TableRow = React.forwardRef<
  92. HTMLTableRowElement,
  93. React.ComponentProps<'tr'>
  94. >(function TableRow({ className, ...props }, ref) {
  95. const { isHeaderRow } = useContext(TableHeaderContext);
  96. const { isFooterRow } = useContext(TableFooterContext);
  97. return (
  98. <tr
  99. ref={ref}
  100. className={tableRowStyleStyle({
  101. isHeaderRow,
  102. isFooterRow,
  103. class: className,
  104. })}
  105. {...props}
  106. />
  107. );
  108. });
  109. const TableData = React.forwardRef<
  110. HTMLTableCellElement,
  111. React.ComponentProps<'td'>
  112. >(function TableData({ className, ...props }, ref) {
  113. return (
  114. <td ref={ref} className={tableDataStyle({ class: className })} {...props} />
  115. );
  116. });
  117. const TableCaption = React.forwardRef<
  118. HTMLTableCaptionElement,
  119. React.ComponentProps<'caption'>
  120. >(function TableCaption({ className, ...props }, ref) {
  121. return (
  122. <caption
  123. ref={ref}
  124. className={tableCaptionStyle({ class: className })}
  125. {...props}
  126. />
  127. );
  128. });
  129. Table.displayName = 'Table';
  130. TableHeader.displayName = 'TableHeader';
  131. TableBody.displayName = 'TableBody';
  132. TableFooter.displayName = 'TableFooter';
  133. TableHead.displayName = 'TableHead';
  134. TableRow.displayName = 'TableRow';
  135. TableData.displayName = 'TableData';
  136. TableCaption.displayName = 'TableCaption';
  137. export {
  138. Table,
  139. TableHeader,
  140. TableBody,
  141. TableFooter,
  142. TableHead,
  143. TableRow,
  144. TableData,
  145. TableCaption,
  146. };