Skip to content

Commit a641422

Browse files
authored
Merge pull request #9932 from TylerAPfledderer/feat/new-table-theme
feat(Table): add component, theme, and story
2 parents 7bfb628 + bb42801 commit a641422

File tree

13 files changed

+463
-40
lines changed

13 files changed

+463
-40
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { tableAnatomy } from "@chakra-ui/anatomy"
2+
import {
3+
createMultiStyleConfigHelpers,
4+
cssVar,
5+
defineStyle,
6+
} from "@chakra-ui/react"
7+
import { defineMergeStyles, tableDefaultTheme } from "./components.utils"
8+
9+
const { defineMultiStyleConfig, definePartsStyle } =
10+
createMultiStyleConfigHelpers(tableAnatomy.keys)
11+
12+
const $bgColor = cssVar("bg-color")
13+
14+
const cellPadding = defineStyle({
15+
p: 4,
16+
})
17+
18+
const baseStyle = defineMergeStyles(
19+
tableDefaultTheme.baseStyle,
20+
definePartsStyle({
21+
table: {
22+
[$bgColor.variable]: "colors.background.highlight",
23+
minW: "556px",
24+
},
25+
th: {
26+
borderBottom: "1px",
27+
borderColor: "body.base",
28+
color: "body.base",
29+
textTransform: "capitalize",
30+
verticalAlign: "bottom",
31+
...cellPadding,
32+
},
33+
tr: {
34+
"th, td": {
35+
_notLast: {
36+
borderRight: "2px",
37+
borderRightColor: "background.base",
38+
},
39+
},
40+
},
41+
td: {
42+
...cellPadding,
43+
},
44+
tbody: {
45+
tr: {
46+
verticalAlign: "top",
47+
_hover: {
48+
/**
49+
* Override specificity when hovering
50+
* over even rows in 'striped' variant.
51+
*/
52+
bg: $bgColor.reference,
53+
},
54+
},
55+
},
56+
})
57+
)
58+
59+
const variantMinimalStriped = definePartsStyle({
60+
tbody: {
61+
tr: {
62+
_even: {
63+
bg: $bgColor.reference,
64+
},
65+
},
66+
},
67+
})
68+
69+
const variantSimpleStriped = definePartsStyle({
70+
...variantMinimalStriped,
71+
thead: {
72+
bg: $bgColor.reference,
73+
},
74+
})
75+
76+
const variantSimple = definePartsStyle({
77+
thead: {
78+
bg: $bgColor.reference,
79+
},
80+
})
81+
82+
export const Table = defineMultiStyleConfig({
83+
baseStyle,
84+
variants: {
85+
minimal: {},
86+
"minimal-striped": variantMinimalStriped,
87+
simple: variantSimple,
88+
"simple-striped": variantSimpleStriped,
89+
},
90+
defaultProps: {
91+
variant: "simple",
92+
},
93+
})

src/@chakra-ui/gatsby-plugin/components/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Modal } from "./Modal"
88
import { Checkbox } from "./Checkbox"
99
import { Progress } from "./Progress"
1010
import { Tabs } from "./Tabs"
11+
import { Table } from "./Table"
1112
import { Radio } from "./Radio"
1213
import { Select } from "./Select"
1314
import { Switch } from "./Switch"
@@ -24,7 +25,6 @@ import {
2425
headingDefaultTheme,
2526
menuDefaultTheme,
2627
spinnerDefaultTheme,
27-
tableDefaultTheme,
2828
} from "./components.utils"
2929

3030
export default {
@@ -51,7 +51,7 @@ export default {
5151
Select,
5252
Spinner: spinnerDefaultTheme,
5353
Switch,
54-
Table: tableDefaultTheme,
54+
Table,
5555
Tabs,
5656
Tag,
5757
}

src/components/MarkdownTable.tsx

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/components/Table/index.tsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as React from "react"
2+
import {
3+
Table as ChakraTable,
4+
TableContainer,
5+
Td,
6+
Th,
7+
Tr,
8+
ThemingProps,
9+
Tbody,
10+
Thead,
11+
} from "@chakra-ui/react"
12+
import { MDXProviderComponentsProp } from "@mdx-js/react"
13+
14+
/*
15+
* TODO: Currently, there are cell spacing issues with some table content.
16+
* Prefer `layout="fixed"` in the future when content has been addressed
17+
* to provide equal cell widths.
18+
*/
19+
20+
interface TableProps extends ThemingProps<"Table"> {
21+
children: React.ReactNode
22+
}
23+
24+
const Table = (props: TableProps) => {
25+
const { variant, ...rest } = props
26+
return (
27+
<TableContainer whiteSpace="normal">
28+
<ChakraTable variant={variant} {...rest} />
29+
</TableContainer>
30+
)
31+
}
32+
33+
export const mdxTableComponents: MDXProviderComponentsProp = {
34+
table: Table,
35+
th: ({ align, ...rest }) => <Th textAlign={align} {...rest} />,
36+
td: ({ align, ...rest }) => <Td textAlign={align} {...rest} />,
37+
tr: (props) => <Tr {...props} />,
38+
tbody: (props) => <Tbody {...props} />,
39+
thead: (props) => <Thead {...props} />,
40+
}
41+
42+
export default Table
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import * as React from "react"
2+
import { Flex } from "@chakra-ui/react"
3+
import { Meta, StoryObj } from "@storybook/react"
4+
import TableComponent from ".."
5+
import {
6+
MdxDemoData,
7+
MdxEnergyConsumpData,
8+
MdxTypesOfBridgesData,
9+
} from "./mockMdxData"
10+
11+
type TableType = typeof TableComponent
12+
13+
const meta: Meta<TableType> = {
14+
title: "Molecules / Display Content / Tables",
15+
component: TableComponent,
16+
decorators: [
17+
(Story) => (
18+
<Flex flexDir="column" gap={16} maxW="container.md">
19+
<Story />
20+
</Flex>
21+
),
22+
],
23+
}
24+
25+
export default meta
26+
27+
type Story = StoryObj<TableType>
28+
29+
export const Tables: Story = {
30+
args: {
31+
children: <MdxDemoData />,
32+
},
33+
render: (args) => (
34+
<>
35+
<TableComponent {...args} />
36+
<TableComponent {...args} variant="minimal" />
37+
<TableComponent {...args} variant="minimal-striped" />
38+
<TableComponent {...args} variant="simple-striped" />
39+
</>
40+
),
41+
}
42+
43+
export const MockDocContent: Story = {
44+
render: () => (
45+
<>
46+
<TableComponent children={<MdxEnergyConsumpData />} variant="simple" />
47+
<TableComponent children={<MdxTypesOfBridgesData />} />
48+
</>
49+
),
50+
}

0 commit comments

Comments
 (0)