|
1 | | -import { type ComponentPropsWithoutRef, forwardRef } from 'react'; |
| 1 | +import { |
| 2 | + Children, |
| 3 | + type ComponentPropsWithoutRef, |
| 4 | + createRef, |
| 5 | + forwardRef, |
| 6 | + useEffect, |
| 7 | + useState, |
| 8 | +} from 'react'; |
2 | 9 | import classNames from 'classnames'; |
3 | | -import { HeadingLevel } from '#components/utils/HeadingLevel.js'; |
4 | | -import { NotificationBannerHeading, NotificationBannerLink } from './components/index.js'; |
| 10 | +import { |
| 11 | + NotificationBannerHeading, |
| 12 | + NotificationBannerLink, |
| 13 | + NotificationBannerTitle, |
| 14 | +} from './components/index.js'; |
| 15 | +import { type NotificationBanner as NotificationBannerModule } from 'nhsuk-frontend'; |
| 16 | +import { childIsOfComponentType } from '#util/types/TypeGuards.js'; |
5 | 17 |
|
6 | 18 | export interface NotificationBannerProps extends ComponentPropsWithoutRef<'div'> { |
7 | 19 | success?: boolean; |
| 20 | + disableAutoFocus?: boolean; |
8 | 21 | } |
9 | 22 |
|
10 | 23 | const NotificationBannerComponent = forwardRef<HTMLDivElement, NotificationBannerProps>( |
11 | | - ({ children, className, title, success, ...rest }, forwardedRef) => { |
| 24 | + ({ children, className, title, success, role, disableAutoFocus, ...rest }, forwardedRef) => { |
| 25 | + const [moduleRef] = useState(() => forwardedRef || createRef<HTMLDivElement>()); |
| 26 | + const [instance, setInstance] = useState<NotificationBannerModule>(); |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + if (!('current' in moduleRef) || !moduleRef.current || instance) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + const { current: $root } = moduleRef; |
| 34 | + |
| 35 | + import('nhsuk-frontend').then(({ NotificationBanner }) => { |
| 36 | + setInstance(new NotificationBanner($root)); |
| 37 | + }); |
| 38 | + }, [moduleRef, instance]); |
| 39 | + |
| 40 | + const items = Children.toArray(children); |
| 41 | + const titleElement = items.find((child) => |
| 42 | + childIsOfComponentType(child, NotificationBannerTitle), |
| 43 | + ) || <NotificationBannerTitle success={success}>{title}</NotificationBannerTitle>; |
| 44 | + const nonTitleItems = items.filter( |
| 45 | + (child) => !childIsOfComponentType(child, NotificationBannerTitle), |
| 46 | + ); |
| 47 | + const headerElement = nonTitleItems.find((child) => |
| 48 | + childIsOfComponentType(child, NotificationBannerHeading), |
| 49 | + ); |
| 50 | + const bodyItems = nonTitleItems.filter( |
| 51 | + (child) => !childIsOfComponentType(child, NotificationBannerHeading), |
| 52 | + ); |
12 | 53 | return ( |
13 | | - <section |
| 54 | + <div |
14 | 55 | className={classNames( |
15 | 56 | 'nhsuk-notification-banner', |
16 | 57 | { 'nhsuk-notification-banner--success': success }, |
17 | 58 | className, |
18 | 59 | )} |
19 | | - aria-labelledby="nhsuk-notification-banner-title" |
| 60 | + aria-labelledby={titleElement.props.id || 'nhsuk-notification-banner-title'} |
20 | 61 | data-module="nhsuk-notification-banner" |
| 62 | + data-disable-auto-focus={disableAutoFocus} |
| 63 | + ref={moduleRef} |
| 64 | + role={role || (success ? 'alert' : 'region')} |
21 | 65 | > |
22 | | - <div className="nhsuk-notification-banner__header"> |
23 | | - <HeadingLevel |
24 | | - className="nhsuk-notification-banner__title" |
25 | | - headingLevel={'h2'} |
26 | | - id="nhsuk-notification-banner-title" |
27 | | - > |
28 | | - {title || (success ? 'Success' : 'Important')} |
29 | | - </HeadingLevel> |
30 | | - </div> |
31 | | - <div className={'nhsuk-notification-banner__content'} ref={forwardedRef} {...rest}> |
32 | | - {children} |
| 66 | + {titleElement} |
| 67 | + <div className={'nhsuk-notification-banner__content'} {...rest}> |
| 68 | + {headerElement} |
| 69 | + {bodyItems} |
33 | 70 | </div> |
34 | | - </section> |
| 71 | + </div> |
35 | 72 | ); |
36 | 73 | }, |
37 | 74 | ); |
38 | 75 |
|
39 | 76 | NotificationBannerComponent.displayName = 'NotificationBanner'; |
40 | 77 |
|
41 | 78 | export const NotificationBanner = Object.assign(NotificationBannerComponent, { |
| 79 | + Title: NotificationBannerTitle, |
42 | 80 | Heading: NotificationBannerHeading, |
43 | 81 | Link: NotificationBannerLink, |
44 | 82 | }); |
0 commit comments