Line data Source code
1 : /// Compare two style objects shallowly
2 1 : function shallowEqualStyle(a: object | undefined, b: object | undefined): boolean {
3 1 : if (a === b) return true;
4 1 : if (a == null || b == null) return false;
5 1 : const aKeys = Object.keys(a);
6 1 : if (aKeys.length !== Object.keys(b).length) return false;
7 1 : for (const key of aKeys) {
8 1 : if ((a as Record<string, unknown>)[key] !== (b as Record<string, unknown>)[key]) return false;
9 : }
10 1 : return true;
11 1 : }
12 :
13 : /// Shallow props comparison for React.memo that also compares the `style` prop shallowly.
14 : /// Cells receive fresh style literals from their parents on every render.
15 1 : export function arePropsEqualWithStyle<P extends object>(prev: Readonly<P>, next: Readonly<P>): boolean {
16 1 : const prevKeys = Object.keys(prev) as (keyof P)[];
17 1 : if (prevKeys.length !== Object.keys(next).length) return false;
18 1 : for (const key of prevKeys) {
19 1 : if (key === 'style') {
20 1 : if (!shallowEqualStyle(prev[key] as object | undefined, next[key] as object | undefined)) return false;
21 1 : } else if (!Object.is(prev[key], next[key])) {
22 1 : return false;
23 : }
24 : }
25 1 : return true;
26 1 : }
|