Skip to content

Commit 441ced5

Browse files
committed
feat: adds options to set the alert durations by severity
1 parent fd7192e commit 441ced5

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

README.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,13 @@ ConfirmService.choose({
9393

9494
The `ConfirmComponentHost` accepts the following props:
9595

96-
| Property | Description |
97-
| --- | --- |
98-
| renderAlert | Required. Provide a function which renders the alert component. See [renderAlert](#renderAlert) |
99-
| renderConfirm | Required. Provide a function which renders the confirmation component. See [renderConfirm](#renderConfirm) |
100-
| renderChoice | Optional. Provide a function which renders the choice component. See [renderChoice](#renderChoice) |
101-
| strings | Takes an object to provide default values for `yes`, `no`, and `cancel` button captions. Use this to localize these texts. |
96+
| Property | Required | Description |
97+
| --- | --- | --- |
98+
| renderAlert | yes | Provide a function which renders the alert component. See [renderAlert](#renderAlert) |
99+
| renderConfirm | yes | Provide a function which renders the confirmation component. See [renderConfirm](#renderConfirm) |
100+
| renderChoice | no | Provide a function which renders the choice component. See [renderChoice](#renderChoice) |
101+
| strings | no | Takes an object to provide default values for `yes`, `no`, and `cancel` button captions. Use this to localize these texts. |
102+
| alertDurations | no | You can provide an object to set the durations of an alert for each severity in ms. The defaults are: info: 3000, success: 3000, warning: 10000, error: 10000. |
102103

103104
### renderAlert
104105

@@ -145,32 +146,32 @@ The `ConfirmComponentHost` accepts the following props:
145146

146147
To show an alert to the user, call the `alert` function. It has the following parameters:
147148

148-
| Parameter | Description |
149-
| --- | ---
150-
| message | The message to display. |
151-
| severity | The severity of the alert. |
149+
| Parameter | Required | Description |
150+
| --- | --- | --- |
151+
| message | yes | The message to display. |
152+
| severity | yes | The severity of the alert. |
152153

153154
### Confirm
154155

155156
To show a confirmation to the user, use the `confirm` function. It takes one options parameter:
156157

157-
| Property | Description |
158-
| --- | --- |
159-
| title | The optional title of the confirmation. |
160-
| message | The message of the confirmation. |
161-
| yes | The caption of the button to accept. If not provided the `yes` property of `strings` is used. The default is "Yes". |
162-
| no | The caption of the button to deny. If not provided the `no` property of `strings` is used. The default is "No". If you pass `null`, the button is not displayed. |
158+
| Property | Required | Description |
159+
| --- | --- | --- |
160+
| title | no | The title of the confirmation. |
161+
| message | yes | The message of the confirmation. |
162+
| yes | no | The caption of the button to accept. If not provided the `yes` property of `strings` is used. The default is "Yes". |
163+
| no | no | The caption of the button to deny. If not provided the `no` property of `strings` is used. The default is "No". If you pass `null`, the button is not displayed. |
163164

164165
This function returns a `Promise`. It will be resolved if the confirmation is accepted and rejected if the confirmation is denied.
165166

166167
### Choose
167168

168169
To show a choice to the user, use the `choose` function. It takes one options parameter:
169170

170-
| Property | Description |
171-
| --- | --- |
172-
| title | The optional title of the choice. |
173-
| options | The possible choices. |
174-
| cancelCaption | The optional caption of the cancel action. If not provided the `cancel` property of `strings` is used. The default is "Cancel". |
171+
| Property | Required | Description |
172+
| --- | --- | --- |
173+
| title | no | The title of the choice. |
174+
| options | yes | The possible choices. |
175+
| cancelCaption | no | The caption of the cancel action. If not provided the `cancel` property of `strings` is used. The default is "Cancel". |
175176

176177
This function returns a `Promise`. It will be resolved with the selected option and rejected if the choice is cancelled.

src/Component.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ export interface ChoiceRenderProps {
5252
onCancel: () => void,
5353
}
5454

55+
type AlertDurations = Record<Service.AlertSeverity, number>;
56+
5557
export interface Props {
5658
renderAlert: (props: AlertRenderProps) => React.ReactElement,
5759
renderConfirm: (props: ConfirmRenderProps) => React.ReactElement,
@@ -61,8 +63,16 @@ export interface Props {
6163
no?: string,
6264
cancel?: string,
6365
},
66+
alertDurations?: AlertDurations,
6467
}
6568

69+
const defaultDurations: AlertDurations = {
70+
info: 3_000,
71+
success: 3_000,
72+
warning: 10_000,
73+
error: 10_000,
74+
};
75+
6676
export class ConfirmComponentHost extends React.Component<Props, State> {
6777
public constructor (props: Props) {
6878
super(props);
@@ -106,8 +116,8 @@ export class ConfirmComponentHost extends React.Component<Props, State> {
106116

107117
public override render (): React.ReactNode {
108118
const { alert, confirm, choice } = this.state;
109-
const { renderAlert, renderConfirm, renderChoice } = this.props;
110-
const autoHideDuration = alert.severity === "success" || alert.severity === "info" ? 3_000 : 10_000;
119+
const { renderAlert, renderConfirm, renderChoice, alertDurations } = this.props;
120+
const autoHideDuration = alertDurations?.[alert.severity] ?? defaultDurations[alert.severity];
111121

112122
return (
113123
<Fragment>

0 commit comments

Comments
 (0)