File tree Expand file tree Collapse file tree 11 files changed +192
-4
lines changed
example/src/stories/Table Expand file tree Collapse file tree 11 files changed +192
-4
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1- import React , { ElementType } from 'react'
1+ import React , { ElementType , useContext } from 'react'
22import styled from 'styled-components'
33import { PC , PP , PR } from '../../types/PolymorphicElementProps'
4+ import { TableContext } from './TableContext'
5+ import { TableRowGroupContext } from './TableRowGroupContext'
46
57const paddingMap = {
68 none : 'padding: 0;' ,
@@ -24,7 +26,13 @@ export interface CellProps {
2426
2527export 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 }
Original file line number Diff line number Diff line change 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+ )
Original file line number Diff line number Diff line change 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+ )
Original file line number Diff line number Diff line change 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+ )
Original file line number Diff line number Diff line change 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+ } )
Original file line number Diff line number Diff line change 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+ )
Original file line number Diff line number Diff line change 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+ )
Original file line number Diff line number Diff line change 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+ } )
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments