Skip to content

Commit 625bc9d

Browse files
committed
feat: add an optional options object to the alert function
1 parent 99a6cc4 commit 625bc9d

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ To show an alert to the user, call the `alert` function. It has the following pa
160160
| Parameter | Required | Description |
161161
| --- | --- | --- |
162162
| message | yes | The message to display. |
163-
| severity | yes | The severity of the alert. |
163+
| severityOrOptions | yes | The severity of the alert or an options object. |
164164

165165
### Confirm
166166

src/Component.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface State {
66
isOpen: boolean,
77
severity: Service.AlertSeverity,
88
message: string,
9-
9+
duration?: number,
1010
},
1111
confirm: {
1212
isOpen: boolean,
@@ -127,7 +127,7 @@ class ConfirmComponentHost extends React.Component<Props, State> {
127127
public override render (): React.ReactNode {
128128
const { alert, confirm, choice } = this.state;
129129
const { renderAlert, renderConfirm, renderChoice, alertDurations } = this.props;
130-
const autoHideDuration = alertDurations?.[alert.severity] ?? defaultDurations[alert.severity];
130+
const autoHideDuration = alert.duration ?? alertDurations?.[alert.severity] ?? defaultDurations[alert.severity];
131131

132132
return (
133133
<Fragment>
@@ -154,19 +154,20 @@ class ConfirmComponentHost extends React.Component<Props, State> {
154154
);
155155
}
156156

157-
private readonly showAlert = (message: string, severity: Service.AlertSeverity): void => {
157+
private readonly showAlert = (message: string, severityOrOptions: Service.AlertSeverity | Service.AlertOptions): void => {
158158
const { alert } = this.state;
159159

160160
if (alert.isOpen) {
161161
this.hideAlert();
162162

163-
setTimeout(() => this.showAlert(message, severity), 10);
163+
setTimeout(() => this.showAlert(message, severityOrOptions), 10);
164164
} else {
165165
this.setState({
166166
alert: {
167167
isOpen: true,
168168
message,
169-
severity,
169+
severity: typeof severityOrOptions === "string" ? severityOrOptions : severityOrOptions.severity,
170+
duration: typeof severityOrOptions === "string" ? undefined : severityOrOptions.duration,
170171
},
171172
});
172173
}

src/Service.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
type AlertSeverity = "error" | "warning" | "info" | "success";
2-
type AlertFunc = (message: string, severity: AlertSeverity) => void;
2+
type AlertFunc = (message: string, severityOrOptions: AlertSeverity | AlertOptions) => void;
33
type ConfirmFunc = (options: ConfirmOptions, callback: (result: boolean) => void) => void;
44
type ChooseFunc = (props: ChooseOptions, callback: (result: Option | null) => void) => void;
55

6+
interface AlertOptions {
7+
/**
8+
* The severity of the alert.
9+
*/
10+
severity: AlertSeverity,
11+
/**
12+
* The duration of the alert in milliseconds.
13+
*/
14+
duration?: number,
15+
}
16+
617
interface ConfirmOptions {
718
/**
819
* The title of the confirmation.
@@ -80,16 +91,16 @@ const ConfirmService = {
8091
/**
8192
* Shows an alert.
8293
* @param message The message of the alert.
83-
* @param severity The severity of the alert.
94+
* @param severityOrOptions The severity of the alert or an options object.
8495
*/
85-
alert (this: void, message: string, severity: AlertSeverity): void {
96+
alert (this: void, message: string, severityOrOptions: AlertSeverity | AlertOptions): void {
8697
const handlers = getCurrentHandlers();
8798

8899
if (!handlers) {
89100
throw new Error("ConfirmService is not initialized.");
90101
}
91102

92-
handlers.alert(message, severity);
103+
handlers.alert(message, severityOrOptions);
93104
},
94105
/**
95106
* Shows a confirmation.
@@ -144,6 +155,7 @@ const ConfirmService = {
144155
export type {
145156
AlertSeverity,
146157
Handlers,
158+
AlertOptions,
147159
ConfirmOptions,
148160
Option,
149161
ChooseOptions,

tests/Service.spec.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addHandlers, ChooseOptions, ConfirmOptions, ConfirmService, Handlers, Option, removeHandlers } from "../src/Service";
1+
import { addHandlers, AlertOptions, ChooseOptions, ConfirmOptions, ConfirmService, Handlers, Option, removeHandlers } from "../src/Service";
22

33
describe("Service", () => {
44
const mockAlert = jest.fn();
@@ -19,7 +19,8 @@ describe("Service", () => {
1919
});
2020

2121
describe("alert", () => {
22-
it("calls the provided alert function", () => {
22+
it("calls the provided alert function with a severity", () => {
23+
// arrange
2324
const message = "Message";
2425
const severity = "error";
2526

@@ -31,6 +32,22 @@ describe("Service", () => {
3132
expect(mockAlert).toHaveBeenCalledWith(message, severity);
3233
});
3334

35+
it("calls the provided alert function with an options object", () => {
36+
// arrange
37+
const message = "Message";
38+
const options: AlertOptions = {
39+
severity: "error",
40+
duration: 1000,
41+
};
42+
43+
// act
44+
ConfirmService.alert(message, options);
45+
46+
// assert
47+
expect(alert.alert).toHaveBeenCalledTimes(1);
48+
expect(mockAlert).toHaveBeenCalledWith(message, options);
49+
});
50+
3451
it("throws if not initialized", () => {
3552
// arrange
3653
removeHandlers(alert);

0 commit comments

Comments
 (0)