react-native-molecules 0.5.0-beta.1 → 0.5.0-beta.11

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.
Files changed (165) hide show
  1. package/README.md +87 -0
  2. package/components/Accordion/index.tsx +1 -6
  3. package/components/Accordion/utils.ts +17 -14
  4. package/components/ActivityIndicator/ActivityIndicator.tsx +12 -20
  5. package/components/ActivityIndicator/index.tsx +1 -5
  6. package/components/Appbar/index.tsx +1 -4
  7. package/components/Appbar/utils.ts +33 -21
  8. package/components/Avatar/index.tsx +1 -5
  9. package/components/Avatar/utils.ts +2 -6
  10. package/components/Backdrop/Backdrop.tsx +2 -2
  11. package/components/Backdrop/index.tsx +1 -5
  12. package/components/Backdrop/utils.ts +5 -6
  13. package/components/Badge/index.tsx +1 -5
  14. package/components/Badge/utils.ts +2 -6
  15. package/components/Button/Button.tsx +211 -264
  16. package/components/Button/index.tsx +9 -7
  17. package/components/Button/types.ts +16 -2
  18. package/components/Button/utils.ts +231 -210
  19. package/components/Card/Card.tsx +8 -4
  20. package/components/Card/CardContent.tsx +5 -4
  21. package/components/Card/CardHeader.tsx +5 -3
  22. package/components/Card/CardMedia.tsx +5 -3
  23. package/components/Card/CardTypography.tsx +5 -3
  24. package/components/Card/index.tsx +1 -5
  25. package/components/Card/utils.ts +5 -6
  26. package/components/Checkbox/Checkbox.tsx +1 -0
  27. package/components/Checkbox/CheckboxBase.ios.tsx +1 -0
  28. package/components/Checkbox/CheckboxBase.tsx +24 -128
  29. package/components/Checkbox/index.tsx +1 -5
  30. package/components/Checkbox/utils.ts +6 -31
  31. package/components/Chip/Chip.tsx +40 -52
  32. package/components/Chip/index.tsx +1 -5
  33. package/components/Chip/utils.ts +5 -13
  34. package/components/DatePickerDocked/index.tsx +1 -5
  35. package/components/DatePickerDocked/utils.ts +21 -19
  36. package/components/DatePickerInline/index.tsx +1 -5
  37. package/components/DatePickerInline/utils.ts +41 -28
  38. package/components/DatePickerInput/DatePickerInput.tsx +4 -2
  39. package/components/DatePickerInput/DatePickerInputWithoutModal.tsx +0 -4
  40. package/components/DatePickerInput/index.tsx +1 -5
  41. package/components/DatePickerInput/types.ts +1 -3
  42. package/components/DatePickerInput/utils.ts +5 -6
  43. package/components/DatePickerModal/CalendarEdit.tsx +10 -9
  44. package/components/DatePickerModal/DatePickerModalHeader.tsx +1 -1
  45. package/components/DatePickerModal/index.tsx +1 -5
  46. package/components/DatePickerModal/utils.ts +17 -16
  47. package/components/DateTimePicker/index.tsx +1 -5
  48. package/components/DateTimePicker/utils.ts +5 -6
  49. package/components/Dialog/index.tsx +1 -5
  50. package/components/Dialog/utils.ts +22 -16
  51. package/components/Drawer/Collapsible/utils.ts +13 -13
  52. package/components/Drawer/Drawer.tsx +2 -3
  53. package/components/Drawer/DrawerContent.tsx +5 -3
  54. package/components/Drawer/DrawerFooter.tsx +5 -4
  55. package/components/Drawer/DrawerHeader.tsx +5 -4
  56. package/components/Drawer/DrawerItem.tsx +5 -3
  57. package/components/Drawer/DrawerItemGroup.tsx +5 -4
  58. package/components/Drawer/index.tsx +1 -5
  59. package/components/Drawer/utils.ts +7 -7
  60. package/components/ElementGroup/ElementGroup.tsx +16 -14
  61. package/components/ElementGroup/index.tsx +1 -5
  62. package/components/ElementGroup/utils.ts +5 -6
  63. package/components/FAB/index.tsx +1 -5
  64. package/components/FAB/utils.ts +2 -6
  65. package/components/FilePicker/FilePicker.tsx +47 -76
  66. package/components/FilePicker/index.tsx +1 -5
  67. package/components/FilePicker/utils.ts +5 -6
  68. package/components/HelperText/HelperText.tsx +0 -35
  69. package/components/HelperText/index.tsx +1 -5
  70. package/components/HelperText/utils.ts +5 -7
  71. package/components/HorizontalDivider/HorizontalDivider.tsx +5 -3
  72. package/components/HorizontalDivider/index.tsx +1 -5
  73. package/components/Icon/CrossFadeIcon.tsx +3 -5
  74. package/components/Icon/Icon.tsx +2 -4
  75. package/components/Icon/iconFactory.tsx +3 -3
  76. package/components/Icon/index.tsx +2 -6
  77. package/components/Icon/types.ts +17 -6
  78. package/components/IconButton/IconButton.tsx +45 -58
  79. package/components/IconButton/index.tsx +1 -5
  80. package/components/IconButton/utils.ts +153 -49
  81. package/components/If/index.tsx +1 -5
  82. package/components/InputAddon/index.tsx +1 -5
  83. package/components/InputAddon/utils.ts +5 -6
  84. package/components/Link/index.tsx +1 -5
  85. package/components/Link/utils.ts +2 -6
  86. package/components/ListItem/index.tsx +1 -5
  87. package/components/ListItem/utils.ts +13 -11
  88. package/components/LoadingIndicator/LoadingIndicator.tsx +253 -0
  89. package/components/LoadingIndicator/LoadingIndicator.web.tsx +136 -0
  90. package/components/LoadingIndicator/index.tsx +13 -0
  91. package/components/LoadingIndicator/utils.ts +117 -0
  92. package/components/Menu/index.tsx +1 -5
  93. package/components/Menu/utils.ts +6 -8
  94. package/components/Modal/index.tsx +1 -5
  95. package/components/Modal/utils.ts +2 -6
  96. package/components/NavigationRail/NavigationRailHeader.tsx +1 -1
  97. package/components/NavigationRail/index.tsx +1 -5
  98. package/components/NavigationRail/utils.ts +21 -17
  99. package/components/NavigationStack/index.tsx +1 -5
  100. package/components/NavigationStack/utils.tsx +7 -1
  101. package/components/Portal/index.tsx +1 -5
  102. package/components/RadioButton/index.ts +1 -5
  103. package/components/RadioButton/utils.ts +9 -8
  104. package/components/Rating/index.tsx +1 -5
  105. package/components/Rating/utils.ts +6 -8
  106. package/components/Select/Select.tsx +369 -507
  107. package/components/Select/index.ts +7 -14
  108. package/components/Select/types.ts +2 -4
  109. package/components/Select/utils.ts +215 -0
  110. package/components/Slot/Slot.tsx +244 -0
  111. package/components/Slot/compose-refs.tsx +60 -0
  112. package/components/Slot/index.tsx +8 -0
  113. package/components/StateLayer/index.tsx +1 -5
  114. package/components/StateLayer/utils.ts +5 -6
  115. package/components/Surface/Surface.android.tsx +34 -8
  116. package/components/Surface/Surface.ios.tsx +36 -29
  117. package/components/Surface/Surface.tsx +31 -4
  118. package/components/Surface/index.tsx +1 -5
  119. package/components/Surface/utils.ts +49 -36
  120. package/components/Switch/Switch.tsx +8 -2
  121. package/components/Switch/index.tsx +1 -5
  122. package/components/Switch/utils.ts +2 -6
  123. package/components/Tabs/TabItem.tsx +35 -58
  124. package/components/Tabs/TabLabel.tsx +5 -9
  125. package/components/Tabs/Tabs.tsx +154 -149
  126. package/components/Tabs/index.tsx +1 -5
  127. package/components/Tabs/utils.ts +25 -12
  128. package/components/Text/Text.tsx +2 -8
  129. package/components/TextInput/TextInput.tsx +655 -571
  130. package/components/TextInput/index.tsx +19 -7
  131. package/components/TextInput/types.ts +76 -27
  132. package/components/TextInput/utils.ts +232 -159
  133. package/components/TextInputWithMask/index.tsx +1 -5
  134. package/components/TimePicker/AmPmSwitcher.tsx +1 -1
  135. package/components/TimePicker/index.tsx +1 -5
  136. package/components/TimePicker/utils.ts +29 -21
  137. package/components/TimePickerField/TimePickerField.tsx +7 -5
  138. package/components/TimePickerField/index.tsx +1 -5
  139. package/components/TimePickerField/utils.ts +5 -6
  140. package/components/TimePickerModal/TimePickerModal.tsx +6 -2
  141. package/components/TimePickerModal/index.tsx +1 -5
  142. package/components/TimePickerModal/utils.ts +5 -6
  143. package/components/Tooltip/TooltipTrigger.tsx +25 -16
  144. package/components/Tooltip/index.tsx +1 -5
  145. package/components/Tooltip/utils.ts +5 -6
  146. package/components/TouchableRipple/TouchableRipple.native.tsx +50 -14
  147. package/components/TouchableRipple/TouchableRipple.tsx +137 -47
  148. package/components/TouchableRipple/index.tsx +1 -5
  149. package/components/TouchableRipple/utils.ts +5 -6
  150. package/components/VerticalDivider/VerticalDivider.tsx +9 -8
  151. package/components/VerticalDivider/index.tsx +1 -5
  152. package/core/componentsRegistry.ts +31 -19
  153. package/hocs/withPortal.tsx +1 -1
  154. package/hooks/index.tsx +0 -5
  155. package/hooks/useControlledValue.tsx +20 -4
  156. package/hooks/useSubcomponents.tsx +63 -31
  157. package/hooks/useWhatHasUpdated.tsx +48 -0
  158. package/package.json +12 -15
  159. package/shortcuts-manager/ShortcutsManager/ShortcutsManager.tsx +5 -2
  160. package/styles/shadow.ts +2 -1
  161. package/styles/themes/LightTheme.tsx +1 -1
  162. package/utils/extractPropertiesFromStyles.ts +25 -0
  163. package/utils/lodash.ts +77 -6
  164. package/utils/repository.ts +2 -52
  165. package/hooks/useSearchable.tsx +0 -74
@@ -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.1",
3
+ "version": "0.5.0-beta.11",
4
4
  "author": "Thet Aung <thetaung.dev@gmail.com>",
5
5
  "license": "MIT",
6
6
  "main": "index.ts",
@@ -42,41 +42,38 @@
42
42
  },
43
43
  "dependencies": {
44
44
  "@gorhom/portal": "^1.0.14",
45
+ "@radix-ui/react-compose-refs": "^1.1.2",
45
46
  "@react-native-vector-icons/feather": "^12.4.0",
46
47
  "@react-native-vector-icons/material-design-icons": "^12.4.0",
47
48
  "color": "^4.2.3",
48
49
  "date-fns": "^4.1.0",
49
50
  "eventemitter2": "^6.4.9",
50
- "lodash.debounce": "^4.0.8",
51
- "lodash.get": "^4.4.2",
52
- "lodash.keyby": "^4.6.0",
53
- "lodash.memoize": "^4.1.2",
54
- "lodash.omitby": "^4.6.0",
51
+ "flubber": "^0.4.2",
55
52
  "use-sync-external-store": "^1.2.0"
56
53
  },
57
54
  "peerDependencies": {
58
55
  "@react-native-documents/picker": "^10.1.2",
59
56
  "react": "19.1.0",
60
57
  "react-native": "0.81.4",
61
- "react-native-unistyles": "^3.0.15",
62
- "react-native-web": "~0.21.1"
58
+ "react-native-svg": ">=12.1.0",
59
+ "react-native-unistyles": "^3.0.22",
60
+ "react-native-web": "~0.21.1",
61
+ "react-native-reanimated": ">=4.0.0",
62
+ "react-native-redash": ">=18.0.0"
63
63
  },
64
64
  "devDependencies": {
65
+ "@types/flubber": "^0.4.0",
66
+ "react-native-svg": "^15.10.1",
65
67
  "@react-native-documents/picker": "^10.1.2",
66
68
  "@types/color": "^3.0.3",
67
69
  "@types/jest": "^29.1.2",
68
- "@types/lodash": "^4.17.13",
69
- "@types/lodash.debounce": "^4.0.7",
70
- "@types/lodash.get": "^4.4.7",
71
- "@types/lodash.keyby": "^4.6.7",
72
- "@types/lodash.memoize": "^4.1.7",
73
- "@types/lodash.omitby": "^4.6.7",
74
70
  "@types/react": "~19.1.10",
75
71
  "@types/use-sync-external-store": "^1.5.0",
76
72
  "react": "19.1.0",
77
73
  "react-native": "0.81.4",
78
74
  "react-native-builder-bob": "^0.17.1",
79
- "react-native-unistyles": "^3.0.15",
75
+ "react-native-reanimated": "~4.1.1",
76
+ "react-native-unistyles": "^3.0.22",
80
77
  "react-native-web": "~0.21.1"
81
78
  },
82
79
  "eslintIgnore": [
@@ -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,
@@ -25,7 +25,7 @@ export const generateLightThemeColors = (
25
25
  error: palette.error40,
26
26
  errorContainer: palette.error90,
27
27
  onPrimary: palette.primary100,
28
- onPrimaryContainer: palette.primary10,
28
+ onPrimaryContainer: palette.primary30,
29
29
  onSecondary: palette.secondary100,
30
30
  onSecondaryContainer: palette.secondary10,
31
31
  onTertiary: palette.tertiary100,
@@ -0,0 +1,25 @@
1
+ import { StyleSheet } from 'react-native-unistyles';
2
+
3
+ // TODO - abstract this
4
+ export function extractPropertiesFromStyles(
5
+ _objectsArray: Record<string, any>,
6
+ propertiesToExtract: string[],
7
+ ) {
8
+ const extracted: Record<string, any> = {};
9
+
10
+ const objectsArray = StyleSheet.flatten(_objectsArray);
11
+
12
+ for (let i = objectsArray.length - 1; i >= 0; i--) {
13
+ const obj = objectsArray[i];
14
+
15
+ for (const prop of propertiesToExtract) {
16
+ if (!obj) continue;
17
+ if (prop in obj) {
18
+ // @ts-ignore
19
+ extracted[prop] = obj[prop];
20
+ }
21
+ }
22
+ }
23
+
24
+ return extracted;
25
+ }
package/utils/lodash.ts CHANGED
@@ -1,9 +1,80 @@
1
- import memoize from 'lodash.memoize';
2
- export { default as debounce } from 'lodash.debounce';
3
- export { default as get } from 'lodash.get';
4
- export { default as keyBy } from 'lodash.keyby';
5
- export { default as memoize } from 'lodash.memoize';
6
- 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
+ };
7
78
 
8
79
  export const isNil = (value: unknown): value is null | undefined => value == null;
9
80
  export const noop = () => {};
@@ -1,19 +1,11 @@
1
- import EventEmitter, {
2
- type ConstructorOptions,
3
- type event as Event,
4
- type eventNS,
5
- } from 'eventemitter2';
6
-
7
- import { debounce } from './lodash';
8
-
9
- interface RepositoryConstructor<T> extends ConstructorOptions {
1
+ interface RepositoryConstructor<T> {
10
2
  onRegister?: (arg: T, name: string, registery: Record<string, T>) => T;
11
3
  name?: string;
12
4
  }
13
5
 
14
6
  let id = Date.now();
15
7
 
16
- export class Repository<T> extends EventEmitter {
8
+ export class Repository<T> {
17
9
  private registry: Record<string, T> = {};
18
10
  readonly #name!: string;
19
11
 
@@ -30,9 +22,7 @@ export class Repository<T> extends EventEmitter {
30
22
  constructor({
31
23
  onRegister = arg => arg,
32
24
  name = Repository.uniqueId,
33
- ...options
34
25
  }: RepositoryConstructor<T> = {}) {
35
- super(options);
36
26
  this.#onRegister = onRegister;
37
27
  this.#name = name;
38
28
  }
@@ -41,11 +31,6 @@ export class Repository<T> extends EventEmitter {
41
31
  return !!this.registry[itemName];
42
32
  };
43
33
 
44
- emit(event: eventNS | Event, ...values: any[]) {
45
- event = typeof event === 'string' ? `${this.#name}::event` : event;
46
- return super.emit(event, ...values);
47
- }
48
-
49
34
  /**
50
35
  * Register a item with the src.
51
36
  */
@@ -57,8 +42,6 @@ export class Repository<T> extends EventEmitter {
57
42
  ...this.registry,
58
43
  [itemName]: updatedItem,
59
44
  };
60
-
61
- this.emit('item_registered', itemName);
62
45
  };
63
46
 
64
47
  /**
@@ -67,37 +50,4 @@ export class Repository<T> extends EventEmitter {
67
50
  getAll = () => {
68
51
  return this.registry;
69
52
  };
70
-
71
- /**
72
- * Register a listener to the module registry.
73
- * Return value returns a function that removes the listener.
74
- */
75
- listen = (
76
- callback: (events: string[]) => void,
77
- { throttle = 100 }: { throttle?: number } = {},
78
- ) => {
79
- let cached: string[] = [];
80
-
81
- const wrappedCallback = () => {
82
- callback(cached);
83
- cached = [];
84
- };
85
-
86
- const debouncedCallback = !throttle
87
- ? wrappedCallback
88
- : debounce(wrappedCallback, Math.abs(throttle), {
89
- trailing: true,
90
- leading: false,
91
- });
92
-
93
- const handledCallback = (itemName: string) => {
94
- cached.push(itemName);
95
- debouncedCallback();
96
- };
97
-
98
- this.on('item_registered', handledCallback);
99
- return () => {
100
- this.off('item_registered', handledCallback);
101
- };
102
- };
103
53
  }
@@ -1,74 +0,0 @@
1
- import { type ReactElement, useCallback, useMemo } from 'react';
2
-
3
- import { IconButton } from '../components/IconButton';
4
- import {
5
- TextInput,
6
- type TextInputElementProps,
7
- type TextInputProps,
8
- } from '../components/TextInput';
9
- import useControlledValue from './useControlledValue';
10
-
11
- export interface UseSearchableProps {
12
- searchable?: boolean;
13
- onQueryChange?: (search: string) => void;
14
- query?: string;
15
- searchInputProps?: Omit<TextInputProps, 'value' | 'onChangeText'>;
16
- }
17
-
18
- const useSearchInputProps = (props?: Partial<TextInputProps>) => {
19
- const onChange = props?.onChangeText;
20
- const onPressClear = useCallback(() => {
21
- onChange?.('');
22
- }, [onChange]);
23
-
24
- const right = useCallback(
25
- ({ color, forceFocus }: TextInputElementProps) => (
26
- <IconButton
27
- name={props?.value ? 'close-circle-outline' : 'magnify'}
28
- onPress={props?.value ? onPressClear : forceFocus}
29
- color={color}
30
- />
31
- ),
32
- [onPressClear, props?.value],
33
- );
34
-
35
- return useMemo(
36
- () => (props?.right === undefined ? props : { right, ...props }),
37
- [props, right],
38
- );
39
- };
40
-
41
- const useSearchable = ({
42
- searchable,
43
- onQueryChange,
44
- query,
45
- searchInputProps,
46
- }: UseSearchableProps = {}): ReactElement<TextInputProps> | null => {
47
- const [value, onValueChange] = useControlledValue({
48
- value: query || '',
49
- defaultValue: '', // to disable the useControlledValue inside TextInput
50
- onChange: onQueryChange,
51
- disabled: !searchable,
52
- });
53
-
54
- const props = useSearchInputProps(
55
- useMemo(
56
- () => ({
57
- ...searchInputProps,
58
- value,
59
- onChangeText: onValueChange,
60
- }),
61
- [onValueChange, searchInputProps, value],
62
- ),
63
- );
64
-
65
- return useMemo(() => {
66
- if (!searchable || !onQueryChange) return null;
67
-
68
- return <TextInput {...props} />;
69
- }, [onQueryChange, props, searchable]);
70
- };
71
-
72
- export { useSearchInputProps };
73
-
74
- export default useSearchable;