react-native-molecules 0.5.0-beta.22 → 0.5.0-beta.24
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/Button/Button.tsx +3 -1
- package/components/Checkbox/Checkbox.tsx +2 -1
- package/components/DateField/useDateFieldState.ts +2 -2
- package/components/DatePicker/DatePickerProvider.tsx +1 -1
- package/components/DatePicker/utils.ts +2 -0
- package/components/DatePickerInline/DatePickerInline.tsx +1 -1
- package/components/DatePickerInline/DatePickerInlineBase.tsx +1 -1
- package/components/DatePickerInline/Day.tsx +1 -1
- package/components/DatePickerInline/MonthPicker.tsx +24 -40
- package/components/DatePickerInline/Swiper.tsx +1 -1
- package/components/DatePickerInline/SwiperUtils.ts +1 -1
- package/components/DatePickerInline/YearPicker.tsx +44 -79
- 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 +156 -387
- package/components/List/context.tsx +4 -5
- package/components/List/index.ts +0 -1
- package/components/List/types.ts +77 -109
- package/components/List/utils.ts +4 -37
- package/components/Menu/Menu.tsx +13 -30
- package/components/Menu/index.tsx +0 -2
- package/components/Popover/Popover.tsx +7 -10
- package/components/Popover/PopoverRoot.tsx +6 -20
- package/components/Popover/common.ts +4 -0
- package/components/Popover/index.ts +2 -8
- package/components/Popover/usePlatformMeasure.ts +4 -2
- 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 +304 -71
- package/components/Select/context.tsx +30 -3
- package/components/Select/index.ts +20 -2
- package/components/Select/types.ts +43 -25
- package/components/Select/utils.ts +18 -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 +76 -152
- 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 +5 -28
- 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
|
@@ -1,8 +1,7 @@
|
|
|
1
|
+
import { resolveContrastColor } from '@react-native-molecules/utils/helpers/resolveContrastColor';
|
|
1
2
|
import { useMemo } from 'react';
|
|
2
3
|
import { useUnistyles } from 'react-native-unistyles';
|
|
3
4
|
|
|
4
|
-
import { resolveContrastColor } from '../utils/color';
|
|
5
|
-
|
|
6
5
|
export const useContrastColor = (bgColor: string, lightColor?: string, darkColor?: string) => {
|
|
7
6
|
const isDarkMode = useUnistyles().theme.dark;
|
|
8
7
|
|
package/hooks/useFilePicker.tsx
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isNil, omitBy } from '@react-native-molecules/utils/helpers/lodash';
|
|
1
2
|
import { useCallback } from 'react';
|
|
2
3
|
|
|
3
4
|
import {
|
|
@@ -5,7 +6,6 @@ import {
|
|
|
5
6
|
type DocumentPickerOptions,
|
|
6
7
|
type DocumentResult,
|
|
7
8
|
} from '../utils/DocumentPicker';
|
|
8
|
-
import { isNil, omitBy } from '../utils/lodash';
|
|
9
9
|
|
|
10
10
|
export const useFilePicker = (options: DocumentPickerOptions) => {
|
|
11
11
|
const openFilePicker = useCallback(
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
import { isNil } from '@react-native-molecules/utils/helpers/lodash';
|
|
2
|
+
import { usePrevious } from '@react-native-molecules/utils/hooks';
|
|
1
3
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
4
|
import type { NativeSyntheticEvent, TextInputFocusEventData } from 'react-native';
|
|
3
5
|
|
|
4
6
|
import { formatNumberWithMask, normalizeToNumberString } from '../utils';
|
|
5
|
-
import { isNil } from '../utils/lodash';
|
|
6
|
-
import usePrevious from './usePrevious';
|
|
7
7
|
|
|
8
8
|
export type NumberMaskConfig = {
|
|
9
9
|
prefix?: string;
|
package/hooks/useMediaQuery.tsx
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
+
import { isNil } from '@react-native-molecules/utils/helpers/lodash';
|
|
1
2
|
import { useMemo } from 'react';
|
|
2
3
|
import { useWindowDimensions } from 'react-native';
|
|
3
4
|
|
|
4
|
-
import { isNil } from '../utils/lodash';
|
|
5
|
-
|
|
6
5
|
type Query = {
|
|
7
6
|
maxWidth?: number;
|
|
8
7
|
minWidth?: number;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-molecules",
|
|
3
|
-
"version": "0.5.0-beta.
|
|
3
|
+
"version": "0.5.0-beta.24",
|
|
4
4
|
"author": "Thet Aung <thetaung.dev@gmail.com>",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "index.ts",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
"components/DatePickerInline/store.tsx",
|
|
10
10
|
"components/List/context.tsx",
|
|
11
11
|
"components/Select/context.tsx",
|
|
12
|
-
"components/TimePicker/context.tsx"
|
|
12
|
+
"components/TimePicker/context.tsx",
|
|
13
|
+
"components/Popover/common.ts"
|
|
13
14
|
],
|
|
14
15
|
"files": [
|
|
15
16
|
"components",
|
|
@@ -48,6 +49,7 @@
|
|
|
48
49
|
"perf": "npx reassure"
|
|
49
50
|
},
|
|
50
51
|
"dependencies": {
|
|
52
|
+
"@react-native-molecules/utils": "workspace:^",
|
|
51
53
|
"@gorhom/portal": "^1.0.14",
|
|
52
54
|
"@radix-ui/react-compose-refs": "^1.1.2",
|
|
53
55
|
"@react-native-vector-icons/feather": "^12.4.0",
|
|
@@ -89,30 +91,5 @@
|
|
|
89
91
|
"test-cases/node_modules",
|
|
90
92
|
"dist",
|
|
91
93
|
"cache"
|
|
92
|
-
]
|
|
93
|
-
"react-native-builder-bob": {
|
|
94
|
-
"source": "src",
|
|
95
|
-
"output": "lib",
|
|
96
|
-
"targets": [
|
|
97
|
-
[
|
|
98
|
-
"commonjs",
|
|
99
|
-
{
|
|
100
|
-
"configFile": true
|
|
101
|
-
}
|
|
102
|
-
],
|
|
103
|
-
[
|
|
104
|
-
"module",
|
|
105
|
-
{
|
|
106
|
-
"configFile": true
|
|
107
|
-
}
|
|
108
|
-
],
|
|
109
|
-
[
|
|
110
|
-
"typescript",
|
|
111
|
-
{
|
|
112
|
-
"project": "tsconfig.build.json",
|
|
113
|
-
"rootDir": "src"
|
|
114
|
-
}
|
|
115
|
-
]
|
|
116
|
-
]
|
|
117
|
-
}
|
|
94
|
+
]
|
|
118
95
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { keyBy } from '@react-native-molecules/utils/helpers/lodash';
|
|
1
2
|
import { memo, useContext, useEffect, useMemo, useRef } from 'react';
|
|
2
3
|
|
|
3
|
-
import { keyBy } from '../../utils/lodash';
|
|
4
4
|
import EventsManager from '../EventsManager';
|
|
5
5
|
import type { Shortcut } from '../types';
|
|
6
6
|
import {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { createFastContext } from '@react-native-molecules/utils/fast-context';
|
|
1
2
|
import type { ReactNode } from 'react';
|
|
2
3
|
|
|
3
|
-
import { createFastContext } from '../../fast-context';
|
|
4
4
|
import type { Scope, Shortcut } from '../types';
|
|
5
5
|
|
|
6
6
|
export type ShortcutsManagerContextType = {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { keyBy } from '@react-native-molecules/utils/helpers/lodash';
|
|
1
2
|
import { useCallback } from 'react';
|
|
2
3
|
|
|
3
|
-
import { keyBy } from '../../utils/lodash';
|
|
4
4
|
import { useShortcutsManagerStoreRef } from '../ShortcutsManager/utils';
|
|
5
5
|
import type { Scope } from '../types';
|
|
6
6
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { useLatest } from '@react-native-molecules/utils/hooks';
|
|
1
2
|
import { useCallback, useEffect } from 'react';
|
|
2
3
|
|
|
3
|
-
import useLatest from '../../hooks/useLatest';
|
|
4
4
|
import { useShortcutsManagerStoreRef } from '../ShortcutsManager';
|
|
5
5
|
import type { ShortcutEventDetail } from '../types';
|
|
6
6
|
import { calculateShortcutEventName } from '../utils';
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { isNil } from '@react-native-molecules/utils/helpers/lodash';
|
|
2
|
+
|
|
1
3
|
import { createNumberMask, type CreateNumberMaskProps } from '../createNumberMask';
|
|
2
|
-
import { isNil } from '../lodash';
|
|
3
4
|
import { formatWithMask } from './formatWithMask';
|
|
4
5
|
|
|
5
6
|
export type FormatNumberWithMaskProps = CreateNumberMaskProps & {
|
package/utils/index.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
export { addEventListener, addListener } from './addEventListener';
|
|
2
2
|
export { BackgroundContext } from './backgroundContext';
|
|
3
|
-
export { default as color, resolveContrastColor } from './color';
|
|
4
|
-
export * from './compare';
|
|
5
3
|
export { default as composeEventHandlers } from './composeEventHandlers';
|
|
6
4
|
export { createNumberMask, type CreateNumberMaskProps } from './createNumberMask';
|
|
7
5
|
export * from './createSyntheticEvent';
|
|
@@ -16,5 +14,4 @@ export {
|
|
|
16
14
|
normalizeToNumberString,
|
|
17
15
|
type NormalizeToNumberStringProps,
|
|
18
16
|
} from './normalizeToNumberString';
|
|
19
|
-
export { Repository } from './repository';
|
|
20
17
|
export * from './resolveStateVariant';
|
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
|
-
};
|