Skip to content

Commit 59c54de

Browse files
committed
feat: Cell
1 parent 438a5ac commit 59c54de

File tree

11 files changed

+192
-4
lines changed

11 files changed

+192
-4
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Cell } from '@solved-ac/ui-react'
2+
import { ComponentMeta, ComponentStory } from '@storybook/react'
3+
import React from 'react'
4+
5+
export default {
6+
title: 'Components/Table/Cell',
7+
component: Cell,
8+
argTypes: {
9+
children: {
10+
control: 'none',
11+
description: 'The children to display inside the cell',
12+
},
13+
padding: {
14+
control: {
15+
type: 'select',
16+
options: ['none', 'normal', 'wide'],
17+
},
18+
description: 'The padding of the cell',
19+
},
20+
},
21+
} as ComponentMeta<typeof Cell>
22+
23+
const Template: ComponentStory<typeof Cell> = (args) => <Cell {...args} />
24+
25+
export const Default = Template.bind({})
26+
Default.args = {
27+
children: <>Cell</>,
28+
}

src/components/Table/Cell.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import React, { ElementType } from 'react'
1+
import React, { ElementType, useContext } from 'react'
22
import styled from 'styled-components'
33
import { PC, PP, PR } from '../../types/PolymorphicElementProps'
4+
import { TableContext } from './TableContext'
5+
import { TableRowGroupContext } from './TableRowGroupContext'
46

57
const paddingMap = {
68
none: 'padding: 0;',
@@ -24,7 +26,13 @@ export interface CellProps {
2426

2527
export const Cell: PC<'td', CellProps> = React.forwardRef(
2628
<T extends ElementType>(props: PP<T, CellProps>, ref?: PR<T>) => {
27-
const { padding = 'normal', as = 'td', ...rest } = props
29+
const tableContext = useContext(TableContext)
30+
const tableRowGroupContext = useContext(TableRowGroupContext)
31+
const {
32+
padding = tableContext.padding,
33+
as = tableRowGroupContext.header ? 'th' : 'td',
34+
...rest
35+
} = props
2836

2937
return <CellContainer padding={padding} ref={ref} as={as} {...rest} />
3038
}

src/components/Table/Row.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React, { ElementType, useContext } from 'react'
2+
import styled from 'styled-components'
3+
import { PC, PP, PR } from '../../types/PolymorphicElementProps'
4+
import { TableContext } from './TableContext'
5+
6+
interface RowContainerProps {
7+
header: boolean
8+
}
9+
10+
const RowContainer = styled.tr<RowContainerProps>`
11+
display: table-row;
12+
${({ header }) => header && 'text-align: center; font-weight: 700;'}
13+
`
14+
15+
export interface RowProps {
16+
header?: boolean
17+
padding?: 'none' | 'normal' | 'wide'
18+
}
19+
20+
export const Row: PC<'tr', RowProps> = React.forwardRef(
21+
<T extends ElementType>(props: PP<T, RowProps>, ref?: PR<T>) => {
22+
const tableContext = useContext(TableContext)
23+
const {
24+
header = false,
25+
padding = tableContext.padding,
26+
as = 'tr',
27+
...rest
28+
} = props
29+
30+
return (
31+
<TableContext.Provider value={{ padding }}>
32+
<RowContainer header={header} ref={ref} as={as} {...rest} />
33+
</TableContext.Provider>
34+
)
35+
}
36+
)

src/components/Table/Table.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React, { ElementType } from 'react'
2+
import styled from 'styled-components'
3+
import { PC, PP, PR } from '../../types/PolymorphicElementProps'
4+
import { TableContext } from './TableContext'
5+
import { TableRowGroupContext } from './TableRowGroupContext'
6+
7+
interface TableContainerProps {
8+
fullWidth: boolean
9+
}
10+
11+
const TableContainer = styled.table<TableContainerProps>`
12+
display: table;
13+
${({ fullWidth }) => fullWidth && 'width: 100%;'}
14+
`
15+
16+
export interface TableProps {
17+
fullWidth?: boolean
18+
padding?: 'none' | 'normal' | 'wide'
19+
}
20+
21+
export const Table: PC<'table', TableProps> = React.forwardRef(
22+
<T extends ElementType>(props: PP<T, TableProps>, ref?: PR<T>) => {
23+
const {
24+
fullWidth = false,
25+
padding = 'normal',
26+
as = 'table',
27+
...rest
28+
} = props
29+
30+
return (
31+
<TableContext.Provider value={{ padding }}>
32+
<TableRowGroupContext.Provider value={{ header: false }}>
33+
<TableContainer fullWidth={fullWidth} ref={ref} as={as} {...rest} />
34+
</TableRowGroupContext.Provider>
35+
</TableContext.Provider>
36+
)
37+
}
38+
)

src/components/Table/TableBody.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React, { ElementType } from 'react'
2+
import styled from 'styled-components'
3+
import { PC, PP, PR } from '../../types/PolymorphicElementProps'
4+
5+
const TableBodyContainer = styled.tbody`
6+
display: table-row-group;
7+
`
8+
9+
export const TableBody: PC<'tbody'> = React.forwardRef(
10+
<T extends ElementType>(props: PP<T>, ref?: PR<T>) => {
11+
const { as = 'tbody', ...rest } = props
12+
13+
return <TableBodyContainer ref={ref} as={as} {...rest} />
14+
}
15+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react'
2+
3+
export interface TableContextProps {
4+
padding: 'none' | 'normal' | 'wide'
5+
}
6+
7+
export const TableContext = React.createContext<TableContextProps>({
8+
padding: 'normal',
9+
})

src/components/Table/TableFoot.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React, { ElementType } from 'react'
2+
import styled from 'styled-components'
3+
import { PC, PP, PR } from '../../types/PolymorphicElementProps'
4+
5+
const TableFootContainer = styled.tfoot`
6+
display: table-footer-group;
7+
text-align: center;
8+
font-weight: 700;
9+
`
10+
11+
export const TableFoot: PC<'tfoot'> = React.forwardRef(
12+
<T extends ElementType>(props: PP<T>, ref?: PR<T>) => {
13+
const { as = 'tfoot', ...rest } = props
14+
15+
return <TableFootContainer ref={ref} as={as} {...rest} />
16+
}
17+
)

src/components/Table/TableHead.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React, { ElementType } from 'react'
2+
import styled from 'styled-components'
3+
import { PC, PP, PR } from '../../types/PolymorphicElementProps'
4+
import { TableRowGroupContext } from './TableRowGroupContext'
5+
6+
const TableHeadContainer = styled.thead`
7+
display: table-header-group;
8+
text-align: center;
9+
font-weight: 700;
10+
`
11+
12+
export const TableHead: PC<'thead'> = React.forwardRef(
13+
<T extends ElementType>(props: PP<T>, ref?: PR<T>) => {
14+
const { as = 'thead', ...rest } = props
15+
16+
return (
17+
<TableRowGroupContext.Provider value={{ header: true }}>
18+
<TableHeadContainer ref={ref} as={as} {...rest} />
19+
</TableRowGroupContext.Provider>
20+
)
21+
}
22+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react'
2+
3+
export interface TableRowGroupContextProps {
4+
header: boolean
5+
}
6+
7+
export const TableRowGroupContext =
8+
React.createContext<TableRowGroupContextProps>({
9+
header: false,
10+
})

src/components/Table/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
export * from './Cell';
1+
export * from './Cell'
2+
export * from './Row'
3+
export * from './Table'
4+
export * from './TableBody'
5+
export * from './TableFoot'
6+
export * from './TableHead'
27

0 commit comments

Comments
 (0)