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