xref: /expo/docs/ui/components/Table/Table.tsx (revision 2f97575e)
1import { css } from '@emotion/react';
2import { theme, borderRadius, typography, spacing } from '@expo/styleguide';
3import React, { PropsWithChildren } from 'react';
4
5import { TableHeaders } from './TableHeaders';
6import { TableLayout, TextAlign } from './types';
7
8type TableProps = PropsWithChildren<{
9  headers?: string[];
10  headersAlign?: TextAlign[];
11  layout?: TableLayout;
12}>;
13
14export const Table = ({
15  children,
16  headers = [],
17  headersAlign,
18  layout = TableLayout.Auto,
19}: TableProps) => (
20  <div css={tableWrapperStyle}>
21    <table css={[tableStyle, { tableLayout: layout }]}>
22      {headers.length ? (
23        <>
24          <TableHeaders headers={headers} headersAlign={headersAlign} />
25          <tbody>{children}</tbody>
26        </>
27      ) : (
28        children
29      )}
30    </table>
31  </div>
32);
33
34const tableWrapperStyle = css({
35  border: `1px solid ${theme.border.default}`,
36  borderRadius: borderRadius.medium,
37  overflow: 'hidden',
38  marginBottom: spacing[4],
39});
40
41const tableStyle = css({
42  ...typography.fontSizes[14],
43  width: '100%',
44  border: 0,
45  borderRadius: 0,
46  marginBottom: 0,
47  borderCollapse: 'collapse',
48  color: theme.text.default,
49
50  'blockquote, li': {
51    ...typography.fontSizes[14],
52  },
53});
54