-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.tsx
52 lines (43 loc) · 1.07 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { useMemo } from 'react';
import { isNotDefined } from '@togglecorp/fujs';
interface Props<T> {
diffContainerClassName?: string;
value?: T;
oldValue?: T;
children: React.ReactNode;
enabled: boolean;
showOnlyDiff?: boolean;
}
function DiffWrapper<T>(props: Props<T>) {
const {
diffContainerClassName,
oldValue,
value,
children,
enabled = false,
showOnlyDiff,
} = props;
const hasChanged = useMemo(() => {
// NOTE: we consider `null` and `undefined` as same for
// this scenario
if (isNotDefined(oldValue) && isNotDefined(value)) {
return false;
}
return JSON.stringify(oldValue) !== JSON.stringify(value);
}, [oldValue, value]);
if (!enabled) {
return children;
}
if (!hasChanged && showOnlyDiff) {
return null;
}
if (!hasChanged) {
return children;
}
return (
<div className={diffContainerClassName}>
{children}
</div>
);
}
export default DiffWrapper;