Skip to content

Commit 2932c91

Browse files
committed
#342: Improve readability of the Notice Props type def, forward the mouse event to the onClose callback
1 parent 143a6d1 commit 2932c91

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

src/Notice.tsx

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,38 @@ import { useConstCallback } from "./tools/powerhooks/useConstCallback";
1818
import { createComponentI18nApi } from "./i18n";
1919
import { useAnalyticsId } from "./tools/useAnalyticsId";
2020

21-
export type NoticeProps = {
22-
id?: string;
23-
className?: string;
24-
classes?: Partial<Record<"root" | "title" | "close", string>>;
25-
title: NonNullable<ReactNode>;
26-
style?: CSSProperties;
27-
} & (NoticeProps.NonClosable | NoticeProps.Closable);
21+
export type NoticeProps = NoticeProps.NonClosable | NoticeProps.Closable;
2822

2923
export namespace NoticeProps {
30-
export type NonClosable = {
24+
type Common = {
25+
id?: string;
26+
className?: string;
27+
classes?: Partial<Record<"root" | "title" | "close", string>>;
28+
title: NonNullable<ReactNode>;
29+
style?: CSSProperties;
30+
};
31+
32+
export type NonClosable = Common & {
3133
isClosable?: false;
32-
isClosed?: undefined;
33-
onClose?: undefined;
34+
35+
isClosed?: never;
36+
onClose?: never;
3437
};
3538

36-
export type Closable = {
37-
isClosable: true;
38-
} & (Closable.Controlled | Closable.Uncontrolled);
39+
export type Closable = Closable.Controlled | Closable.Uncontrolled;
3940

4041
export namespace Closable {
41-
export type Controlled = {
42+
export type Controlled = Common & {
43+
isClosable: true;
4244
isClosed: boolean;
43-
onClose: () => void;
45+
onClose: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
4446
};
4547

46-
export type Uncontrolled = {
47-
isClosed?: undefined;
48-
onClose?: () => void;
48+
export type Uncontrolled = Common & {
49+
isClosable: true;
50+
onClose?: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
51+
52+
isClosed?: never;
4953
};
5054
}
5155
}
@@ -107,16 +111,18 @@ export const Notice = memo(
107111
buttonElement.focus();
108112
}, [buttonElement]);
109113

110-
const onCloseButtonClick = useConstCallback(() => {
111-
if (props_isClosed === undefined) {
112-
//Uncontrolled
113-
setIsClosed(true);
114-
onClose?.();
115-
} else {
116-
//Controlled
117-
onClose();
114+
const onCloseButtonClick = useConstCallback(
115+
(event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
116+
if (props_isClosed === undefined) {
117+
//Uncontrolled
118+
setIsClosed(true);
119+
onClose?.(event);
120+
} else {
121+
//Controlled
122+
onClose(event);
123+
}
118124
}
119-
});
125+
);
120126

121127
const { t } = useTranslation();
122128

@@ -133,8 +139,8 @@ export const Notice = memo(
133139
style={style}
134140
{...rest}
135141
>
136-
<div className="fr-container">
137-
<div className="fr-notice__body">
142+
<div className={fr.cx("fr-container")}>
143+
<div className={fr.cx("fr-notice__body")}>
138144
<p className={cx(fr.cx(`fr-notice__title`), classes.title)}>{title}</p>
139145
{/* TODO: Use our button once we have one */}
140146
{isClosable && (

0 commit comments

Comments
 (0)