table.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import {useMemo} from 'react';
  2. import {COL_WIDTH_MINIMUM} from 'sentry/components/gridEditable';
  3. import {
  4. Body as _TableWrapper,
  5. Grid as _Table,
  6. GridBody,
  7. GridBodyCell,
  8. GridBodyCellStatus,
  9. GridHead,
  10. GridHeadCell,
  11. GridRow,
  12. Header,
  13. HeaderButtonContainer,
  14. HeaderTitle,
  15. } from 'sentry/components/gridEditable/styles';
  16. interface TableProps extends React.ComponentProps<typeof _TableWrapper> {}
  17. export function Table({children, style, ...props}: TableProps) {
  18. return (
  19. <_TableWrapper {...props}>
  20. <_Table style={style}>{children}</_Table>
  21. </_TableWrapper>
  22. );
  23. }
  24. interface TableStatusProps {
  25. children: React.ReactNode;
  26. }
  27. export function TableStatus({children}: TableStatusProps) {
  28. return (
  29. <GridRow>
  30. <GridBodyCellStatus>{children}</GridBodyCellStatus>
  31. </GridRow>
  32. );
  33. }
  34. const MINIMUM_COLUMN_WIDTH = COL_WIDTH_MINIMUM;
  35. type Item = {
  36. label: string;
  37. value: React.ReactNode;
  38. width?: number | 'min-content';
  39. };
  40. interface UseTableStylesOptions {
  41. items: Item[];
  42. minimumColumnWidth?: number;
  43. }
  44. export function useTableStyles({
  45. items,
  46. minimumColumnWidth = MINIMUM_COLUMN_WIDTH,
  47. }: UseTableStylesOptions) {
  48. const tableStyles = useMemo(() => {
  49. const columns = new Array(items.length);
  50. for (let i = 0; i < items.length; i++) {
  51. if (typeof items[i].width === 'number') {
  52. columns[i] = `${items[i].width}px`;
  53. } else {
  54. columns[i] = items[i].width ?? `minmax(${minimumColumnWidth}px, auto)`;
  55. }
  56. }
  57. return {
  58. gridTemplateColumns: columns.join(' '),
  59. };
  60. }, [items, minimumColumnWidth]);
  61. return {tableStyles};
  62. }
  63. export const TableBody = GridBody;
  64. export const TableBodyCell = GridBodyCell;
  65. export const TableHead = GridHead;
  66. export const TableHeadCell = GridHeadCell;
  67. export const TableHeader = Header;
  68. export const TableHeaderActions = HeaderButtonContainer;
  69. export const TableHeaderTitle = HeaderTitle;
  70. export const TableRow = GridRow;