react-native-molecules 0.5.0-beta.2 → 0.5.0-beta.21

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 (160) hide show
  1. package/README.md +1 -1
  2. package/components/Accordion/Accordion.tsx +2 -6
  3. package/components/Accordion/AccordionItem.tsx +16 -12
  4. package/components/Accordion/AccordionItemContent.tsx +6 -1
  5. package/components/Accordion/AccordionItemHeader.tsx +1 -1
  6. package/components/Accordion/utils.ts +6 -0
  7. package/components/ActivityIndicator/ActivityIndicator.tsx +6 -15
  8. package/components/Appbar/AppbarBase.tsx +18 -13
  9. package/components/Button/Button.tsx +209 -264
  10. package/components/Button/index.tsx +9 -3
  11. package/components/Button/types.ts +16 -2
  12. package/components/Button/utils.ts +230 -208
  13. package/components/Checkbox/CheckboxBase.ios.tsx +10 -14
  14. package/components/Checkbox/CheckboxBase.tsx +14 -121
  15. package/components/Checkbox/utils.ts +0 -25
  16. package/components/Chip/Chip.tsx +40 -52
  17. package/components/Chip/utils.ts +3 -7
  18. package/components/DateField/DateField.tsx +111 -0
  19. package/components/DateField/index.tsx +6 -0
  20. package/components/{DatePickerInput/inputUtils.ts → DateField/useDateFieldState.ts} +17 -49
  21. package/components/DatePicker/DateCalendar.tsx +83 -0
  22. package/components/DatePicker/DatePickerActions.tsx +73 -0
  23. package/components/DatePicker/DatePickerModal.tsx +245 -0
  24. package/components/DatePicker/DatePickerPopover.tsx +79 -0
  25. package/components/DatePicker/DatePickerProvider.tsx +158 -0
  26. package/components/DatePicker/DatePickerTrigger.tsx +23 -0
  27. package/components/DatePicker/context.tsx +83 -0
  28. package/components/DatePicker/index.tsx +45 -0
  29. package/components/DatePicker/utils.ts +293 -0
  30. package/components/DatePickerInline/DatePickerContext.tsx +1 -0
  31. package/components/DatePickerInline/DatePickerDockedHeader.tsx +117 -0
  32. package/components/DatePickerInline/DatePickerInline.tsx +16 -15
  33. package/components/DatePickerInline/DatePickerInlineBase.tsx +8 -2
  34. package/components/DatePickerInline/DatePickerInlineHeader.tsx +8 -4
  35. package/components/DatePickerInline/Day.tsx +25 -1
  36. package/components/DatePickerInline/DayNames.tsx +13 -10
  37. package/components/DatePickerInline/DayRange.tsx +2 -4
  38. package/components/DatePickerInline/HeaderItem.tsx +42 -27
  39. package/components/DatePickerInline/Month.tsx +48 -67
  40. package/components/DatePickerInline/MonthPicker.tsx +38 -44
  41. package/components/DatePickerInline/Swiper.native.tsx +21 -4
  42. package/components/DatePickerInline/Swiper.tsx +168 -13
  43. package/components/DatePickerInline/Week.tsx +6 -1
  44. package/components/DatePickerInline/YearPicker.tsx +206 -53
  45. package/components/DatePickerInline/dateUtils.tsx +17 -12
  46. package/components/DatePickerInline/types.ts +6 -2
  47. package/components/DatePickerInline/utils.ts +66 -29
  48. package/components/Drawer/Drawer.tsx +17 -6
  49. package/components/ElementGroup/ElementGroup.tsx +16 -14
  50. package/components/FilePicker/FilePicker.tsx +48 -78
  51. package/components/FilePicker/index.tsx +2 -1
  52. package/components/FilePicker/utils.ts +9 -0
  53. package/components/HelperText/HelperText.tsx +0 -35
  54. package/components/Icon/iconFactory.tsx +3 -3
  55. package/components/Icon/index.tsx +1 -1
  56. package/components/Icon/types.ts +17 -6
  57. package/components/IconButton/IconButton.tsx +42 -57
  58. package/components/IconButton/utils.ts +142 -33
  59. package/components/ListItem/ListItem.tsx +3 -1
  60. package/components/ListItem/utils.ts +1 -1
  61. package/components/LoadingIndicator/LoadingIndicator.tsx +253 -0
  62. package/components/LoadingIndicator/LoadingIndicator.web.tsx +136 -0
  63. package/components/LoadingIndicator/index.tsx +13 -0
  64. package/components/LoadingIndicator/utils.ts +117 -0
  65. package/components/Menu/Menu.tsx +3 -18
  66. package/components/NavigationRail/NavigationRail.tsx +15 -9
  67. package/components/Popover/Popover.tsx +122 -145
  68. package/components/Popover/PopoverRoot.tsx +74 -0
  69. package/components/Popover/common.ts +50 -34
  70. package/components/Popover/index.ts +18 -1
  71. package/components/Popover/usePlatformMeasure.native.ts +90 -0
  72. package/components/Popover/usePlatformMeasure.ts +118 -0
  73. package/components/Popover/utils.ts +34 -0
  74. package/components/Select/Select.tsx +368 -507
  75. package/components/Select/context.tsx +72 -0
  76. package/components/Select/index.ts +8 -14
  77. package/components/Select/types.ts +2 -4
  78. package/components/Select/utils.ts +144 -0
  79. package/components/Slot/Slot.tsx +244 -0
  80. package/components/Slot/compose-refs.tsx +62 -0
  81. package/components/Slot/index.tsx +8 -0
  82. package/components/Surface/Surface.android.tsx +34 -8
  83. package/components/Surface/Surface.ios.tsx +36 -29
  84. package/components/Surface/Surface.tsx +31 -4
  85. package/components/Surface/utils.ts +44 -30
  86. package/components/Switch/Switch.tsx +8 -2
  87. package/components/Tabs/TabItem.tsx +35 -58
  88. package/components/Tabs/TabLabel.tsx +5 -9
  89. package/components/Tabs/Tabs.tsx +154 -148
  90. package/components/Tabs/utils.ts +15 -2
  91. package/components/TextInput/TextInput.tsx +658 -575
  92. package/components/TextInput/index.tsx +19 -3
  93. package/components/TextInput/types.ts +76 -27
  94. package/components/TextInput/utils.ts +225 -145
  95. package/components/TimeField/TimeField.tsx +75 -0
  96. package/components/TimeField/index.tsx +6 -0
  97. package/components/TimeField/useTimeFieldState.ts +70 -0
  98. package/components/{TimePickerField/sanitizeTime.ts → TimeField/utils.ts} +77 -10
  99. package/components/TimePicker/TimeInput.tsx +87 -37
  100. package/components/TimePicker/TimeInputs.tsx +137 -49
  101. package/components/TimePicker/TimePicker.tsx +73 -10
  102. package/components/TimePicker/TimePickerModal.tsx +186 -0
  103. package/components/TimePicker/context.tsx +17 -0
  104. package/components/TimePicker/index.tsx +15 -3
  105. package/components/TimePicker/utils.ts +93 -0
  106. package/components/Tooltip/Tooltip.tsx +42 -67
  107. package/components/Tooltip/TooltipContent.tsx +32 -5
  108. package/components/Tooltip/TooltipTrigger.tsx +20 -20
  109. package/components/Tooltip/index.tsx +1 -1
  110. package/components/TouchableRipple/TouchableRipple.native.tsx +50 -14
  111. package/components/TouchableRipple/TouchableRipple.tsx +137 -47
  112. package/hocs/withPortal.tsx +1 -1
  113. package/hooks/index.tsx +0 -6
  114. package/hooks/useActionState.tsx +19 -8
  115. package/hooks/useControlledValue.tsx +20 -4
  116. package/hooks/useFilePicker.tsx +6 -16
  117. package/hooks/useWhatHasUpdated.tsx +48 -0
  118. package/package.json +17 -13
  119. package/shortcuts-manager/ShortcutsManager/ShortcutsManager.tsx +5 -2
  120. package/styles/shadow.ts +2 -1
  121. package/styles/themes/LightTheme.tsx +1 -1
  122. package/utils/DocumentPicker/documentPicker.ts +78 -27
  123. package/utils/DocumentPicker/types.ts +0 -1
  124. package/utils/extractPropertiesFromStyles.ts +25 -0
  125. package/utils/extractSubcomponents.ts +89 -0
  126. package/utils/lodash.ts +77 -5
  127. package/components/DatePickerDocked/DatePickerDocked.tsx +0 -30
  128. package/components/DatePickerDocked/DatePickerDockedHeader.tsx +0 -129
  129. package/components/DatePickerDocked/index.tsx +0 -17
  130. package/components/DatePickerDocked/types.ts +0 -11
  131. package/components/DatePickerDocked/utils.ts +0 -157
  132. package/components/DatePickerInput/DatePickerInput.tsx +0 -139
  133. package/components/DatePickerInput/DatePickerInputModal.tsx +0 -48
  134. package/components/DatePickerInput/DatePickerInputWithoutModal.tsx +0 -77
  135. package/components/DatePickerInput/DateRangeInput.tsx +0 -88
  136. package/components/DatePickerInput/index.tsx +0 -10
  137. package/components/DatePickerInput/types.ts +0 -28
  138. package/components/DatePickerInput/utils.ts +0 -15
  139. package/components/DatePickerModal/AnimatedCrossView.tsx +0 -94
  140. package/components/DatePickerModal/CalendarEdit.tsx +0 -139
  141. package/components/DatePickerModal/DatePickerModal.tsx +0 -85
  142. package/components/DatePickerModal/DatePickerModalContent.tsx +0 -155
  143. package/components/DatePickerModal/DatePickerModalContentHeader.tsx +0 -213
  144. package/components/DatePickerModal/DatePickerModalHeader.tsx +0 -74
  145. package/components/DatePickerModal/DatePickerModalHeaderBackground.tsx +0 -13
  146. package/components/DatePickerModal/index.tsx +0 -16
  147. package/components/DatePickerModal/types.ts +0 -92
  148. package/components/DatePickerModal/utils.ts +0 -122
  149. package/components/DateTimePicker/DateTimePicker.tsx +0 -172
  150. package/components/DateTimePicker/index.tsx +0 -10
  151. package/components/DateTimePicker/utils.ts +0 -12
  152. package/components/Popover/Popover.native.tsx +0 -185
  153. package/components/TimePickerField/TimePickerField.tsx +0 -152
  154. package/components/TimePickerField/index.tsx +0 -10
  155. package/components/TimePickerField/utils.ts +0 -94
  156. package/components/TimePickerModal/TimePickerModal.tsx +0 -115
  157. package/components/TimePickerModal/index.tsx +0 -10
  158. package/components/TimePickerModal/utils.ts +0 -47
  159. package/hooks/useSearchable.tsx +0 -74
  160. package/hooks/useSubcomponents.tsx +0 -59
@@ -2,6 +2,14 @@ import { Platform } from 'react-native';
2
2
 
3
3
  import type { DocumentPickerOptions, DocumentResult } from './types';
4
4
 
5
+ class OperationCanceledError extends Error {
6
+ code = 'OPERATION_CANCELED';
7
+ constructor() {
8
+ super('user canceled the document picker');
9
+ this.name = 'OperationCanceledError';
10
+ }
11
+ }
12
+
5
13
  const resolveFileData = (file: File): Promise<DocumentResult> => {
6
14
  return new Promise((resolve, reject) => {
7
15
  const mimeType = file.type;
@@ -31,46 +39,89 @@ const resolveFileData = (file: File): Promise<DocumentResult> => {
31
39
  const getDocumentAsyncWeb = async ({
32
40
  type = '*/*',
33
41
  multiple = false,
34
- }: DocumentPickerOptions): Promise<DocumentResult | DocumentResult[]> => {
42
+ onCancel,
43
+ onError,
44
+ }: DocumentPickerOptions): Promise<DocumentResult[]> => {
35
45
  // SSR guard
36
46
  if (Platform.OS !== 'web') {
37
- return { type: 'cancel' };
47
+ const error = new OperationCanceledError();
48
+ onCancel?.();
49
+ throw error;
38
50
  }
39
51
 
40
- const input = document.createElement('input');
41
- input.style.display = 'none';
42
- input.setAttribute('type', 'file');
43
- // @ts-expect-error
44
- input.setAttribute('accept', Array.isArray(type) ? type.join(',') : type);
52
+ return new Promise((resolve, reject) => {
53
+ try {
54
+ const input = document.createElement('input');
55
+ input.style.display = 'none';
56
+ input.setAttribute('type', 'file');
57
+ // @ts-expect-error
58
+ input.setAttribute('accept', Array.isArray(type) ? type.join(',') : type);
45
59
 
46
- if (multiple) {
47
- input.setAttribute('multiple', 'multiple');
48
- }
60
+ if (multiple) {
61
+ input.setAttribute('multiple', 'multiple');
62
+ }
49
63
 
50
- document.body.appendChild(input);
64
+ document.body.appendChild(input);
51
65
 
52
- return new Promise(resolve => {
53
- input.addEventListener('change', () => {
54
- if (input.files) {
55
- const response: Promise<DocumentResult>[] = [];
66
+ const cleanup = () => {
67
+ try {
68
+ document.body.removeChild(input);
69
+ } catch (e) {
70
+ // Input already removed, ignore
71
+ }
72
+ };
56
73
 
57
- Array.from(input.files).forEach(file => response.push(resolveFileData(file)));
74
+ input.addEventListener('change', async () => {
75
+ try {
76
+ if (input.files && input.files.length > 0) {
77
+ const response: Promise<DocumentResult>[] = [];
58
78
 
59
- return resolve(Promise.all(response));
60
- } else {
61
- resolve({ type: 'cancel' });
62
- }
79
+ Array.from(input.files).forEach(file =>
80
+ response.push(resolveFileData(file)),
81
+ );
63
82
 
64
- document.body.removeChild(input);
65
- });
83
+ const results = await Promise.all(response);
84
+ resolve(results);
85
+ }
86
+ } catch (error) {
87
+ onError?.(error);
88
+ reject(error);
89
+ } finally {
90
+ cleanup();
91
+ }
92
+ });
66
93
 
67
- const event = new MouseEvent('click');
68
- input.dispatchEvent(event);
94
+ input.addEventListener('cancel', () => {
95
+ const error = new OperationCanceledError();
96
+ onCancel?.();
97
+ cleanup();
98
+ reject(error);
99
+ });
100
+
101
+ input.addEventListener('error', () => {
102
+ const error = new Error('File picker error occurred');
103
+ onError?.(error);
104
+ cleanup();
105
+ reject(error);
106
+ });
107
+
108
+ const event = new MouseEvent('click');
109
+ input.dispatchEvent(event);
110
+ } catch (error) {
111
+ // Handle errors from file picker setup or opening
112
+ onError?.(error);
113
+ reject(error);
114
+ }
69
115
  });
70
116
  };
71
117
 
118
+ const pickSingle = (options: DocumentPickerOptions = {}) =>
119
+ getDocumentAsyncWeb({ ...options, multiple: false });
120
+
121
+ const pickMultiple = (options: DocumentPickerOptions = {}) =>
122
+ getDocumentAsyncWeb({ ...options, multiple: true });
123
+
72
124
  export default {
73
- pickSingle: ({ type }: DocumentPickerOptions) => getDocumentAsyncWeb({ type, multiple: false }),
74
- pickMultiple: ({ type }: DocumentPickerOptions) =>
75
- getDocumentAsyncWeb({ type, multiple: true }),
125
+ pickSingle,
126
+ pickMultiple,
76
127
  };
@@ -12,7 +12,6 @@ export type DocumentPickerOptions = Omit<RNDocumentPickerOptions, 'allowMultiSel
12
12
  multiple?: boolean;
13
13
  /**
14
14
  * runs when the DocumentPicker is cancelled
15
- * currently, only supported on IOS and Android
16
15
  */
17
16
  onCancel?: () => void;
18
17
  /**
@@ -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
+ }
@@ -0,0 +1,89 @@
1
+ import type { ReactElement, ReactNode } from 'react';
2
+ import { Children, type FC, isValidElement } from 'react';
3
+
4
+ export type ExtractSubcomponentsArgs<T extends string> = {
5
+ children: ReactNode;
6
+ /**
7
+ * array of displayName as string
8
+ * */
9
+ allowedChildren: (T | { name: T; allowMultiple?: boolean })[];
10
+ /**
11
+ * If true, also returns the remaining children that don't match any of the allowedChildren
12
+ * in a `rest` property
13
+ */
14
+ includeRest?: boolean;
15
+ };
16
+
17
+ export type ExtractSubcomponentsResult<T extends string, IncludeRest extends boolean = false> = {
18
+ [key in T]: ReactElement[];
19
+ } & (IncludeRest extends true ? { rest: ReactNode[] } : {});
20
+
21
+ /**
22
+ * This will return an object with the displayNames as the property names
23
+ * eg. allowedChildren: ['Drawer_Header', 'Drawer_Content', 'Drawer_Footer', 'DrawerItem'];
24
+ *
25
+ * return value -> {
26
+ * Drawer_Header: [],
27
+ * Drawer_Content: [],
28
+ * Drawer_Footer: [],
29
+ * DrawerItem: [],
30
+ * }
31
+ *
32
+ * If includeRest is true, also returns:
33
+ * {
34
+ * ...above,
35
+ * rest: [remaining children that don't match allowedChildren]
36
+ * }
37
+ * */
38
+ export function extractSubcomponents<
39
+ T extends string = string,
40
+ IncludeRest extends boolean = false,
41
+ >({
42
+ children,
43
+ allowedChildren,
44
+ includeRest,
45
+ }: ExtractSubcomponentsArgs<T> & { includeRest?: IncludeRest }): ExtractSubcomponentsResult<
46
+ T,
47
+ IncludeRest
48
+ > {
49
+ // Single map: name → allowMultiple
50
+ const allowedMap = new Map<T, boolean>();
51
+ for (const entry of allowedChildren) {
52
+ if (typeof entry === 'string') {
53
+ allowedMap.set(entry as T, true);
54
+ } else {
55
+ allowedMap.set(entry.name, entry.allowMultiple ?? true);
56
+ }
57
+ }
58
+
59
+ const result = (includeRest ? { rest: [] } : {}) as ExtractSubcomponentsResult<T, IncludeRest>;
60
+ for (const name of allowedMap.keys()) {
61
+ (result as any)[name] = [];
62
+ }
63
+
64
+ Children.forEach(children, child => {
65
+ if (!isValidElement(child)) {
66
+ if (includeRest) (result as any).rest.push(child);
67
+ return;
68
+ }
69
+
70
+ const displayName = (child.type as FC)?.displayName as T | undefined;
71
+ if (!displayName) {
72
+ if (includeRest) (result as any).rest.push(child);
73
+ return;
74
+ }
75
+
76
+ const allowMultiple = allowedMap.get(displayName);
77
+ if (allowMultiple !== undefined) {
78
+ if (allowMultiple) {
79
+ (result as any)[displayName].push(child);
80
+ } else {
81
+ (result as any)[displayName] = [child];
82
+ }
83
+ } else if (includeRest) {
84
+ (result as any).rest.push(child);
85
+ }
86
+ });
87
+
88
+ return result;
89
+ }
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 = () => {};
@@ -1,30 +0,0 @@
1
- import { memo } from 'react';
2
-
3
- import DatePickerInlineBase from '../DatePickerInline/DatePickerInlineBase';
4
- import { Popover } from '../Popover';
5
- import DatePickerDockedHeader from './DatePickerDockedHeader';
6
- import type { DatePickerDockedProps } from './types';
7
- import { datePickerDockedMonthStyles } from './utils';
8
-
9
- const DatePickerDocked = (props: DatePickerDockedProps) => {
10
- const { triggerRef, isOpen, onToggle, onClose } = props;
11
-
12
- return (
13
- <Popover
14
- triggerRef={triggerRef}
15
- isOpen={isOpen}
16
- onClose={onClose}
17
- {...props.popoverContentProps}>
18
- <DatePickerInlineBase
19
- {...props}
20
- // TODO - fix ts issues
21
- // @ts-ignore
22
- HeaderComponent={DatePickerDockedHeader}
23
- onToggle={onToggle}
24
- monthStyle={datePickerDockedMonthStyles}
25
- />
26
- </Popover>
27
- );
28
- };
29
-
30
- export default memo(DatePickerDocked);
@@ -1,129 +0,0 @@
1
- import { add, format } from 'date-fns';
2
- import { memo, useCallback, useMemo } from 'react';
3
- import { View, type ViewStyle } from 'react-native';
4
-
5
- import { useDatePickerStore } from '../DatePickerInline/DatePickerContext';
6
- import type { DisableWeekDaysType } from '../DatePickerInline/dateUtils';
7
- import DayNames from '../DatePickerInline/DayNames';
8
- import HeaderItem from '../DatePickerInline/HeaderItem';
9
- import { dayNamesHeight } from '../DatePickerInline/utils';
10
- import { datePickerDockedHeaderStyles } from './utils';
11
-
12
- const buttonContainerHeight = 56;
13
- const buttonContainerMarginTop = 4;
14
- const buttonContainerMarginBottom = 8;
15
-
16
- export type CalendarHeaderProps = {
17
- locale?: string;
18
- disableWeekDays?: DisableWeekDaysType;
19
- style?: ViewStyle;
20
- };
21
-
22
- function DatePickerDockedHeader({
23
- locale = 'en',
24
- disableWeekDays,
25
- style: styleProp,
26
- }: CalendarHeaderProps) {
27
- const [{ localDate, pickerType, startDateYear, endDateYear }, setStore] = useDatePickerStore(
28
- state => ({
29
- localDate: state.localDate,
30
- pickerType: state.pickerType,
31
- startDateYear: state.startDateYear,
32
- endDateYear: state.endDateYear,
33
- }),
34
- );
35
-
36
- // const componentStyles = useComponentStyles('DatePickerDocked_Header', styleProp);
37
-
38
- const monthName = useMemo(() => {
39
- return format(localDate, 'MMM');
40
- }, [localDate]);
41
-
42
- const { containerStyle, daysWrapperStyle } = useMemo(() => {
43
- return {
44
- containerStyle: [datePickerDockedHeaderStyles.datePickerHeader, styleProp],
45
- daysWrapperStyle: datePickerDockedHeaderStyles.daysWrapperStyle,
46
- };
47
- }, [styleProp]);
48
-
49
- const handlePressDropDown = useCallback(
50
- (type: 'month' | 'year') => {
51
- setStore(prev => ({
52
- pickerType: prev.pickerType ? undefined : type,
53
- }));
54
- },
55
- [setStore],
56
- );
57
-
58
- const isNotInRange = useCallback(
59
- (date: Date) => {
60
- const year = date.getFullYear();
61
- const month = date.getMonth();
62
- if (year < startDateYear || year > endDateYear || month > 11 || month < 0) {
63
- return true;
64
- }
65
- return false;
66
- },
67
- [startDateYear, endDateYear],
68
- );
69
-
70
- const handleChange = useCallback(
71
- (offset: number, type?: 'month' | 'year') => {
72
- let newDate = localDate;
73
- const prop = type === 'month' ? 'months' : 'years';
74
- newDate = add(newDate, { [prop]: offset });
75
-
76
- if (isNotInRange(newDate)) return;
77
-
78
- setStore(() => ({
79
- localDate: newDate,
80
- }));
81
- },
82
- [isNotInRange, localDate, setStore],
83
- );
84
-
85
- const handleChangePrevious = useMemo(() => handleChange.bind(null, -1), [handleChange]);
86
- const handleChangeNext = useMemo(() => handleChange.bind(null, 1), [handleChange]);
87
-
88
- return (
89
- <View>
90
- <View style={containerStyle} pointerEvents={'box-none'}>
91
- <HeaderItem
92
- selecting={!!pickerType}
93
- type="month"
94
- value={monthName}
95
- onNext={handleChangeNext}
96
- onPrev={handleChangePrevious}
97
- onPressDropdown={handlePressDropDown}
98
- pickerType={pickerType}
99
- />
100
- <HeaderItem
101
- selecting={!!pickerType}
102
- type="year"
103
- value={localDate.getFullYear()}
104
- onNext={handleChangeNext}
105
- onPrev={handleChangePrevious}
106
- onPressDropdown={handlePressDropDown}
107
- pickerType={pickerType}
108
- />
109
- </View>
110
- <View style={daysWrapperStyle}>
111
- <DayNames disableWeekDays={disableWeekDays} locale={locale} />
112
- </View>
113
- </View>
114
- );
115
- }
116
-
117
- export function getCalendarHeaderHeight(scrollMode: 'horizontal' | 'vertical') {
118
- if (scrollMode === 'horizontal') {
119
- return (
120
- buttonContainerHeight +
121
- buttonContainerMarginTop +
122
- buttonContainerMarginBottom +
123
- dayNamesHeight
124
- );
125
- }
126
- return dayNamesHeight;
127
- }
128
-
129
- export default memo(DatePickerDockedHeader);
@@ -1,17 +0,0 @@
1
- import { getRegisteredComponentWithFallback } from '../../core';
2
- import DatePickerDockedDefault from './DatePickerDocked';
3
-
4
- export const DatePickerDocked = getRegisteredComponentWithFallback(
5
- 'DatePickerDocked',
6
- DatePickerDockedDefault,
7
- );
8
-
9
- export type { DatePickerDockedProps } from './types';
10
- export {
11
- datePickerDockedHeaderStyles,
12
- datePickerDockedMonthItemStyles,
13
- datePickerDockedMonthStyles,
14
- datePickerDockedStyles,
15
- datePickerHeaderItemStyles,
16
- datePickerMonthPickerStyles,
17
- } from './utils';
@@ -1,11 +0,0 @@
1
- import type { ViewProps } from 'react-native';
2
-
3
- import type { DatePickerSingleProps } from '../DatePickerInline';
4
-
5
- export type DatePickerDockedProps = DatePickerSingleProps & {
6
- triggerRef: any;
7
- onToggle: () => void;
8
- onClose: () => void;
9
- isOpen: boolean;
10
- popoverContentProps?: ViewProps;
11
- };
@@ -1,157 +0,0 @@
1
- import { StyleSheet } from 'react-native-unistyles';
2
-
3
- import { getRegisteredComponentStylesWithFallback } from '../../core';
4
-
5
- const datePickerDockedStylesDefault = StyleSheet.create(theme => ({
6
- root: {
7
- roundness: theme.shapes.corner.extraSmall,
8
- headerBackgroundColor: theme.colors.surface,
9
- } as any,
10
-
11
- container: {
12
- minHeight: 360,
13
- minWidth: 360,
14
- },
15
- }));
16
-
17
- const datePickerDockedHeaderStylesDefault = StyleSheet.create(theme => ({
18
- datePickerHeader: {
19
- position: 'relative',
20
- flexDirection: 'row',
21
- alignItems: 'center',
22
- justifyContent: 'space-evenly',
23
- },
24
- daysWrapperStyle: {
25
- marginHorizontal: theme.spacings['3'],
26
- },
27
- }));
28
-
29
- const datePickerHeaderItemStylesDefault = StyleSheet.create(theme => ({
30
- buttonContainer: {
31
- height: 46,
32
- width: '50%',
33
- flexDirection: 'row',
34
- alignItems: 'center',
35
- },
36
- buttonWrapper: {},
37
- spacer: { flex: 1 },
38
- labelStyle: {
39
- ...theme.typescale.bodyMedium,
40
- opacity: 0.7,
41
- },
42
- buttonStyle: {
43
- alignSelf: 'center',
44
- borderRadius: theme.shapes.corner.extraSmall,
45
- },
46
- innerStyle: {
47
- paddingLeft: theme.spacings['0'],
48
- flexDirection: 'row',
49
- alignItems: 'center',
50
- borderRadius: theme.shapes.corner.extraSmall,
51
- },
52
- emtpyView: {
53
- width: 30,
54
- },
55
- }));
56
-
57
- const datePickerMonthPickerStylesDefault = StyleSheet.create(theme => ({
58
- root: {
59
- backgroundColor: theme.colors.surface,
60
- },
61
- month: {
62
- flex: 1,
63
- justifyContent: 'center',
64
- alignItems: 'flex-start',
65
- },
66
- selectedMonth: { color: theme.colors.onSurface },
67
- monthButton: {
68
- width: '100%',
69
- overflow: 'hidden',
70
- },
71
- monthInner: {
72
- height: 46,
73
- alignItems: 'center',
74
- justifyContent: 'flex-start',
75
- flexDirection: 'row',
76
- },
77
- selectedMonthInner: { backgroundColor: theme.colors.surfaceVariant },
78
- monthLabel: {
79
- fontSize: 16,
80
- },
81
- }));
82
-
83
- const datePickerDockedMonthItemStylesDefault = StyleSheet.create(theme => ({
84
- root: {
85
- backgroundColor: theme.colors.surface,
86
- },
87
-
88
- monthButton: {
89
- width: '100%',
90
- overflow: 'hidden',
91
- padding: theme.spacings['0'],
92
-
93
- variants: {
94
- state: {
95
- selected: {
96
- backgroundColor: theme.colors.surfaceVariant,
97
- },
98
- },
99
- },
100
- },
101
- monthInner: {
102
- height: 46,
103
- alignItems: 'center',
104
- justifyContent: 'flex-start',
105
- flexDirection: 'row',
106
- padding: theme.spacings['0'],
107
- },
108
- monthLabel: {
109
- fontSize: 16,
110
-
111
- variants: {
112
- state: {
113
- selected: {
114
- color: theme.colors.onSurface,
115
- },
116
- },
117
- },
118
- },
119
- }));
120
-
121
- const datePickerDockedMonthStylesDefault = StyleSheet.create(theme => ({
122
- dockedHeaderStyle: {
123
- alignItems: 'flex-start',
124
- marginLeft: theme.spacings['4'],
125
- },
126
- weekContainerStyle: {
127
- marginHorizontal: theme.spacings['3'],
128
- },
129
- backDropStyle: {
130
- backgroundColor: 'transparent',
131
- },
132
- }));
133
-
134
- export const datePickerDockedStyles = getRegisteredComponentStylesWithFallback(
135
- 'DatePickerDocked',
136
- datePickerDockedStylesDefault,
137
- );
138
- export const datePickerDockedHeaderStyles = getRegisteredComponentStylesWithFallback(
139
- 'DatePickerDocked_Header',
140
- datePickerDockedHeaderStylesDefault,
141
- );
142
- export const datePickerHeaderItemStyles = getRegisteredComponentStylesWithFallback(
143
- 'DatePicker_HeaderItem',
144
- datePickerHeaderItemStylesDefault,
145
- );
146
- export const datePickerMonthPickerStyles = getRegisteredComponentStylesWithFallback(
147
- 'DatePickerDocked_MonthPicker',
148
- datePickerMonthPickerStylesDefault,
149
- );
150
- export const datePickerDockedMonthItemStyles = getRegisteredComponentStylesWithFallback(
151
- 'DatePickerDocked_MonthItem',
152
- datePickerDockedMonthItemStylesDefault,
153
- );
154
- export const datePickerDockedMonthStyles = getRegisteredComponentStylesWithFallback(
155
- 'DatePickerDocked_Month',
156
- datePickerDockedMonthStylesDefault,
157
- );