|
| 1 | +import React, { memo, forwardRef, useState, useEffect } from "react"; |
| 2 | +import type { ReactNode } from "react"; |
| 3 | +import type { FrClassName } from "./lib/generatedFromCss/classNames"; |
| 4 | +import { symToStr } from "tsafe/symToStr"; |
| 5 | +import { fr } from "./lib"; |
| 6 | +import { cx } from "./lib/tools/cx"; |
| 7 | +import { assert } from "tsafe/assert"; |
| 8 | +import type { Equals } from "tsafe"; |
| 9 | +import { useConstCallback } from "./lib/tools/powerhooks/useConstCallback"; |
| 10 | +import { createComponentI18nApi } from "./lib/i18n"; |
| 11 | + |
| 12 | +export type AlertProps = { |
| 13 | + className?: string; |
| 14 | + severity: AlertProps.Severity; |
| 15 | + /** Default h3 */ |
| 16 | + as?: `h${2 | 3 | 4 | 5 | 6}`; |
| 17 | + classes?: Partial<Record<"root" | "title" | "description" | "close", string>>; |
| 18 | +} & (AlertProps.Md | AlertProps.Sm) & |
| 19 | + (AlertProps.NonClosable | AlertProps.Closable); |
| 20 | + |
| 21 | +export namespace AlertProps { |
| 22 | + export type Md = { |
| 23 | + isSmall?: false; |
| 24 | + title: NonNullable<ReactNode>; |
| 25 | + description?: NonNullable<ReactNode>; |
| 26 | + }; |
| 27 | + |
| 28 | + export type Sm = { |
| 29 | + isSmall: true; |
| 30 | + title?: NonNullable<ReactNode>; |
| 31 | + description: NonNullable<ReactNode>; |
| 32 | + }; |
| 33 | + |
| 34 | + export type NonClosable = { |
| 35 | + isClosable: false; |
| 36 | + isClosed?: undefined; |
| 37 | + onClose?: undefined; |
| 38 | + }; |
| 39 | + |
| 40 | + export type Closable = { |
| 41 | + isClosable: true; |
| 42 | + } & (Closable.Controlled | Closable.Uncontrolled); |
| 43 | + |
| 44 | + export namespace Closable { |
| 45 | + export type Controlled = { |
| 46 | + isClosed: boolean; |
| 47 | + onClose: () => void; |
| 48 | + }; |
| 49 | + |
| 50 | + export type Uncontrolled = { |
| 51 | + isClosed?: undefined; |
| 52 | + onClose?: () => void; |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + type ExtractSeverity<FrClassName> = FrClassName extends `fr-alert--${infer Severity}` |
| 57 | + ? Exclude<Severity, "sm"> |
| 58 | + : never; |
| 59 | + |
| 60 | + export type Severity = ExtractSeverity<FrClassName>; |
| 61 | +} |
| 62 | + |
| 63 | +/** @see <https://www.systeme-de-design.gouv.fr/elements-d-interface/composants/alerte> */ |
| 64 | +export const Alert = memo( |
| 65 | + forwardRef<HTMLDivElement, AlertProps>((props, ref) => { |
| 66 | + const { |
| 67 | + className, |
| 68 | + severity, |
| 69 | + as: HtmlTitleTag = "h3", |
| 70 | + classes = {}, |
| 71 | + isSmall, |
| 72 | + title, |
| 73 | + description, |
| 74 | + isClosable, |
| 75 | + isClosed: props_isClosed, |
| 76 | + onClose, |
| 77 | + ...rest |
| 78 | + } = props; |
| 79 | + |
| 80 | + assert<Equals<keyof typeof rest, never>>(); |
| 81 | + |
| 82 | + const [isClosed, setIsClosed] = useState(props_isClosed ?? false); |
| 83 | + |
| 84 | + useEffect(() => { |
| 85 | + if (props_isClosed === undefined) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + setIsClosed(props_isClosed); |
| 90 | + }, [props_isClosed]); |
| 91 | + |
| 92 | + const onCloseButtonClick = useConstCallback(() => { |
| 93 | + if (props_isClosed === undefined) { |
| 94 | + //Uncontrolled |
| 95 | + setIsClosed(true); |
| 96 | + onClose?.(); |
| 97 | + } else { |
| 98 | + //Controlled |
| 99 | + onClose(); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + const { t } = useTranslation(); |
| 104 | + |
| 105 | + if (isClosed) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + return ( |
| 110 | + <div |
| 111 | + className={cx( |
| 112 | + fr.cx("fr-alert", `fr-alert--${severity}`, { "fr-alert--sm": isSmall }), |
| 113 | + classes.root, |
| 114 | + className |
| 115 | + )} |
| 116 | + ref={ref} |
| 117 | + {...rest} |
| 118 | + > |
| 119 | + <HtmlTitleTag className={cx(fr.cx("fr-alert__title"), classes.title)}> |
| 120 | + {title} |
| 121 | + </HtmlTitleTag> |
| 122 | + <p className={classes.description}>{description}</p> |
| 123 | + {/* TODO: Use our button once we have one */} |
| 124 | + {isClosable && ( |
| 125 | + <button |
| 126 | + className={cx(fr.cx("fr-link--close", "fr-link"), classes.close)} |
| 127 | + onClick={onCloseButtonClick} |
| 128 | + > |
| 129 | + {t("hide message")} |
| 130 | + </button> |
| 131 | + )} |
| 132 | + </div> |
| 133 | + ); |
| 134 | + }) |
| 135 | +); |
| 136 | + |
| 137 | +Alert.displayName = symToStr({ Alert }); |
| 138 | + |
| 139 | +export default Alert; |
| 140 | + |
| 141 | +const { useTranslation, addAlertTranslations } = createComponentI18nApi({ |
| 142 | + "componentName": symToStr({ Alert }), |
| 143 | + "frMessages": { |
| 144 | + /* spell-checker: disable */ |
| 145 | + "hide message": "Masquer le message" |
| 146 | + /* spell-checker: enable */ |
| 147 | + } |
| 148 | +}); |
| 149 | + |
| 150 | +addAlertTranslations({ |
| 151 | + "lang": "en", |
| 152 | + "messages": { |
| 153 | + "hide message": "Hide the message" |
| 154 | + } |
| 155 | +}); |
| 156 | + |
| 157 | +addAlertTranslations({ |
| 158 | + "lang": "es", |
| 159 | + "messages": { |
| 160 | + /* spell-checker: disable */ |
| 161 | + "hide message": "Occultar el mesage" |
| 162 | + /* spell-checker: enable */ |
| 163 | + } |
| 164 | +}); |
| 165 | + |
| 166 | +export { addAlertTranslations }; |
0 commit comments