react-native-molecules 0.5.0-beta.23 → 0.5.0-beta.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/Accordion/Accordion.tsx +1 -1
- package/components/Accordion/AccordionItem.tsx +1 -1
- package/components/Checkbox/Checkbox.tsx +2 -1
- package/components/DateField/useDateFieldState.ts +2 -2
- package/components/DatePicker/DatePickerProvider.tsx +1 -1
- package/components/DatePickerInline/DatePickerInline.tsx +1 -1
- package/components/DatePickerInline/DatePickerInlineBase.tsx +1 -1
- package/components/DatePickerInline/Day.tsx +1 -1
- package/components/DatePickerInline/Swiper.tsx +1 -1
- package/components/DatePickerInline/SwiperUtils.ts +1 -1
- package/components/DatePickerInline/dateUtils.tsx +1 -1
- package/components/DatePickerInline/store.tsx +2 -1
- package/components/Divider/index.tsx +2 -3
- package/components/ElementGroup/ElementGroup.tsx +1 -1
- package/components/FilePicker/FilePicker.tsx +1 -1
- package/components/Icon/iconFactory.tsx +2 -1
- package/components/IconButton/IconButton.tsx +39 -13
- package/components/IconButton/index.tsx +1 -0
- package/components/IconButton/types.ts +2 -0
- package/components/List/List.tsx +2 -1
- package/components/List/context.tsx +2 -1
- package/components/Portal/Portal.tsx +1 -2
- package/components/RadioButton/RadioButtonGroup.tsx +1 -2
- package/components/Rating/Rating.tsx +1 -1
- package/components/Select/Select.tsx +103 -34
- package/components/Select/context.tsx +3 -1
- package/components/Select/index.ts +20 -2
- package/components/Select/types.ts +2 -0
- package/components/Select/utils.ts +11 -4
- package/components/Switch/Switch.ios.tsx +1 -1
- package/components/Switch/Switch.tsx +2 -1
- package/components/Tabs/Tabs.tsx +2 -2
- package/components/TextInput/TextInput.tsx +4 -3
- package/components/TimePicker/AnalogClock.tsx +1 -1
- package/components/TimePicker/TimeInputs.tsx +1 -1
- package/components/TimePicker/TimePicker.tsx +1 -1
- package/components/TimePicker/TimePickerModal.tsx +1 -1
- package/components/Tooltip/Tooltip.tsx +1 -1
- package/components/TouchableRipple/TouchableRipple.tsx +1 -1
- package/hocs/index.tsx +1 -1
- package/hocs/withKeyboardAccessibility.tsx +2 -3
- package/hooks/index.tsx +2 -6
- package/hooks/useContrastColor.ts +1 -2
- package/hooks/useFilePicker.tsx +1 -1
- package/hooks/useHandleNumberFormat.tsx +2 -2
- package/hooks/useMediaQuery.tsx +1 -2
- package/package.json +95 -118
- package/shortcuts-manager/ShortcutsManager/ShortcutsManager.tsx +1 -1
- package/shortcuts-manager/ShortcutsManager/utils.tsx +1 -1
- package/shortcuts-manager/useSetScopes/useSetScopes.tsx +1 -1
- package/shortcuts-manager/useShortcut/useShortcut.tsx +1 -1
- package/utils/extractTextStyles.ts +1 -2
- package/utils/formatNumberWithMask/formatNumberWithMask.ts +2 -1
- package/utils/index.ts +0 -3
- package/utils/normalizeToNumberString/normalizeToNumberString.ts +1 -1
- package/context-bridge/index.tsx +0 -87
- package/fast-context/index.tsx +0 -190
- package/hocs/typedMemo.tsx +0 -5
- package/hooks/useControlledValue.tsx +0 -84
- package/hooks/useLatest.tsx +0 -9
- package/hooks/useMergedRefs.ts +0 -14
- package/hooks/usePrevious.ts +0 -13
- package/hooks/useToggle.tsx +0 -24
- package/hooks/useWhatHasUpdated.tsx +0 -48
- package/utils/color.ts +0 -22
- package/utils/compare/index.ts +0 -54
- package/utils/lodash.ts +0 -121
- package/utils/repository.ts +0 -53
package/context-bridge/index.tsx
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import type { ComponentType, Context as ContextType, PropsWithChildren } from 'react';
|
|
2
|
-
import { Fragment, useContext, useId, useMemo, useRef, useState } from 'react';
|
|
3
|
-
|
|
4
|
-
import typedMemo from '../hocs/typedMemo';
|
|
5
|
-
import { Repository } from '../utils/repository';
|
|
6
|
-
|
|
7
|
-
// https://github.com/pmndrs/its-fine/blob/main/src/index.tsx
|
|
8
|
-
// In development, React will warn about using contexts between renderers.
|
|
9
|
-
// Hide the warning because its-fine fixes this issue
|
|
10
|
-
// https://github.com/facebook/react/pull/12779
|
|
11
|
-
function wrapContext<T>(context: React.Context<T>): React.Context<T> {
|
|
12
|
-
try {
|
|
13
|
-
return Object.defineProperties(context, {
|
|
14
|
-
_currentRenderer: {
|
|
15
|
-
get() {
|
|
16
|
-
return null;
|
|
17
|
-
},
|
|
18
|
-
set() {},
|
|
19
|
-
},
|
|
20
|
-
_currentRenderer2: {
|
|
21
|
-
get() {
|
|
22
|
-
return null;
|
|
23
|
-
},
|
|
24
|
-
set() {},
|
|
25
|
-
},
|
|
26
|
-
});
|
|
27
|
-
} catch (_) {
|
|
28
|
-
return context;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export const createContextBridge = <T extends object>(
|
|
33
|
-
bridgeName: string,
|
|
34
|
-
Wrapper: ComponentType<T>,
|
|
35
|
-
contexts: ContextType<any>[] = [],
|
|
36
|
-
) => {
|
|
37
|
-
const respository = new Repository<ContextType<any>[]>({
|
|
38
|
-
name: bridgeName,
|
|
39
|
-
onRegister: (arg, name, registry) => {
|
|
40
|
-
return [registry[name] ?? [], arg].flat();
|
|
41
|
-
},
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
return {
|
|
45
|
-
registerContextToBridge: (updatedContexts: ContextType<any> | ContextType<any>[]) => {
|
|
46
|
-
respository.register(
|
|
47
|
-
'contexts',
|
|
48
|
-
([] as ContextType<any>[])
|
|
49
|
-
.concat(updatedContexts)
|
|
50
|
-
.map(context => wrapContext(context)),
|
|
51
|
-
);
|
|
52
|
-
},
|
|
53
|
-
BridgedComponent: typedMemo((props: PropsWithChildren<T> & { name?: string }) => {
|
|
54
|
-
const { name, ...rest } = props;
|
|
55
|
-
const contextValuesRef = useRef<any[]>([]);
|
|
56
|
-
|
|
57
|
-
const id = useId();
|
|
58
|
-
|
|
59
|
-
const [allContexts] = useState(() =>
|
|
60
|
-
Array.from(new Set([...contexts, ...Object.values(respository.getAll()).flat()])),
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
for (const i in allContexts) {
|
|
64
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
65
|
-
contextValuesRef.current[i] = useContext(allContexts[i]);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const content = useMemo(() => {
|
|
69
|
-
return allContexts.reduce((acc, Context, currentIndex) => {
|
|
70
|
-
return (
|
|
71
|
-
<Context.Provider value={contextValuesRef.current[currentIndex]}>
|
|
72
|
-
{acc}
|
|
73
|
-
</Context.Provider>
|
|
74
|
-
);
|
|
75
|
-
}, <>{props.children}</>);
|
|
76
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
77
|
-
}, [...contextValuesRef.current, allContexts, props.children]);
|
|
78
|
-
|
|
79
|
-
const _key = name ? name + id : id;
|
|
80
|
-
return (
|
|
81
|
-
<Wrapper name={name} {...(rest as T)}>
|
|
82
|
-
<Fragment key={_key}>{content}</Fragment>
|
|
83
|
-
</Wrapper>
|
|
84
|
-
);
|
|
85
|
-
}),
|
|
86
|
-
};
|
|
87
|
-
};
|
package/fast-context/index.tsx
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type Context,
|
|
3
|
-
createContext,
|
|
4
|
-
type ReactNode,
|
|
5
|
-
type RefObject,
|
|
6
|
-
useCallback,
|
|
7
|
-
useContext,
|
|
8
|
-
useEffect,
|
|
9
|
-
useMemo,
|
|
10
|
-
useRef,
|
|
11
|
-
} from 'react';
|
|
12
|
-
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/with-selector';
|
|
13
|
-
|
|
14
|
-
import typedMemo from '../hocs/typedMemo';
|
|
15
|
-
import { usePrevious } from '../hooks';
|
|
16
|
-
import { shallowCompare } from '../utils';
|
|
17
|
-
|
|
18
|
-
type StoreDataType = Record<string, any>;
|
|
19
|
-
|
|
20
|
-
type SelectorOutputType<IStore, SelectorOutput> = (store: IStore) => SelectorOutput;
|
|
21
|
-
|
|
22
|
-
type UseStoreDataReturnType<T> = {
|
|
23
|
-
get: () => T;
|
|
24
|
-
set: (value: (prev: T) => Partial<T>) => void;
|
|
25
|
-
store: RefObject<T>;
|
|
26
|
-
subscribe: (callback: () => void) => () => void;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const useStoreData = <IStore extends StoreDataType>(
|
|
30
|
-
value: IStore,
|
|
31
|
-
defaultValue: IStore | null,
|
|
32
|
-
watch: boolean = false,
|
|
33
|
-
): UseStoreDataReturnType<IStore> => {
|
|
34
|
-
const store = useRef<IStore>({ ...defaultValue, ...(value as IStore) });
|
|
35
|
-
const watchRef = useRef(watch);
|
|
36
|
-
|
|
37
|
-
const get = useCallback(() => store.current, [store]);
|
|
38
|
-
|
|
39
|
-
const subscribers = useRef(new Set<() => void>());
|
|
40
|
-
|
|
41
|
-
const set = useCallback(
|
|
42
|
-
(callback: (prev: IStore) => Partial<IStore>) => {
|
|
43
|
-
store.current = { ...store.current, ...callback(store.current) };
|
|
44
|
-
subscribers.current.forEach(subscriber => subscriber());
|
|
45
|
-
},
|
|
46
|
-
[store],
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
const subscribe = useCallback((callback: () => void) => {
|
|
50
|
-
subscribers.current.add(callback);
|
|
51
|
-
|
|
52
|
-
return () => subscribers.current.delete(callback);
|
|
53
|
-
}, []);
|
|
54
|
-
|
|
55
|
-
/** The effect is required to trigger the updates on the consumers */
|
|
56
|
-
useEffect(() => {
|
|
57
|
-
if (!watchRef.current) return;
|
|
58
|
-
set(prev => ({ ...prev, ...value }));
|
|
59
|
-
}, [set, value]);
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Cases:
|
|
63
|
-
* 1. when the value updates, we want the data to be updated immediately
|
|
64
|
-
* 2. the data stored in store.current may not be current with regards to the parent and the parent must have dropped the references related to the data.
|
|
65
|
-
* 3. because store.current is a ref, on watch and change, if we update store.current, no side-effect introduced.
|
|
66
|
-
*/
|
|
67
|
-
if (usePrevious(value).current !== value && watchRef.current) {
|
|
68
|
-
store.current = { ...store.current, ...value };
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return useMemo(
|
|
72
|
-
() => ({
|
|
73
|
-
get,
|
|
74
|
-
set,
|
|
75
|
-
subscribe,
|
|
76
|
-
store,
|
|
77
|
-
}),
|
|
78
|
-
[get, set, subscribe],
|
|
79
|
-
);
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
export const createFastContext = <T extends StoreDataType = {}>(
|
|
83
|
-
defaultValue: T | null = null,
|
|
84
|
-
watch: boolean = false,
|
|
85
|
-
) => {
|
|
86
|
-
const context = createContext<UseStoreDataReturnType<T> | null>(null);
|
|
87
|
-
|
|
88
|
-
return {
|
|
89
|
-
/**
|
|
90
|
-
* using the value from here will never cause a rerender as context is based on refs.
|
|
91
|
-
*/
|
|
92
|
-
useStoreRef: createUseRefContext(context),
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
*
|
|
96
|
-
* the value is memoized and thus changing the value will have no effect.
|
|
97
|
-
* use key prop to unmount and remount if necessary. alternatively use set from the context to update the value.
|
|
98
|
-
*
|
|
99
|
-
*/
|
|
100
|
-
Provider: createProvider<T>(
|
|
101
|
-
context as Context<UseStoreDataReturnType<T>>,
|
|
102
|
-
defaultValue,
|
|
103
|
-
watch,
|
|
104
|
-
),
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
*
|
|
108
|
-
* @param selector
|
|
109
|
-
* @param equalityCheck
|
|
110
|
-
* @returns tuple, first item is return value of the selector and the second item is setter
|
|
111
|
-
*
|
|
112
|
-
*/
|
|
113
|
-
useContext: <SelectorOutput,>(
|
|
114
|
-
selector: SelectorOutputType<T, SelectorOutput>,
|
|
115
|
-
equalityCheck = shallowCompare,
|
|
116
|
-
) => useStore(context as Context<UseStoreDataReturnType<T>>, selector, equalityCheck),
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
*
|
|
120
|
-
* @param selector
|
|
121
|
-
* @param equalityCheck
|
|
122
|
-
* @returns return value of the selector
|
|
123
|
-
*
|
|
124
|
-
*/
|
|
125
|
-
useContextValue: <SelectorOutput,>(
|
|
126
|
-
selector: SelectorOutputType<T, SelectorOutput>,
|
|
127
|
-
equalityCheck = shallowCompare,
|
|
128
|
-
) => useStoreValue(context as Context<UseStoreDataReturnType<T>>, selector, equalityCheck),
|
|
129
|
-
/**
|
|
130
|
-
* context of the store. Useful for ContextBridge
|
|
131
|
-
*/
|
|
132
|
-
Context: context,
|
|
133
|
-
};
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
export const createProvider = <T extends Record<string, any> = {}>(
|
|
137
|
-
StoreContext: Context<UseStoreDataReturnType<T>>,
|
|
138
|
-
defaultValue: T | null,
|
|
139
|
-
watch: boolean,
|
|
140
|
-
) =>
|
|
141
|
-
typedMemo(({ value, children }: { value: T; children: ReactNode }) => {
|
|
142
|
-
return (
|
|
143
|
-
<StoreContext.Provider value={useStoreData<T>(value, defaultValue, watch) as any}>
|
|
144
|
-
{children}
|
|
145
|
-
</StoreContext.Provider>
|
|
146
|
-
);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
export const createUseRefContext = <T,>(_Context: Context<UseStoreDataReturnType<T> | null>) => {
|
|
150
|
-
return () => {
|
|
151
|
-
const value = useContext<UseStoreDataReturnType<T> | null>(_Context);
|
|
152
|
-
if (value === null)
|
|
153
|
-
throw 'Fast Context requires the value to be wrapped in a Provider with a value.';
|
|
154
|
-
|
|
155
|
-
return value;
|
|
156
|
-
};
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
export const useStore = <IStore extends StoreDataType, SelectorOutput>(
|
|
160
|
-
_Context: Context<UseStoreDataReturnType<IStore>>,
|
|
161
|
-
selector: SelectorOutputType<IStore, SelectorOutput>,
|
|
162
|
-
equalityFn = Object.is,
|
|
163
|
-
): [SelectorOutput, (value: (prev: IStore) => Partial<IStore>) => void] => {
|
|
164
|
-
const store = useContext(_Context);
|
|
165
|
-
if (!store) {
|
|
166
|
-
throw new Error('Store not found');
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const state = useSyncExternalStoreWithSelector(
|
|
170
|
-
store.subscribe,
|
|
171
|
-
() => {
|
|
172
|
-
return store.get();
|
|
173
|
-
},
|
|
174
|
-
undefined,
|
|
175
|
-
snapshot => {
|
|
176
|
-
return selector(snapshot);
|
|
177
|
-
},
|
|
178
|
-
equalityFn,
|
|
179
|
-
);
|
|
180
|
-
|
|
181
|
-
return [state, store.set];
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
export const useStoreValue = <IStore extends StoreDataType, SelectorOutput>(
|
|
185
|
-
_Context: Context<UseStoreDataReturnType<IStore>>,
|
|
186
|
-
selector: SelectorOutputType<IStore, SelectorOutput>,
|
|
187
|
-
equalityFn = Object.is,
|
|
188
|
-
) => {
|
|
189
|
-
return useStore(_Context, selector, equalityFn)[0];
|
|
190
|
-
};
|
package/hocs/typedMemo.tsx
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
-
|
|
3
|
-
import useLatest from './useLatest';
|
|
4
|
-
|
|
5
|
-
type ReturnType<T> = [T, (value: T, ...args: any[]) => void];
|
|
6
|
-
|
|
7
|
-
type Args<T> = {
|
|
8
|
-
value?: T;
|
|
9
|
-
defaultValue?: T;
|
|
10
|
-
onChange?: (value: T, ...args: any[]) => any;
|
|
11
|
-
disabled?: boolean;
|
|
12
|
-
manipulateValue?: (value: T | undefined, prevValue: T | undefined) => T;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const defaultManipulateValue = (val: any) => val;
|
|
16
|
-
|
|
17
|
-
const useControlledValue = <T,>({
|
|
18
|
-
value: valueProp,
|
|
19
|
-
defaultValue,
|
|
20
|
-
disabled = false,
|
|
21
|
-
onChange,
|
|
22
|
-
manipulateValue = defaultManipulateValue,
|
|
23
|
-
}: Args<T>): ReturnType<T> => {
|
|
24
|
-
const value = useMemo(
|
|
25
|
-
() =>
|
|
26
|
-
valueProp !== undefined
|
|
27
|
-
? manipulateValue(valueProp, undefined)
|
|
28
|
-
: manipulateValue(defaultValue, undefined),
|
|
29
|
-
[defaultValue, manipulateValue, valueProp],
|
|
30
|
-
);
|
|
31
|
-
|
|
32
|
-
const hasWarnedRef = useRef(false);
|
|
33
|
-
|
|
34
|
-
const isUncontrolled = useRef(valueProp).current === undefined;
|
|
35
|
-
const [uncontrolledValue, setValue] = useState(value);
|
|
36
|
-
const valuePropRef = useLatest(valueProp);
|
|
37
|
-
const onChangeRef = useLatest(onChange);
|
|
38
|
-
const manipulateValueRef = useLatest(manipulateValue);
|
|
39
|
-
const uncontrolledValueRef = useLatest(uncontrolledValue);
|
|
40
|
-
|
|
41
|
-
const updateValue = useCallback(
|
|
42
|
-
(val: T, ...rest: any[]) => {
|
|
43
|
-
if (disabled) return;
|
|
44
|
-
|
|
45
|
-
if (isUncontrolled) {
|
|
46
|
-
setValue(manipulateValueRef.current(val, uncontrolledValueRef.current));
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
onChangeRef.current?.(
|
|
50
|
-
manipulateValueRef.current(
|
|
51
|
-
val,
|
|
52
|
-
isUncontrolled ? uncontrolledValueRef.current : valuePropRef.current,
|
|
53
|
-
),
|
|
54
|
-
...rest,
|
|
55
|
-
);
|
|
56
|
-
},
|
|
57
|
-
[
|
|
58
|
-
disabled,
|
|
59
|
-
isUncontrolled,
|
|
60
|
-
manipulateValueRef,
|
|
61
|
-
onChangeRef,
|
|
62
|
-
uncontrolledValueRef,
|
|
63
|
-
valuePropRef,
|
|
64
|
-
],
|
|
65
|
-
);
|
|
66
|
-
|
|
67
|
-
useEffect(() => {
|
|
68
|
-
if (hasWarnedRef.current) return;
|
|
69
|
-
hasWarnedRef.current = true;
|
|
70
|
-
|
|
71
|
-
if (valueProp !== undefined && isUncontrolled) {
|
|
72
|
-
console.warn(
|
|
73
|
-
'Trying to change the value from uncontrolled to controlled can lead to inconsistencies',
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
}, [isUncontrolled, valueProp]);
|
|
77
|
-
|
|
78
|
-
return useMemo(
|
|
79
|
-
() => [isUncontrolled ? uncontrolledValue : value, updateValue],
|
|
80
|
-
[isUncontrolled, uncontrolledValue, updateValue, value],
|
|
81
|
-
);
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
export default useControlledValue;
|
package/hooks/useLatest.tsx
DELETED
package/hooks/useMergedRefs.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { useCallback } from 'react';
|
|
2
|
-
|
|
3
|
-
import { mergeRefs } from '../utils';
|
|
4
|
-
import useLatest from './useLatest';
|
|
5
|
-
|
|
6
|
-
export const useMergedRefs: typeof mergeRefs = refs => {
|
|
7
|
-
const latestRefs = useLatest(refs);
|
|
8
|
-
return useCallback(
|
|
9
|
-
value => {
|
|
10
|
-
mergeRefs(latestRefs.current)(value);
|
|
11
|
-
},
|
|
12
|
-
[latestRefs],
|
|
13
|
-
);
|
|
14
|
-
};
|
package/hooks/usePrevious.ts
DELETED
package/hooks/useToggle.tsx
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { useCallback, useMemo, useState } from 'react';
|
|
2
|
-
|
|
3
|
-
const useToggle = (initialState = false) => {
|
|
4
|
-
const [state, setState] = useState<boolean>(initialState);
|
|
5
|
-
|
|
6
|
-
const toggle = useCallback(() => setState(prevState => !prevState), []);
|
|
7
|
-
|
|
8
|
-
const handleClose = useCallback(() => setState(false), [setState]);
|
|
9
|
-
|
|
10
|
-
const handleOpen = useCallback(() => setState(true), [setState]);
|
|
11
|
-
|
|
12
|
-
return useMemo(
|
|
13
|
-
() => ({
|
|
14
|
-
state,
|
|
15
|
-
onToggle: toggle,
|
|
16
|
-
setState,
|
|
17
|
-
handleOpen,
|
|
18
|
-
handleClose,
|
|
19
|
-
}),
|
|
20
|
-
[state, toggle, setState, handleOpen, handleClose],
|
|
21
|
-
);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export default useToggle;
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { useEffect, useMemo } from 'react';
|
|
2
|
-
|
|
3
|
-
import useLatest from './useLatest';
|
|
4
|
-
|
|
5
|
-
const map = new Map();
|
|
6
|
-
|
|
7
|
-
export const whatHasUpdatedFactory = <T extends Record<string, unknown>>(
|
|
8
|
-
name: string,
|
|
9
|
-
prevObject: T,
|
|
10
|
-
{ debug = false, useCached = false } = {},
|
|
11
|
-
) => {
|
|
12
|
-
const getPrev = () => {
|
|
13
|
-
if (!map.has(name)) map.set(name, prevObject);
|
|
14
|
-
return useCached ? map.get(name) : prevObject;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const setPrev = (value: T) => {
|
|
18
|
-
if (useCached) map.set(name, value);
|
|
19
|
-
else prevObject = value;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
return (nextObject: T) => {
|
|
23
|
-
const old = getPrev();
|
|
24
|
-
const changes = Object.keys({ ...nextObject, ...old }).reduce((all, key) => {
|
|
25
|
-
const newValue = nextObject?.[key];
|
|
26
|
-
const oldValue = old[key];
|
|
27
|
-
if (oldValue === newValue) return all;
|
|
28
|
-
return { ...all, [key]: { newValue, oldValue } };
|
|
29
|
-
}, {});
|
|
30
|
-
|
|
31
|
-
setPrev(nextObject);
|
|
32
|
-
|
|
33
|
-
if (!debug && !Object.keys(changes).length) return;
|
|
34
|
-
// eslint-disable-next-line no-console
|
|
35
|
-
console.log('🚨🕵️ UPDATED', name, changes);
|
|
36
|
-
};
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export const useWhatHasUpdated = (name: string, props: Record<string, any>) => {
|
|
40
|
-
const argRef = useLatest({ name, props });
|
|
41
|
-
const checkFunc = useMemo(
|
|
42
|
-
() => whatHasUpdatedFactory(argRef.current.name, argRef.current.props),
|
|
43
|
-
[argRef],
|
|
44
|
-
);
|
|
45
|
-
useEffect(() => {
|
|
46
|
-
checkFunc(props);
|
|
47
|
-
});
|
|
48
|
-
};
|
package/utils/color.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import color from 'color';
|
|
2
|
-
|
|
3
|
-
import { weakMemoized } from './lodash';
|
|
4
|
-
|
|
5
|
-
export const resolveContrastColor = weakMemoized(
|
|
6
|
-
(
|
|
7
|
-
bgColor: string,
|
|
8
|
-
lightColor: string = '#fff',
|
|
9
|
-
darkColor: string = '#000',
|
|
10
|
-
isDarkMode?: boolean,
|
|
11
|
-
) => {
|
|
12
|
-
let isLightColor = !isDarkMode;
|
|
13
|
-
try {
|
|
14
|
-
// TODO: Account for transparency.
|
|
15
|
-
isLightColor = color(bgColor).isLight();
|
|
16
|
-
} finally {
|
|
17
|
-
return isLightColor ? darkColor : lightColor;
|
|
18
|
-
}
|
|
19
|
-
},
|
|
20
|
-
);
|
|
21
|
-
|
|
22
|
-
export default color;
|
package/utils/compare/index.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* inlined Object.is polyfill to avoid requiring consumers ship their own
|
|
5
|
-
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
|
|
6
|
-
*/
|
|
7
|
-
function is(x: any, y: any) {
|
|
8
|
-
// SameValue algorithm
|
|
9
|
-
if (x === y) {
|
|
10
|
-
// Steps 1-5, 7-10
|
|
11
|
-
// Steps 6.b-6.e: +0 != -0
|
|
12
|
-
return x !== 0 || 1 / x === 1 / y;
|
|
13
|
-
} else {
|
|
14
|
-
// Step 6.a: NaN == NaN
|
|
15
|
-
return x !== x && y !== y;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Performs equality by iterating through keys on an object and returning false
|
|
21
|
-
* when any key has values which are not strictly equal between the arguments.
|
|
22
|
-
* Returns true when the values of all keys are strictly equal.
|
|
23
|
-
*/
|
|
24
|
-
export function shallowCompare(objA?: unknown, objB?: unknown) {
|
|
25
|
-
if (is(objA, objB)) {
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const keysA = Object.keys(objA);
|
|
34
|
-
const keysB = Object.keys(objB);
|
|
35
|
-
|
|
36
|
-
if (keysA.length !== keysB.length) {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Test for A's keys different from B.
|
|
41
|
-
for (let i = 0; i < keysA.length; i++) {
|
|
42
|
-
if (
|
|
43
|
-
!hasOwnProperty.call(objB, keysA[i]) ||
|
|
44
|
-
!is(
|
|
45
|
-
(objA as Record<string, unknown>)[keysA[i]],
|
|
46
|
-
(objB as Record<string, unknown>)[keysA[i]],
|
|
47
|
-
)
|
|
48
|
-
) {
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return true;
|
|
54
|
-
}
|
package/utils/lodash.ts
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
type MemoizeCache<K = any, V = any> = {
|
|
2
|
-
has(key: K): boolean;
|
|
3
|
-
get(key: K): V | undefined;
|
|
4
|
-
set(key: K, value: V): unknown;
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
type MemoizeFn = {
|
|
8
|
-
<T extends (...args: any[]) => any>(
|
|
9
|
-
func: T,
|
|
10
|
-
resolver?: (...args: Parameters<T>) => any,
|
|
11
|
-
): MemoizedFunction<T>;
|
|
12
|
-
Cache: { new (): MemoizeCache<any, any> };
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export interface MemoizedFunction<T extends (...args: any[]) => any> {
|
|
16
|
-
(...args: Parameters<T>): ReturnType<T>;
|
|
17
|
-
cache: MemoizeCache<any, ReturnType<T>>;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const memoize: MemoizeFn = ((
|
|
21
|
-
func: (...args: any[]) => any,
|
|
22
|
-
resolver?: (...args: any[]) => any,
|
|
23
|
-
) => {
|
|
24
|
-
if (typeof func !== 'function') {
|
|
25
|
-
throw new TypeError('Expected a function');
|
|
26
|
-
}
|
|
27
|
-
const memoized = function (this: unknown, ...args: any[]) {
|
|
28
|
-
const key = resolver ? resolver.apply(this, args) : args[0];
|
|
29
|
-
const { cache } = memoized as MemoizedFunction<typeof func>;
|
|
30
|
-
if (cache.has(key)) {
|
|
31
|
-
return cache.get(key);
|
|
32
|
-
}
|
|
33
|
-
const result = func.apply(this, args);
|
|
34
|
-
cache.set(key, result);
|
|
35
|
-
return result;
|
|
36
|
-
} as MemoizedFunction<typeof func>;
|
|
37
|
-
|
|
38
|
-
(memoized as MemoizedFunction<typeof func>).cache = new memoize.Cache();
|
|
39
|
-
return memoized;
|
|
40
|
-
}) as MemoizeFn;
|
|
41
|
-
|
|
42
|
-
memoize.Cache = Map;
|
|
43
|
-
|
|
44
|
-
type Iteratee<T> = ((item: T) => string | number | symbol) | keyof T;
|
|
45
|
-
|
|
46
|
-
export const keyBy = <T extends Record<string, any>>(
|
|
47
|
-
collection: T[],
|
|
48
|
-
iteratee: Iteratee<T>,
|
|
49
|
-
): Record<string, T> => {
|
|
50
|
-
const result: Record<string, T> = {};
|
|
51
|
-
if (!Array.isArray(collection)) return result;
|
|
52
|
-
|
|
53
|
-
const getter = typeof iteratee === 'function' ? iteratee : (item: T) => item[iteratee];
|
|
54
|
-
for (const item of collection) {
|
|
55
|
-
const key = getter(item);
|
|
56
|
-
if (key != null) {
|
|
57
|
-
result[String(key)] = item;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return result;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
type Predicate = (value: any, key: string) => boolean;
|
|
64
|
-
|
|
65
|
-
export const omitBy = <T extends Record<string, any>>(
|
|
66
|
-
object: T,
|
|
67
|
-
predicate: Predicate,
|
|
68
|
-
): Partial<T> => {
|
|
69
|
-
if (object == null) return {};
|
|
70
|
-
const result: Partial<T> = {};
|
|
71
|
-
for (const [key, value] of Object.entries(object)) {
|
|
72
|
-
if (!predicate(value, key)) {
|
|
73
|
-
result[key as keyof T] = value;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return result;
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
export const isNil = (value: unknown): value is null | undefined => value == null;
|
|
80
|
-
export const noop = () => {};
|
|
81
|
-
|
|
82
|
-
const uniqueIdFactory = () => {
|
|
83
|
-
let number = Number.MAX_SAFE_INTEGER;
|
|
84
|
-
return () => number--;
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
const getUniqueId = uniqueIdFactory();
|
|
88
|
-
const weakMemoize = Object.assign(memoize.bind(null), { Cache: WeakMap });
|
|
89
|
-
const getObjectMemoryAddress = weakMemoize((x: unknown | null) => x && getUniqueId());
|
|
90
|
-
|
|
91
|
-
export const allArgumentResolver = (...args: unknown[]) =>
|
|
92
|
-
args
|
|
93
|
-
.map(x => {
|
|
94
|
-
const type = typeof x;
|
|
95
|
-
switch (type) {
|
|
96
|
-
case 'object':
|
|
97
|
-
case 'function':
|
|
98
|
-
return type.slice(0, 2)! + getObjectMemoryAddress(x);
|
|
99
|
-
default:
|
|
100
|
-
return type.slice(0, 2)! + String(x);
|
|
101
|
-
}
|
|
102
|
-
})
|
|
103
|
-
.join('_');
|
|
104
|
-
|
|
105
|
-
export const createMemoizedFunction = ({
|
|
106
|
-
resolver = allArgumentResolver,
|
|
107
|
-
Cache = Map,
|
|
108
|
-
}: {
|
|
109
|
-
resolver?: (...args: any[]) => any;
|
|
110
|
-
Cache?: typeof memoize.Cache;
|
|
111
|
-
} = {}) => {
|
|
112
|
-
const memo = Object.assign(memoize.bind(null), { Cache });
|
|
113
|
-
return Object.assign(
|
|
114
|
-
<T extends (...args: any[]) => any>(func: T, resolverOverwride?: (...args: any[]) => any) =>
|
|
115
|
-
memo(func, resolverOverwride ?? resolver),
|
|
116
|
-
);
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
export const weakMemoized = createMemoizedFunction({
|
|
120
|
-
Cache: WeakMap,
|
|
121
|
-
});
|