Skip to content

Commit 91adab6

Browse files
committed
Merge branch 'master' of https://github.com/dxc-technology/halstack-react into PelayoFelgueroso/improve-migration_documentation
2 parents 88af0e7 + 5f1708d commit 91adab6

File tree

4 files changed

+34
-36
lines changed

4 files changed

+34
-36
lines changed

apps/website/screens/components/alert/code/AlertCodePage.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ const sections = [
4040
<TableCode>boolean</TableCode>
4141
</td>
4242
<td>
43-
If true, the alert will have a close button that will remove the message from the alert, only in banner
44-
and inline modes. The rest of the functionality will depend on the <Code>onClose</Code> event of each
45-
message (e.g. closing the modal alert).
43+
If true, the alert will have a close button that will call the <Code>onClose</Code>.
4644
</td>
4745
<td>
4846
<TableCode>true</TableCode>
@@ -58,9 +56,9 @@ const sections = [
5856
<ExtendedTableCode>{messageTypeString}</ExtendedTableCode>
5957
</td>
6058
<td>
61-
List of messages to be displayed. Each message has a close action that will, apart from remove from the
62-
alert the current message, call the <Code>onClose</Code> if it is defined. If the message is an array, the
63-
alert will show a navigation bar to move between the messages. <br />
59+
List of messages to be displayed. Each message has a close action that will call the <Code>onClose</Code>{" "}
60+
if it is defined. If the message is an array, the alert will show a navigation bar to move between the
61+
messages. <br />
6462
The <Code>modal</Code> mode only allows one message per alert. In this case, the message must be an object
6563
and the presence of the <Code>onClose</Code> attribute is mandatory, since the management of the closing
6664
of the alert relies completely on the user.

apps/website/screens/components/alert/code/examples/severalMessages.tsx

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
11
import { DxcAlert, DxcInset, DxcFlex } from "@dxc-technology/halstack-react";
2+
import { useState } from "react";
23

34
const code = `() => {
4-
const messages = [
5-
{ text: "Your document as been auto-saved." },
6-
{ text: "You can continue working without worrying, as all changes are automatically saved." },
7-
{ text: "You can also go back to the previous version of the document." },
8-
];
5+
const [messages, setMessages] = useState([
6+
{
7+
text: "Your document as been auto-saved.",
8+
onClose: () => {
9+
setMessages((prevMessages) => prevMessages.filter(({ text }) => text !== "Your document as been auto-saved."));
10+
},
11+
},
12+
{
13+
text: "You can continue working without worrying, as all changes are automatically saved.",
14+
onClose: () => {
15+
setMessages((prevMessages) =>
16+
prevMessages.filter(
17+
({ text }) => text !== "You can continue working without worrying, as all changes are automatically saved."
18+
)
19+
);
20+
},
21+
},
22+
{
23+
text: "You can also go back to the previous version of the document.",
24+
onClose: () => {
25+
setMessages((prevMessages) =>
26+
prevMessages.filter(({ text }) => text !== "You can also go back to the previous version of the document.")
27+
);
28+
},
29+
},
30+
]);
931
1032
return (
1133
<DxcInset space="var(--spacing-padding-xl)">
@@ -25,6 +47,7 @@ const scope = {
2547
DxcAlert,
2648
DxcInset,
2749
DxcFlex,
50+
useState,
2851
};
2952

3053
export default { code, scope };

packages/lib/src/alert/Alert.test.tsx

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,6 @@ describe("Alert component tests", () => {
6868
fireEvent.click(closeButton);
6969
expect(onClose).toHaveBeenCalled();
7070
});
71-
test("Alert with several messages closes properly each one", () => {
72-
const { getByRole, getByText } = render(<DxcAlert title="Info" message={messages} />);
73-
const closeButton = getByRole("button", { name: "Close message" });
74-
const nextButton = getByRole("button", { name: "Next message" });
75-
expect(getByText("1 of 4")).toBeTruthy();
76-
expect(getByText("Message 1")).toBeTruthy();
77-
fireEvent.click(closeButton);
78-
expect(getByText("Message 2")).toBeTruthy();
79-
expect(getByText("1 of 3")).toBeTruthy();
80-
fireEvent.click(nextButton);
81-
fireEvent.click(nextButton);
82-
expect(getByText("Message 4")).toBeTruthy();
83-
expect(getByText("3 of 3")).toBeTruthy();
84-
fireEvent.click(closeButton);
85-
expect(getByText("Message 3")).toBeTruthy();
86-
expect(getByText("2 of 2")).toBeTruthy();
87-
fireEvent.click(closeButton);
88-
expect(getByRole("button", { name: "Close alert" })).toBeTruthy();
89-
expect(getByText("Message 2")).toBeTruthy();
90-
});
9171
test("Alert actions are correctly called when pressed", () => {
9272
const primaryAction = jest.fn();
9373
const secondaryAction = jest.fn();

packages/lib/src/alert/Alert.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import styled from "@emotion/styled";
22
import { css } from "@emotion/react";
3-
import { useState, useId, useEffect, useCallback, useContext } from "react";
3+
import { useState, useId, useEffect, useCallback, useContext, useMemo } from "react";
44
import AlertPropsType from "./types";
55
import DxcIcon from "../icon/Icon";
66
import DxcDivider from "../divider/Divider";
@@ -101,7 +101,7 @@ const DxcAlert = ({
101101
semantic = "info",
102102
title = "",
103103
}: AlertPropsType) => {
104-
const [messages, setMessages] = useState(Array.isArray(message) ? message : [message]);
104+
const messages = useMemo(() => (Array.isArray(message) ? message : [message]), [message]);
105105
const [currentIndex, setCurrentIndex] = useState(0);
106106

107107
const id = useId();
@@ -116,9 +116,6 @@ const DxcAlert = ({
116116

117117
const handleOnClose = useCallback(() => {
118118
messages[currentIndex]?.onClose?.();
119-
if (mode !== "modal") {
120-
setMessages((prevMessages) => prevMessages.filter((_, index) => index !== currentIndex));
121-
}
122119
}, [messages, currentIndex, mode]);
123120

124121
useEffect(() => {

0 commit comments

Comments
 (0)