-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shared/utils): 抽取 transition 的 isSameEl 为公共 util isSameReactEl
- Loading branch information
1 parent
331730d
commit 53d207c
Showing
3 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
packages/shared/src/utils/__tests__/isSameReactEl.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { isSameReactEl } from '@pkg/shared'; | ||
|
||
describe('isSameReactEl', () => { | ||
const isSame = isSameReactEl; | ||
it('should return false when one of the two values is not a ReactElement', () => { | ||
expect(isSame(true, true)).toBeFalsy(); | ||
expect(isSame(false, true)).toBeFalsy(); | ||
expect(isSame(true, false)).toBeFalsy(); | ||
expect(isSame(false, false)).toBeFalsy(); | ||
expect(isSame(false, <div></div>)).toBeFalsy(); | ||
expect(isSame(<div></div>, true)).toBeFalsy(); | ||
}); | ||
|
||
it('should return false', () => { | ||
expect(isSame(<div></div>, <div></div>)).toBeFalsy(); | ||
expect(isSame(<div key={1}></div>, <div key={2}></div>)).toBeFalsy(); | ||
expect(isSame(<div key={1}></div>, <span key={1}></span>)).toBeFalsy(); | ||
}); | ||
|
||
it('should return true', () => { | ||
const div = <div></div>; | ||
expect(isSame(div, div)).toBeTruthy(); | ||
expect(isSame(<div key={1}></div>, <div key={1}></div>)).toBeTruthy(); | ||
expect(isSame(<div key={1}>123</div>, <div key={1}>456</div>)).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import React from 'react'; | ||
|
||
export function isSameReactEl(prev: unknown, next: unknown): boolean { | ||
if (!React.isValidElement(next)) return false; | ||
if (prev === next) return true; | ||
if (!React.isValidElement(prev)) return false; | ||
return prev.type === next.type && prev.key !== null && prev.key === next.key; | ||
} |