Skip to content

Commit 58731a5

Browse files
committed
1 parent 78930a0 commit 58731a5

File tree

3 files changed

+50
-31
lines changed

3 files changed

+50
-31
lines changed

src/Alert.tsx

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@ export type AlertProps = {
2727
as?: `h${2 | 3 | 4 | 5 | 6}`;
2828
classes?: Partial<Record<"root" | "title" | "description" | "close", string>>;
2929
style?: CSSProperties;
30-
} & (AlertProps.DefaultSize | AlertProps.Small) &
31-
(AlertProps.NonClosable | AlertProps.Closable);
30+
31+
/** Display the cross icon (understand isClosableByUser) */
32+
closable?: boolean;
33+
/** To provide if you want the Alert to be controlled */
34+
isClosed?: boolean;
35+
onClose?: () => void;
36+
} & (AlertProps.DefaultSize | AlertProps.Small);
3237

3338
export namespace AlertProps {
3439
export type DefaultSize = {
@@ -45,30 +50,6 @@ export namespace AlertProps {
4550
description: NonNullable<ReactNode>;
4651
};
4752

48-
export type NonClosable = {
49-
/** Default false */
50-
closable?: false;
51-
isClosed?: never;
52-
onClose?: never;
53-
};
54-
55-
export type Closable = {
56-
/** Default false */
57-
closable: true;
58-
} & (Closable.Controlled | Closable.Uncontrolled);
59-
60-
export namespace Closable {
61-
export type Controlled = {
62-
isClosed: boolean;
63-
onClose: () => void;
64-
};
65-
66-
export type Uncontrolled = {
67-
isClosed?: never;
68-
onClose?: () => void;
69-
};
70-
}
71-
7253
type ExtractSeverity<FrClassName> = FrClassName extends `fr-alert--${infer Severity}`
7354
? Exclude<Severity, "sm">
7455
: never;
@@ -89,7 +70,7 @@ export const Alert = memo(
8970
small: isSmall,
9071
title,
9172
description,
92-
closable: isClosable = false,
73+
closable: isClosableByUser = false,
9374
isClosed: props_isClosed,
9475
onClose,
9576
...rest
@@ -145,7 +126,7 @@ export const Alert = memo(
145126
onClose?.();
146127
} else {
147128
//Controlled
148-
onClose();
129+
onClose?.();
149130
}
150131
});
151132

@@ -174,7 +155,7 @@ export const Alert = memo(
174155
</HtmlTitleTag>
175156
)}
176157
<DescriptionTag className={classes.description}>{description}</DescriptionTag>
177-
{isClosable && (
158+
{isClosableByUser && (
178159
<button
179160
ref={setButtonElement}
180161
className={cx(fr.cx("fr-link--close", "fr-link"), classes.close)}

stories/Alert.stories.tsx

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,45 @@ const { meta, getStory } = getStoryFactory({
99
"wrappedComponent": { Alert },
1010
"description": `
1111
- [See DSFR documentation](https://www.systeme-de-design.gouv.fr/elements-d-interface/composants/alerte)
12-
- [See source code](https://github.com/codegouvfr/react-dsfr/blob/main/src/Alert.tsx)`,
12+
- [See source code](https://github.com/codegouvfr/react-dsfr/blob/main/src/Alert.tsx)
13+
14+
## Uncontrolled mode
15+
16+
\`\`\`tsx
17+
import { Alert } from "@codegouvfr/react-dsfr/Alert";
18+
19+
<Alert
20+
severity="success"
21+
title="Message successfully sent"
22+
description="Everything went well"
23+
closable
24+
onClose={()=> alert("The user clicked the close button on the modal")}
25+
/>
26+
\`\`\`
27+
28+
## Controlled mode
29+
30+
\`\`\`tsx
31+
import { Alert } from "@codegouvfr/react-dsfr/Alert";
32+
import { useState } from "react";
33+
34+
const [ isClosed, setIsClosed ] = useState(false);
35+
36+
<Alert
37+
severity="success"
38+
title="Message successfully sent"
39+
description="Everything went well"
40+
isClosed={isClosed}
41+
// If the use should be able to close the modal manually
42+
closable
43+
// This is called only when the user clicks the close button
44+
onClose={()=> setIsClosed(true)}
45+
/>
46+
<button onClick={()=> setIsClosed(false)}>Open alert</button>
47+
<button onClick={()=> setIsClosed(true)}>Close alert</button>
48+
\`\`\`
49+
50+
`,
1351
"argTypes": {
1452
"severity": {
1553
"options": (() => {

test/integration/vite/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<!--<link rel="preload" href="/dsfr/fonts/Spectral-Regular.woff2?v=1.12.1" as="font" crossorigin="anonymous" />-->
2323
<!--<link rel="preload" href="/dsfr/fonts/Spectral-ExtraBold.woff2?v=1.12.1" as="font" crossorigin="anonymous" />-->
2424

25-
<link rel="stylesheet" href="/dsfr/utility/icons/icons.min.css?hash=f7c9104e" />
25+
<link rel="stylesheet" href="/dsfr/utility/icons/icons.min.css?hash=e7b25a4c" />
2626
<link rel="stylesheet" href="/dsfr/dsfr.min.css?v=1.12.1" />
2727

2828
<title>Vite + React + TS</title>

0 commit comments

Comments
 (0)