|
1 | | -import React, { HTMLProps, createContext, useContext } from 'react'; |
| 1 | +import React, { ComponentProps, HTMLProps, ReactNode } from 'react'; |
2 | 2 | import classNames from 'classnames'; |
3 | | -import HeadingLevel, { HeadingLevelType } from '../../util/HeadingLevel'; |
4 | | -import isDev from '../../util/IsDev'; |
| 3 | +import TableBody from './components/TableBody'; |
| 4 | +import TableCaption from './components/TableCaption'; |
| 5 | +import TableCell from './components/TableCell'; |
| 6 | +import TableContainer from './components/TableContainer'; |
| 7 | +import TableHead from './components/TableHead'; |
| 8 | +import TableRow from './components/TableRow'; |
| 9 | +import TablePanel from './components/TablePanel'; |
| 10 | +import TableContext, { ITableContext } from './TableContext'; |
5 | 11 |
|
6 | | -enum TableSectionTypes { |
7 | | - HEAD = 'HEADER', |
8 | | - BODY = 'BODY', |
| 12 | +interface TableProps extends HTMLProps<HTMLTableElement> { |
| 13 | + responsive?: boolean; |
| 14 | + caption?: ReactNode; |
| 15 | + captionProps?: ComponentProps<typeof TableCaption>; |
9 | 16 | } |
10 | 17 |
|
11 | | -const TableSectionContext = createContext<TableSectionTypes>(TableSectionTypes.HEAD); |
12 | | - |
13 | | -const TableHead: React.FC<HTMLProps<HTMLTableSectionElement>> = ({ |
14 | | - className, |
15 | | - children, |
16 | | - ...rest |
17 | | -}) => ( |
18 | | - <thead className={classNames('nhsuk-table__head', className)} {...rest}> |
19 | | - <TableSectionContext.Provider value={TableSectionTypes.HEAD}> |
20 | | - {children} |
21 | | - </TableSectionContext.Provider> |
22 | | - </thead> |
23 | | -); |
24 | | - |
25 | | -const TableBody: React.FC<HTMLProps<HTMLTableSectionElement>> = ({ |
26 | | - className, |
27 | | - children, |
28 | | - ...rest |
29 | | -}) => ( |
30 | | - <tbody className={classNames('nhsuk-table__body', className)} {...rest}> |
31 | | - <TableSectionContext.Provider value={TableSectionTypes.BODY}> |
32 | | - {children} |
33 | | - </TableSectionContext.Provider> |
34 | | - </tbody> |
35 | | -); |
36 | | - |
37 | | -const TableRow: React.FC<HTMLProps<HTMLTableRowElement>> = ({ className, ...rest }) => ( |
38 | | - <tr className={classNames('nhsuk-table__row', className)} {...rest} /> |
39 | | -); |
40 | | - |
41 | | -interface TableCellProps extends HTMLProps<HTMLTableCellElement> { |
42 | | - header?: boolean | undefined; |
| 18 | +interface TableState { |
| 19 | + headings: string[]; |
43 | 20 | } |
44 | 21 |
|
45 | | -const TableCell: React.FC<TableCellProps> = ({ className, header, ...rest }) => { |
46 | | - const sectionType = useContext(TableSectionContext); |
47 | | - if (header !== undefined) { |
48 | | - if (header === true) { |
49 | | - return <th className={classNames('nhsuk-table__header', className)} scope="col" {...rest} />; |
50 | | - } |
51 | | - return <td className={classNames('nhsuk-table__cell', className)} {...rest} />; |
52 | | - } |
53 | | - if (sectionType === TableSectionTypes.HEAD) { |
54 | | - return <th className={classNames('nhsuk-table__header', className)} scope="col" {...rest} />; |
55 | | - } |
56 | | - if (sectionType === TableSectionTypes.BODY) { |
57 | | - return <td className={classNames('nhsuk-table__cell', className)} {...rest} />; |
| 22 | +class Table extends React.PureComponent<TableProps, TableState> { |
| 23 | + static defaultProps = { |
| 24 | + responsive: false, |
| 25 | + }; |
| 26 | + |
| 27 | + static Container = TableContainer; |
| 28 | + |
| 29 | + static Head = TableHead; |
| 30 | + |
| 31 | + static Row = TableRow; |
| 32 | + |
| 33 | + static Cell = TableCell; |
| 34 | + |
| 35 | + static Body = TableBody; |
| 36 | + |
| 37 | + static Panel = TablePanel; |
| 38 | + |
| 39 | + constructor(props: TableProps) { |
| 40 | + super(props); |
| 41 | + this.state = { |
| 42 | + headings: [], |
| 43 | + }; |
58 | 44 | } |
59 | | - if (isDev()) { |
60 | | - console.warn( |
61 | | - 'TableCell used outside of TableHead or TableBody elements. Unable to determine section type from context.', |
| 45 | + |
| 46 | + setHeadings = (headings: string[]): void => { |
| 47 | + const isEqual = headings.reduce( |
| 48 | + (prevValue, heading, index) => prevValue && heading === this.state.headings[index], |
| 49 | + true, |
62 | 50 | ); |
63 | | - } |
64 | | - return <td className={classNames('nhsuk-table__cell', className)} {...rest} />; |
65 | | -}; |
66 | 51 |
|
67 | | -interface TablePanelProps extends HTMLProps<HTMLDivElement> { |
68 | | - heading?: string; |
69 | | - headingProps?: HTMLProps<HTMLHeadingElement>; |
70 | | - headingLevel?: HeadingLevelType; |
71 | | -} |
| 52 | + if (!isEqual) this.setState({ headings }); |
| 53 | + }; |
72 | 54 |
|
73 | | -const TablePanel: React.FC<TablePanelProps> = ({ |
74 | | - className, |
75 | | - children, |
76 | | - heading, |
77 | | - headingLevel, |
78 | | - headingProps, |
79 | | - ...rest |
80 | | -}) => ( |
81 | | - <div className={classNames('nhsuk-table__panel-with-heading-tab', className)} {...rest}> |
82 | | - {heading ? ( |
83 | | - <HeadingLevel |
84 | | - className="nhsuk-table__heading-tab" |
85 | | - {...headingProps} |
86 | | - headingLevel={headingLevel} |
87 | | - > |
88 | | - {heading} |
89 | | - </HeadingLevel> |
90 | | - ) : null} |
91 | | - {children} |
92 | | - </div> |
93 | | -); |
94 | | - |
95 | | -TablePanel.defaultProps = { |
96 | | - headingLevel: 'h3', |
97 | | -}; |
| 55 | + render(): JSX.Element { |
| 56 | + const { className, responsive, children, caption, captionProps, ...rest } = this.props; |
98 | 57 |
|
99 | | -interface TableProps extends HTMLProps<HTMLTableElement> { |
100 | | - caption?: string; |
101 | | -} |
| 58 | + const contextValue: ITableContext = { |
| 59 | + isResponsive: Boolean(responsive), |
| 60 | + headings: this.state.headings, |
| 61 | + setHeadings: this.setHeadings, |
| 62 | + }; |
102 | 63 |
|
103 | | -interface Table extends React.FC<TableProps> { |
104 | | - Body: React.FC<HTMLProps<HTMLTableSectionElement>>; |
105 | | - Head: React.FC<HTMLProps<HTMLTableSectionElement>>; |
106 | | - Row: React.FC<HTMLProps<HTMLTableRowElement>>; |
107 | | - Cell: React.FC<TableCellProps>; |
108 | | - Panel: React.FC<TablePanelProps>; |
| 64 | + return ( |
| 65 | + <TableContext.Provider value={contextValue}> |
| 66 | + <table |
| 67 | + className={classNames( |
| 68 | + { 'nhsuk-table': !responsive }, |
| 69 | + { 'nhsuk-table-responsive': responsive }, |
| 70 | + className, |
| 71 | + )} |
| 72 | + {...rest} |
| 73 | + > |
| 74 | + {caption && <TableCaption {...captionProps}>{caption}</TableCaption>} |
| 75 | + {children} |
| 76 | + </table> |
| 77 | + </TableContext.Provider> |
| 78 | + ); |
| 79 | + } |
109 | 80 | } |
110 | 81 |
|
111 | | -const Table: Table = ({ |
112 | | - className, caption, children, ...rest |
113 | | -}) => ( |
114 | | - <div className="nhsuk-table-responsive"> |
115 | | - <table className={classNames('nhsuk-table', className)} {...rest}> |
116 | | - {caption ? <caption className="nhsuk-table__caption">{caption}</caption> : null} |
117 | | - {children} |
118 | | - </table> |
119 | | - </div> |
120 | | -); |
121 | | - |
122 | | -Table.Body = TableBody; |
123 | | -Table.Head = TableHead; |
124 | | -Table.Row = TableRow; |
125 | | -Table.Cell = TableCell; |
126 | | -Table.Panel = TablePanel; |
127 | | - |
128 | 82 | export default Table; |
0 commit comments