react-native-molecules 0.5.0-beta.3 → 0.5.0-beta.5

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.
@@ -1,5 +1,7 @@
1
1
  import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2
2
 
3
+ import useLatest from './useLatest';
4
+
3
5
  type ReturnType<T> = [T, (value: T, ...args: any[]) => void];
4
6
 
5
7
  type Args<T> = {
@@ -31,21 +33,35 @@ const useControlledValue = <T,>({
31
33
 
32
34
  const isUncontrolled = useRef(valueProp).current === undefined;
33
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);
34
40
 
35
41
  const updateValue = useCallback(
36
42
  (val: T, ...rest: any[]) => {
37
43
  if (disabled) return;
38
44
 
39
45
  if (isUncontrolled) {
40
- setValue(manipulateValue(val, uncontrolledValue));
46
+ setValue(manipulateValueRef.current(val, uncontrolledValueRef.current));
41
47
  }
42
48
 
43
- onChange?.(
44
- manipulateValue(val, isUncontrolled ? uncontrolledValue : valueProp),
49
+ onChangeRef.current?.(
50
+ manipulateValueRef.current(
51
+ val,
52
+ isUncontrolled ? uncontrolledValueRef.current : valuePropRef.current,
53
+ ),
45
54
  ...rest,
46
55
  );
47
56
  },
48
- [disabled, isUncontrolled, manipulateValue, onChange, uncontrolledValue, valueProp],
57
+ [
58
+ disabled,
59
+ isUncontrolled,
60
+ manipulateValueRef,
61
+ onChangeRef,
62
+ uncontrolledValueRef,
63
+ valuePropRef,
64
+ ],
49
65
  );
50
66
 
51
67
  useEffect(() => {
@@ -0,0 +1,48 @@
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-molecules",
3
- "version": "0.5.0-beta.3",
3
+ "version": "0.5.0-beta.5",
4
4
  "author": "Thet Aung <thetaung.dev@gmail.com>",
5
5
  "license": "MIT",
6
6
  "main": "index.ts",
@@ -47,10 +47,6 @@
47
47
  "color": "^4.2.3",
48
48
  "date-fns": "^4.1.0",
49
49
  "eventemitter2": "^6.4.9",
50
- "lodash.get": "^4.4.2",
51
- "lodash.keyby": "^4.6.0",
52
- "lodash.memoize": "^4.1.2",
53
- "lodash.omitby": "^4.6.0",
54
50
  "use-sync-external-store": "^1.2.0"
55
51
  },
56
52
  "peerDependencies": {
@@ -64,11 +60,6 @@
64
60
  "@react-native-documents/picker": "^10.1.2",
65
61
  "@types/color": "^3.0.3",
66
62
  "@types/jest": "^29.1.2",
67
- "@types/lodash": "^4.17.13",
68
- "@types/lodash.get": "^4.4.7",
69
- "@types/lodash.keyby": "^4.6.7",
70
- "@types/lodash.memoize": "^4.1.7",
71
- "@types/lodash.omitby": "^4.6.7",
72
63
  "@types/react": "~19.1.10",
73
64
  "@types/use-sync-external-store": "^1.5.0",
74
65
  "react": "19.1.0",
@@ -10,9 +10,12 @@ import {
10
10
  type ShortcutsManagerProps,
11
11
  } from './utils';
12
12
 
13
+ const defaultScopes: ShortcutsManagerProps['scopes'] = [];
14
+ const defaultShortcuts: Shortcut[] = [];
15
+
13
16
  const _ShortcutsManager = ({ shortcuts, scopes, children }: ShortcutsManagerProps) => {
14
- const shortcutsRef = useRef<Shortcut[]>(shortcuts || []);
15
- const scopesRef = useRef(scopes);
17
+ const shortcutsRef = useRef<Shortcut[]>(shortcuts || defaultShortcuts);
18
+ const scopesRef = useRef(scopes || defaultScopes);
16
19
  const parentContextRef = useContext(ShortcutsManagerContext);
17
20
 
18
21
  const contextValue = useMemo(() => {
package/styles/shadow.ts CHANGED
@@ -8,7 +8,8 @@ export const inputRange: MD3Elevation[] = [0, 1, 2, 3, 4, 5];
8
8
  export const shadowHeight = [0, 1, 2, 4, 6, 8];
9
9
  export const shadowRadius = [0, 3, 6, 8, 10, 12];
10
10
 
11
- export default function shadow(elevation: number) {
11
+ export default function shadow(_elevation: number) {
12
+ const elevation = typeof _elevation === 'number' ? (_elevation > 5 ? 5 : _elevation) : 0;
12
13
  return {
13
14
  shadowColor: MD3_SHADOW_COLOR,
14
15
  shadowOpacity: elevation ? MD3_SHADOW_OPACITY : 0,
package/utils/lodash.ts CHANGED
@@ -1,8 +1,80 @@
1
- import memoize from 'lodash.memoize';
2
- export { default as get } from 'lodash.get';
3
- export { default as keyBy } from 'lodash.keyby';
4
- export { default as memoize } from 'lodash.memoize';
5
- export { default as omitBy } from 'lodash.omitby';
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
+ };
6
78
 
7
79
  export const isNil = (value: unknown): value is null | undefined => value == null;
8
80
  export const noop = () => {};