From 11bc7f6b95696b5e7222341f4b799c9d65e40115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Tue, 2 Mar 2021 16:54:33 +0800 Subject: [PATCH] fix: hooks should support update (#161) * fix: hooks should support update * test: Fix lint * remove tsconfig in gitignore --- .gitignore | 1 - src/useNotification.tsx | 21 +++++++++++-- tests/hooks.test.tsx | 67 ++++++++++++++++++++++++++++++++++++++--- tsconfig.json | 16 ++++++++++ 4 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore index de3a2aa..07c0219 100644 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,3 @@ es coverage yarn.lock package-lock.json -tsconfig.json diff --git a/src/useNotification.tsx b/src/useNotification.tsx index 4664f72..b0ed9c3 100644 --- a/src/useNotification.tsx +++ b/src/useNotification.tsx @@ -1,5 +1,6 @@ import * as React from 'react'; -import Notification, { NoticeFunc, NoticeContent } from './Notification'; +import type { NoticeFunc, NoticeContent } from './Notification'; +import type Notification from './Notification'; import Notice from './Notice'; export default function useNotification( @@ -9,14 +10,28 @@ export default function useNotification( const [elements, setElements] = React.useState([]); function notify(noticeProps: NoticeContent) { + let firstMount = true; notificationInstance.add(noticeProps, (div, props) => { const { key } = props; - if (div && !createdRef.current[key]) { + if (div && (!createdRef.current[key] || firstMount)) { const noticeEle = ; createdRef.current[key] = noticeEle; - setElements(originElements => [...originElements, noticeEle]); + + setElements((originElements) => { + const index = originElements.findIndex((ele) => ele.key === props.key); + + if (index === -1) { + return [...originElements, noticeEle]; + } + + const cloneList = [...originElements]; + cloneList[index] = noticeEle; + return cloneList; + }); } + + firstMount = false; }); } diff --git a/tests/hooks.test.tsx b/tests/hooks.test.tsx index b02eae1..2e3f6da 100644 --- a/tests/hooks.test.tsx +++ b/tests/hooks.test.tsx @@ -1,12 +1,13 @@ import React from 'react'; -import { mount, ReactWrapper } from 'enzyme'; +import type { ReactWrapper } from 'enzyme'; +import { mount } from 'enzyme'; import Notification from '../src'; -import { NotificationInstance } from '../src/Notification'; +import type { NotificationInstance } from '../src/Notification'; require('../assets/index.less'); async function timeout(delay = 0) { - return new Promise(resolve => { + return new Promise((resolve) => { setTimeout(resolve, delay); }); } @@ -24,7 +25,7 @@ describe('Notification.Hooks', () => { wrapper = mount(
{node}
); }, } as any, - notification => { + (notification) => { instance = notification; }, ); @@ -64,4 +65,62 @@ describe('Notification.Hooks', () => { instance.destroy(); }); + + it('key replace', async () => { + let instance: NotificationInstance; + + let wrapper: ReactWrapper; + Notification.newInstance( + { + TEST_RENDER: (node: React.ReactElement) => { + wrapper = mount(
{node}
); + }, + } as any, + (notification) => { + instance = notification; + }, + ); + + await timeout(0); + + const Demo = () => { + const [notify, holder] = instance.useNotification(); + return ( + <> +