File tree Expand file tree Collapse file tree 3 files changed +97
-3
lines changed
src/components/table/components Expand file tree Collapse file tree 3 files changed +97
-3
lines changed Original file line number Diff line number Diff line change @@ -9,22 +9,28 @@ const CellOutsideOfSectionWarning =
99export interface TableCellProps extends HTMLProps < HTMLTableCellElement > {
1010 _responsive ?: boolean ;
1111 _responsiveHeading ?: string ;
12+ isNumeric ?: boolean ;
1213}
1314
1415const TableCell : React . FC < TableCellProps > = ( {
1516 className,
1617 _responsive,
1718 _responsiveHeading,
19+ isNumeric,
1820 children,
1921 ...rest
2022} ) => {
2123 const section = useContext ( TableSectionContext ) ;
2224 useDevWarning ( CellOutsideOfSectionWarning , ( ) => section === TableSection . NONE ) ;
2325
26+ const cellClass = section === TableSection . HEAD ? 'nhsuk-table__header' : 'nhsuk-table__cell' ;
27+ const numericHeader = isNumeric ? `${ cellClass } --numeric` : '' ;
28+ const classes = classNames ( cellClass , numericHeader , className )
29+
2430 switch ( section ) {
2531 case TableSection . HEAD :
2632 return (
27- < th className = { classNames ( 'nhsuk-table__header' , className ) } scope = "col" { ...rest } >
33+ < th className = { classes } scope = "col" { ...rest } >
2834 { children }
2935 </ th >
3036 ) ;
@@ -34,7 +40,7 @@ const TableCell: React.FC<TableCellProps> = ({
3440 default :
3541 return (
3642 < td
37- className = { classNames ( 'nhsuk-table__cell' , className ) }
43+ className = { classes }
3844 role = { _responsive ? 'cell' : undefined }
3945 { ...rest }
4046 >
Original file line number Diff line number Diff line change @@ -97,4 +97,62 @@ describe('Table.Cell', () => {
9797
9898 wrapper . unmount ( ) ;
9999 } ) ;
100+
101+ it ( 'adds the numeric class when isNumeric is true' , ( ) => {
102+ const wrapper = mount (
103+ < table >
104+ < tbody >
105+ < tr >
106+ < TableCell data-test = "cell" isNumeric />
107+ </ tr >
108+ </ tbody >
109+ </ table > ,
110+ ) ;
111+
112+ const cell = wrapper . find ( '[data-test="cell"]' ) ;
113+ expect ( cell . last ( ) . prop ( 'className' ) ) . toContain ( 'nhsuk-table__cell--numeric' ) ;
114+
115+ wrapper . unmount ( ) ;
116+ } ) ;
117+
118+ it ( 'adds the numeric header class when isNumeric is true' , ( ) => {
119+ const wrapper = mount (
120+ < table >
121+ < TableHead >
122+ < tr >
123+ < TableCell data-test = "cell" isNumeric />
124+ </ tr >
125+ </ TableHead >
126+ </ table > ,
127+ ) ;
128+
129+ const cell = wrapper . find ( '[data-test="cell"]' ) ;
130+ expect ( cell . last ( ) . prop ( 'className' ) ) . toContain ( 'nhsuk-table__header--numeric' ) ;
131+
132+ wrapper . unmount ( ) ;
133+ } ) ;
134+
135+ it ( 'does not add the numeric header when isNumeric is false' , ( ) => {
136+ const wrapper = mount (
137+ < table >
138+ < TableHead >
139+ < tr >
140+ < TableCell data-test = "header" />
141+ </ tr >
142+ </ TableHead >
143+ < tbody >
144+ < tr >
145+ < TableCell data-test = "cell" />
146+ </ tr >
147+ </ tbody >
148+ </ table > ,
149+ ) ;
150+
151+ const header = wrapper . find ( '[data-test="header"]' ) ;
152+ expect ( header . last ( ) . prop ( 'className' ) ) . not . toContain ( 'nhsuk-table__header--numeric' ) ;
153+ const cell = wrapper . find ( '[data-test="cell"]' ) ;
154+ expect ( cell . last ( ) . prop ( 'className' ) ) . not . toContain ( 'nhsuk-table__header--numeric' ) ;
155+
156+ wrapper . unmount ( ) ;
157+ } ) ;
100158} ) ;
Original file line number Diff line number Diff line change 11import React from 'react' ;
2- import { Table } from '../src' ;
2+ import { Col , Hint , Row , Table } from '../src' ;
33
44export const StandardTable = ( ) : JSX . Element => (
55 < Table caption = "Skin symptoms and possible causes" >
@@ -82,6 +82,36 @@ export const ResponsiveTable = (): JSX.Element => (
8282 </ Table >
8383) ;
8484
85+ export const NumericCells = ( ) => (
86+ < Row >
87+ < Col width = "one-half" >
88+ < Hint > Right-aligned cells are used for numeric values</ Hint >
89+ < Table caption = "Number of cases" >
90+ < Table . Head >
91+ < Table . Row >
92+ < Table . Cell > Location</ Table . Cell >
93+ < Table . Cell isNumeric > Number of cases</ Table . Cell >
94+ </ Table . Row >
95+ </ Table . Head >
96+ < Table . Body >
97+ < Table . Row >
98+ < Table . Cell > England</ Table . Cell >
99+ < Table . Cell isNumeric > 4,000</ Table . Cell >
100+ </ Table . Row >
101+ < Table . Row >
102+ < Table . Cell > Wales</ Table . Cell >
103+ < Table . Cell isNumeric > 2,500</ Table . Cell >
104+ </ Table . Row >
105+ < Table . Row >
106+ < Table . Cell > Scotland</ Table . Cell >
107+ < Table . Cell isNumeric > 600</ Table . Cell >
108+ </ Table . Row >
109+ </ Table . Body >
110+ </ Table >
111+ </ Col >
112+ </ Row >
113+ ) ;
114+
85115export default {
86116 title : 'Components/Table' ,
87117 component : Table ,
You can’t perform that action at this time.
0 commit comments