|
1 | | -/* eslint-disable @typescript-eslint/no-explicit-any */ |
2 | | -import React, { Children, FC, HTMLProps, cloneElement } from 'react'; |
| 1 | +import React, { Children, FC, HTMLProps } from 'react'; |
3 | 2 | import classNames from 'classnames'; |
4 | 3 | import { Container } from '@components/layout'; |
5 | 4 | import { childIsOfComponentType } from '@util/types/TypeGuards'; |
6 | 5 |
|
7 | | -type FooterListProps = HTMLProps<HTMLOListElement> & { singleColumn?: boolean }; |
| 6 | +interface FooterMetaProps extends FooterListProps { |
| 7 | + visuallyHiddenText?: string; |
| 8 | +} |
8 | 9 |
|
9 | | -const FooterList: FC<FooterListProps> = ({ |
10 | | - className, |
| 10 | +const FooterMeta: FC<FooterMetaProps> = ({ |
11 | 11 | children, |
12 | | - singleColumn = false, |
| 12 | + visuallyHiddenText = 'Support links', |
13 | 13 | ...rest |
14 | 14 | }) => { |
15 | | - let newChildren = children; |
| 15 | + const items = Children.toArray(children); |
16 | 16 |
|
17 | | - if (singleColumn) { |
18 | | - newChildren = Children.map(newChildren, (child) => |
19 | | - childIsOfComponentType(child, FooterListItem) ? cloneElement(child, { singleColumn }) : child, |
20 | | - ); |
21 | | - } |
| 17 | + const metaItems = items.filter((child) => childIsOfComponentType(child, FooterListItem)); |
| 18 | + const metaCopyright = items.filter((child) => childIsOfComponentType(child, FooterCopyright)); |
22 | 19 |
|
23 | 20 | return ( |
24 | | - <ul className={classNames('nhsuk-footer__list', className)} {...rest}> |
25 | | - {newChildren} |
26 | | - </ul> |
| 21 | + <div className="nhsuk-footer__meta"> |
| 22 | + <h2 className="nhsuk-u-visually-hidden">{visuallyHiddenText}</h2> |
| 23 | + <FooterList {...rest}>{metaItems}</FooterList> |
| 24 | + {metaCopyright.length ? metaCopyright : <FooterCopyright />} |
| 25 | + </div> |
27 | 26 | ); |
28 | 27 | }; |
29 | 28 |
|
30 | | -const FooterListItem: FC<HTMLProps<HTMLAnchorElement> & { singleColumn?: boolean }> = ({ |
31 | | - className, |
32 | | - singleColumn = false, |
33 | | - ...rest |
34 | | -}) => ( |
35 | | - <li |
36 | | - className={classNames( |
37 | | - 'nhsuk-footer__list-item', |
38 | | - singleColumn ? 'nhsuk-footer-default__list-item' : '', |
39 | | - )} |
40 | | - > |
| 29 | +type FooterListProps = HTMLProps<HTMLUListElement>; |
| 30 | + |
| 31 | +const FooterList: FC<FooterListProps> = ({ className, children, ...rest }) => ( |
| 32 | + <ul className={classNames('nhsuk-footer__list', className)} {...rest}> |
| 33 | + {children} |
| 34 | + </ul> |
| 35 | +); |
| 36 | + |
| 37 | +const FooterListItem: FC<HTMLProps<HTMLAnchorElement>> = ({ className, ...rest }) => ( |
| 38 | + <li className="nhsuk-footer__list-item"> |
41 | 39 | <a className={classNames('nhsuk-footer__list-item-link', className)} {...rest} /> |
42 | 40 | </li> |
43 | 41 | ); |
44 | 42 |
|
45 | | -const FooterCopyright: FC<HTMLProps<HTMLParagraphElement>> = ({ className, ...rest }) => ( |
46 | | - <p className={classNames('nhsuk-footer__copyright', className)} {...rest} /> |
| 43 | +const FooterCopyright: FC<HTMLProps<HTMLParagraphElement>> = ({ |
| 44 | + children = '© NHS England', |
| 45 | + className, |
| 46 | + ...rest |
| 47 | +}) => ( |
| 48 | + <p className={classNames('nhsuk-body-s', className)} {...rest}> |
| 49 | + {children} |
| 50 | + </p> |
47 | 51 | ); |
48 | 52 |
|
49 | 53 | interface FooterProps extends HTMLProps<HTMLDivElement> { |
50 | | - visuallyHiddenText?: false | string; |
| 54 | + containerClassName?: string; |
51 | 55 | } |
52 | 56 |
|
53 | 57 | interface FooterComponent extends FC<FooterProps> { |
| 58 | + Meta: FC<FooterMetaProps>; |
54 | 59 | List: FC<FooterListProps>; |
55 | 60 | ListItem: FC<HTMLProps<HTMLAnchorElement>>; |
56 | 61 | Copyright: FC<HTMLProps<HTMLParagraphElement>>; |
57 | 62 | } |
58 | 63 |
|
59 | | -const FooterComponent: FooterComponent = ({ |
60 | | - className, |
61 | | - children, |
62 | | - visuallyHiddenText = 'Support links', |
63 | | - ...rest |
64 | | -}) => { |
65 | | - const footerCols = Children.toArray(children).filter((child) => |
66 | | - childIsOfComponentType(child, FooterList), |
67 | | - ); |
68 | | - const footerCopyright = Children.toArray(children).filter((child) => |
69 | | - childIsOfComponentType(child, FooterCopyright), |
70 | | - ); |
| 64 | +const FooterComponent: FooterComponent = ({ className, containerClassName, children, ...rest }) => { |
| 65 | + const items = Children.toArray(children); |
| 66 | + const meta = items.filter((child) => childIsOfComponentType(child, FooterMeta)); |
| 67 | + const columns = items.filter((child) => childIsOfComponentType(child, FooterList)); |
71 | 68 |
|
72 | | - let newChildren; |
73 | | - const footerHasMultipleColumns = footerCols.length > 1; |
74 | | - |
75 | | - if (footerHasMultipleColumns) { |
76 | | - // Remove the copyright from being rendered inside the 'nhsuk-footer' div |
77 | | - newChildren = Children.toArray(children).filter( |
78 | | - (child) => !childIsOfComponentType(child, FooterCopyright), |
79 | | - ); |
80 | | - } else { |
81 | | - newChildren = Children.map(children, (child) => |
82 | | - childIsOfComponentType(child, FooterList) |
83 | | - ? cloneElement(child, { singleColumn: true }) |
84 | | - : child, |
85 | | - ); |
86 | | - } |
| 69 | + const columnsPerRow = 4; |
| 70 | + const columnsTotal = Math.ceil(columns.length / columnsPerRow); |
| 71 | + |
| 72 | + const rows = Array.from({ length: columnsTotal }, (column, index) => |
| 73 | + columns.slice(index * columnsPerRow, index * columnsPerRow + columnsPerRow), |
| 74 | + ); |
87 | 75 |
|
88 | 76 | return ( |
89 | | - <footer role="contentinfo" {...rest}> |
90 | | - <div className={classNames('nhsuk-footer-container', className)}> |
91 | | - <Container> |
92 | | - {visuallyHiddenText ? ( |
93 | | - <h2 className="nhsuk-u-visually-hidden">{visuallyHiddenText}</h2> |
94 | | - ) : null} |
95 | | - <div className="nhsuk-footer">{newChildren}</div> |
96 | | - {footerHasMultipleColumns ? <div>{footerCopyright}</div> : undefined} |
97 | | - </Container> |
98 | | - </div> |
| 77 | + <footer className={classNames('nhsuk-footer', className)} role="contentinfo" {...rest}> |
| 78 | + <Container className={containerClassName}> |
| 79 | + {rows.map((row, rowIndex) => ( |
| 80 | + <div className="nhsuk-footer__navigation nhsuk-grid-row" key={`row-${rowIndex}`}> |
| 81 | + {row.map((column, columnIndex) => ( |
| 82 | + <div className="nhsuk-grid-column-one-quarter" key={`column-${columnIndex}`}> |
| 83 | + {column} |
| 84 | + </div> |
| 85 | + ))} |
| 86 | + </div> |
| 87 | + ))} |
| 88 | + {meta} |
| 89 | + </Container> |
99 | 90 | </footer> |
100 | 91 | ); |
101 | 92 | }; |
102 | 93 |
|
| 94 | +FooterComponent.Meta = FooterMeta; |
103 | 95 | FooterComponent.List = FooterList; |
104 | 96 | FooterComponent.ListItem = FooterListItem; |
105 | 97 | FooterComponent.Copyright = FooterCopyright; |
|
0 commit comments