Skip to content

Commit 068c953

Browse files
authored
feat: Change duration to number|false type (#379)
* feat: Change duration to number|false type * feat: update readme * feat: update
1 parent 558b2cf commit 068c953

File tree

6 files changed

+20
-19
lines changed

6 files changed

+20
-19
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ props details:
9797
<thead>
9898
<tr>
9999
<th style="width: 100px;">name</th>
100-
<th style="width: 50px;">type</th>
100+
<th style="width: 200px;">type</th>
101101
<th style="width: 50px;">default</th>
102102
<th>description</th>
103103
</tr>
@@ -129,9 +129,9 @@ props details:
129129
</tr>
130130
<tr>
131131
<td>duration</td>
132-
<td>number</td>
133-
<td>1.5</td>
134-
<td>after duration of time, this notice will disappear.(seconds)</td>
132+
<td>number | false</td>
133+
<td>4.5</td>
134+
<td>The delay for automatic closing in seconds. Set to 0 or false to not close automatically.</td>
135135
</tr>
136136
<tr>
137137
<td>showProgress</td>

src/Notice.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
3838
const [percent, setPercent] = React.useState(0);
3939
const [spentTime, setSpentTime] = React.useState(0);
4040
const mergedHovering = forcedHovering || hovering;
41-
const mergedShowProgress = duration > 0 && showProgress;
41+
const mergedDuration: number = typeof duration === 'number' ? duration : 0;
42+
const mergedShowProgress = mergedDuration > 0 && showProgress;
4243

4344
// ======================== Close =========================
4445
const onInternalClose = () => {
@@ -53,13 +54,13 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
5354

5455
// ======================== Effect ========================
5556
React.useEffect(() => {
56-
if (!mergedHovering && duration > 0) {
57+
if (!mergedHovering && mergedDuration > 0) {
5758
const start = Date.now() - spentTime;
5859
const timeout = setTimeout(
5960
() => {
6061
onInternalClose();
6162
},
62-
duration * 1000 - spentTime,
63+
mergedDuration * 1000 - spentTime,
6364
);
6465

6566
return () => {
@@ -70,7 +71,7 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
7071
};
7172
}
7273
// eslint-disable-next-line react-hooks/exhaustive-deps
73-
}, [duration, mergedHovering, times]);
74+
}, [mergedDuration, mergedHovering, times]);
7475

7576
React.useEffect(() => {
7677
if (!mergedHovering && mergedShowProgress && (pauseOnHover || spentTime === 0)) {
@@ -81,7 +82,7 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
8182
cancelAnimationFrame(animationFrame);
8283
animationFrame = requestAnimationFrame((timestamp) => {
8384
const runtime = timestamp + spentTime - start;
84-
const progress = Math.min(runtime / (duration * 1000), 1);
85+
const progress = Math.min(runtime / (mergedDuration * 1000), 1);
8586
setPercent(progress * 100);
8687
if (progress < 1) {
8788
calculate();
@@ -98,7 +99,7 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
9899
};
99100
}
100101
// eslint-disable-next-line react-hooks/exhaustive-deps
101-
}, [duration, spentTime, mergedHovering, mergedShowProgress, times]);
102+
}, [mergedDuration, spentTime, mergedHovering, mergedShowProgress, times]);
102103

103104
// ======================== Closable ========================
104105
const closableObj = React.useMemo(() => {

src/hooks/useNotification.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface NotificationConfig {
1919
| boolean
2020
| ({ closeIcon?: React.ReactNode; onClose?: VoidFunction } & React.AriaAttributes);
2121
maxCount?: number;
22-
duration?: number;
22+
duration?: number | false | null;
2323
showProgress?: boolean;
2424
pauseOnHover?: boolean;
2525
/** @private. Config for notification holder style. Safe to remove if refactor */

src/interface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type NoticeSemanticProps = 'wrapper';
66

77
export interface NoticeConfig {
88
content?: React.ReactNode;
9-
duration?: number | null;
9+
duration?: number | false | null;
1010
showProgress?: boolean;
1111
pauseOnHover?: boolean;
1212

@@ -32,7 +32,7 @@ export interface OpenConfig extends NoticeConfig {
3232
key: React.Key;
3333
placement?: Placement;
3434
content?: React.ReactNode;
35-
duration?: number | null;
35+
duration?: number | false | null;
3636
}
3737

3838
export type InnerOpenConfig = OpenConfig & { times?: number };

tests/index.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ describe('Notification.Basic', () => {
162162
</p>
163163
),
164164
key,
165-
duration: null,
165+
duration: false,
166166
});
167167
});
168168

@@ -192,7 +192,7 @@ describe('Notification.Basic', () => {
192192
{notUpdatableValue}
193193
</p>
194194
),
195-
duration: null,
195+
duration: false,
196196
});
197197
});
198198

@@ -204,7 +204,7 @@ describe('Notification.Basic', () => {
204204
</p>
205205
),
206206
key,
207-
duration: null,
207+
duration: false,
208208
});
209209
});
210210

@@ -490,7 +490,7 @@ describe('Notification.Basic', () => {
490490
</p>
491491
),
492492
key,
493-
duration: null,
493+
duration: false,
494494
onClick: () => {
495495
clicked += 1;
496496
},

tests/stack.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('stack', () => {
1717
onClick={() => {
1818
api.open({
1919
content: <div className="context-content">Test</div>,
20-
duration: null,
20+
duration: false,
2121
});
2222
}}
2323
/>
@@ -56,7 +56,7 @@ describe('stack', () => {
5656
onClick={() => {
5757
api.open({
5858
content: <div className="context-content">Test</div>,
59-
duration: null,
59+
duration: false,
6060
closable: true,
6161
});
6262
}}

0 commit comments

Comments
 (0)