react-native-molecules 0.5.0-beta.2 → 0.5.0-beta.20
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/README.md +1 -1
- package/components/Accordion/Accordion.tsx +2 -6
- package/components/Accordion/AccordionItem.tsx +16 -12
- package/components/Accordion/AccordionItemContent.tsx +6 -1
- package/components/Accordion/AccordionItemHeader.tsx +1 -1
- package/components/Accordion/utils.ts +6 -0
- package/components/ActivityIndicator/ActivityIndicator.tsx +6 -15
- package/components/Appbar/AppbarBase.tsx +18 -13
- package/components/Button/Button.tsx +209 -264
- package/components/Button/index.tsx +9 -3
- package/components/Button/types.ts +16 -2
- package/components/Button/utils.ts +230 -208
- package/components/Checkbox/CheckboxBase.tsx +23 -128
- package/components/Checkbox/utils.ts +0 -25
- package/components/Chip/Chip.tsx +40 -52
- package/components/Chip/utils.ts +3 -7
- package/components/DateField/DateField.tsx +110 -0
- package/components/DateField/index.tsx +6 -0
- package/components/{DatePickerInput/inputUtils.ts → DateField/useDateFieldState.ts} +17 -49
- package/components/DatePicker/DateCalendar.tsx +83 -0
- package/components/DatePicker/DatePickerActions.tsx +73 -0
- package/components/DatePicker/DatePickerModal.tsx +234 -0
- package/components/DatePicker/DatePickerPopover.tsx +79 -0
- package/components/DatePicker/DatePickerProvider.tsx +152 -0
- package/components/DatePicker/DatePickerTrigger.tsx +23 -0
- package/components/DatePicker/context.tsx +82 -0
- package/components/DatePicker/index.tsx +44 -0
- package/components/DatePicker/utils.ts +293 -0
- package/components/DatePickerInline/DatePickerContext.tsx +1 -0
- package/components/DatePickerInline/DatePickerDockedHeader.tsx +113 -0
- package/components/DatePickerInline/DatePickerInline.tsx +16 -15
- package/components/DatePickerInline/DatePickerInlineBase.tsx +7 -1
- package/components/DatePickerInline/Day.tsx +25 -1
- package/components/DatePickerInline/DayRange.tsx +2 -4
- package/components/DatePickerInline/HeaderItem.tsx +42 -27
- package/components/DatePickerInline/Month.tsx +45 -65
- package/components/DatePickerInline/MonthPicker.tsx +25 -41
- package/components/DatePickerInline/Swiper.native.tsx +21 -4
- package/components/DatePickerInline/Swiper.tsx +168 -13
- package/components/DatePickerInline/Week.tsx +6 -1
- package/components/DatePickerInline/YearPicker.tsx +206 -53
- package/components/DatePickerInline/dateUtils.tsx +17 -12
- package/components/DatePickerInline/types.ts +3 -0
- package/components/DatePickerInline/utils.ts +66 -29
- package/components/Drawer/Drawer.tsx +17 -6
- package/components/ElementGroup/ElementGroup.tsx +16 -14
- package/components/FilePicker/FilePicker.tsx +48 -78
- package/components/FilePicker/index.tsx +2 -1
- package/components/FilePicker/utils.ts +9 -0
- package/components/HelperText/HelperText.tsx +0 -35
- package/components/Icon/iconFactory.tsx +3 -3
- package/components/Icon/index.tsx +1 -1
- package/components/Icon/types.ts +17 -6
- package/components/IconButton/IconButton.tsx +42 -57
- package/components/IconButton/utils.ts +142 -33
- package/components/ListItem/ListItem.tsx +3 -1
- package/components/ListItem/utils.ts +1 -1
- package/components/LoadingIndicator/LoadingIndicator.tsx +253 -0
- package/components/LoadingIndicator/LoadingIndicator.web.tsx +136 -0
- package/components/LoadingIndicator/index.tsx +13 -0
- package/components/LoadingIndicator/utils.ts +117 -0
- package/components/Menu/Menu.tsx +3 -18
- package/components/NavigationRail/NavigationRail.tsx +15 -9
- package/components/Popover/Popover.tsx +122 -145
- package/components/Popover/PopoverRoot.tsx +74 -0
- package/components/Popover/common.ts +50 -34
- package/components/Popover/index.ts +18 -1
- package/components/Popover/usePlatformMeasure.native.ts +90 -0
- package/components/Popover/usePlatformMeasure.ts +118 -0
- package/components/Popover/utils.ts +34 -0
- package/components/Select/Select.tsx +368 -507
- package/components/Select/context.tsx +72 -0
- package/components/Select/index.ts +8 -14
- package/components/Select/types.ts +2 -4
- package/components/Select/utils.ts +144 -0
- package/components/Slot/Slot.tsx +244 -0
- package/components/Slot/compose-refs.tsx +62 -0
- package/components/Slot/index.tsx +8 -0
- package/components/Surface/Surface.android.tsx +34 -8
- package/components/Surface/Surface.ios.tsx +36 -29
- package/components/Surface/Surface.tsx +31 -4
- package/components/Surface/utils.ts +44 -30
- package/components/Switch/Switch.tsx +8 -2
- package/components/Tabs/TabItem.tsx +35 -58
- package/components/Tabs/TabLabel.tsx +5 -9
- package/components/Tabs/Tabs.tsx +154 -148
- package/components/Tabs/utils.ts +15 -2
- package/components/TextInput/TextInput.tsx +658 -575
- package/components/TextInput/index.tsx +19 -3
- package/components/TextInput/types.ts +76 -27
- package/components/TextInput/utils.ts +225 -145
- package/components/TimeField/TimeField.tsx +75 -0
- package/components/TimeField/index.tsx +6 -0
- package/components/TimeField/useTimeFieldState.ts +70 -0
- package/components/{TimePickerField/sanitizeTime.ts → TimeField/utils.ts} +77 -10
- package/components/TimePicker/TimeInput.tsx +87 -37
- package/components/TimePicker/TimeInputs.tsx +137 -49
- package/components/TimePicker/TimePicker.tsx +73 -10
- package/components/TimePicker/TimePickerModal.tsx +186 -0
- package/components/TimePicker/context.tsx +17 -0
- package/components/TimePicker/index.tsx +15 -3
- package/components/TimePicker/utils.ts +93 -0
- package/components/Tooltip/Tooltip.tsx +42 -67
- package/components/Tooltip/TooltipContent.tsx +32 -5
- package/components/Tooltip/TooltipTrigger.tsx +20 -20
- package/components/Tooltip/index.tsx +1 -1
- package/components/TouchableRipple/TouchableRipple.native.tsx +50 -14
- package/components/TouchableRipple/TouchableRipple.tsx +137 -47
- package/hocs/withPortal.tsx +1 -1
- package/hooks/index.tsx +0 -6
- package/hooks/useActionState.tsx +19 -8
- package/hooks/useControlledValue.tsx +20 -4
- package/hooks/useFilePicker.tsx +6 -16
- package/hooks/useWhatHasUpdated.tsx +48 -0
- package/package.json +17 -13
- package/shortcuts-manager/ShortcutsManager/ShortcutsManager.tsx +5 -2
- package/styles/shadow.ts +2 -1
- package/styles/themes/LightTheme.tsx +1 -1
- package/utils/DocumentPicker/documentPicker.ts +78 -27
- package/utils/DocumentPicker/types.ts +0 -1
- package/utils/extractPropertiesFromStyles.ts +25 -0
- package/utils/extractSubcomponents.ts +89 -0
- package/utils/lodash.ts +77 -5
- package/components/DatePickerDocked/DatePickerDocked.tsx +0 -30
- package/components/DatePickerDocked/DatePickerDockedHeader.tsx +0 -129
- package/components/DatePickerDocked/index.tsx +0 -17
- package/components/DatePickerDocked/types.ts +0 -11
- package/components/DatePickerDocked/utils.ts +0 -157
- package/components/DatePickerInput/DatePickerInput.tsx +0 -139
- package/components/DatePickerInput/DatePickerInputModal.tsx +0 -48
- package/components/DatePickerInput/DatePickerInputWithoutModal.tsx +0 -77
- package/components/DatePickerInput/DateRangeInput.tsx +0 -88
- package/components/DatePickerInput/index.tsx +0 -10
- package/components/DatePickerInput/types.ts +0 -28
- package/components/DatePickerInput/utils.ts +0 -15
- package/components/DatePickerModal/AnimatedCrossView.tsx +0 -94
- package/components/DatePickerModal/CalendarEdit.tsx +0 -139
- package/components/DatePickerModal/DatePickerModal.tsx +0 -85
- package/components/DatePickerModal/DatePickerModalContent.tsx +0 -155
- package/components/DatePickerModal/DatePickerModalContentHeader.tsx +0 -213
- package/components/DatePickerModal/DatePickerModalHeader.tsx +0 -74
- package/components/DatePickerModal/DatePickerModalHeaderBackground.tsx +0 -13
- package/components/DatePickerModal/index.tsx +0 -16
- package/components/DatePickerModal/types.ts +0 -92
- package/components/DatePickerModal/utils.ts +0 -122
- package/components/DateTimePicker/DateTimePicker.tsx +0 -172
- package/components/DateTimePicker/index.tsx +0 -10
- package/components/DateTimePicker/utils.ts +0 -12
- package/components/Popover/Popover.native.tsx +0 -185
- package/components/TimePickerField/TimePickerField.tsx +0 -152
- package/components/TimePickerField/index.tsx +0 -10
- package/components/TimePickerField/utils.ts +0 -94
- package/components/TimePickerModal/TimePickerModal.tsx +0 -115
- package/components/TimePickerModal/index.tsx +0 -10
- package/components/TimePickerModal/utils.ts +0 -47
- package/hooks/useSearchable.tsx +0 -74
- package/hooks/useSubcomponents.tsx +0 -59
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import { memo, useCallback, useMemo, useState } from 'react';
|
|
3
|
+
import {
|
|
4
|
+
Platform,
|
|
5
|
+
StatusBar,
|
|
6
|
+
type StatusBarStyle,
|
|
7
|
+
StyleSheet,
|
|
8
|
+
useWindowDimensions,
|
|
9
|
+
View,
|
|
10
|
+
} from 'react-native';
|
|
11
|
+
|
|
12
|
+
import { getRegisteredComponentWithFallback } from '../../core';
|
|
13
|
+
import { format } from '../../utils/date-fns';
|
|
14
|
+
import { DateField } from '../DateField';
|
|
15
|
+
import { IconButton } from '../IconButton';
|
|
16
|
+
import { Modal, type ModalProps } from '../Modal';
|
|
17
|
+
import { Portal } from '../Portal';
|
|
18
|
+
import { Text } from '../Text';
|
|
19
|
+
import { TextInput } from '../TextInput';
|
|
20
|
+
import type { DatePickerContextType, DatePickerValue } from './context';
|
|
21
|
+
import {
|
|
22
|
+
DatePickerContext,
|
|
23
|
+
useDatePickerContext,
|
|
24
|
+
useOptionalDatePickerContext,
|
|
25
|
+
withDraftLayer,
|
|
26
|
+
} from './context';
|
|
27
|
+
import { DateCalendar } from './DateCalendar';
|
|
28
|
+
import { DatePickerActions } from './DatePickerActions';
|
|
29
|
+
import { DatePickerProvider } from './DatePickerProvider';
|
|
30
|
+
import { datePickerModalStyles } from './utils';
|
|
31
|
+
|
|
32
|
+
export type DatePickerModalProps = Omit<ModalProps, 'children' | 'isOpen' | 'onClose'> & {
|
|
33
|
+
children?: ReactNode;
|
|
34
|
+
isOpen?: boolean;
|
|
35
|
+
onClose?: () => void;
|
|
36
|
+
value?: DatePickerValue;
|
|
37
|
+
onChange?: (value: DatePickerValue) => void;
|
|
38
|
+
label?: string;
|
|
39
|
+
disableStatusBar?: boolean;
|
|
40
|
+
disableStatusBarPadding?: boolean;
|
|
41
|
+
confirmLabel?: string;
|
|
42
|
+
cancelLabel?: string;
|
|
43
|
+
editIcon?: string;
|
|
44
|
+
calendarIcon?: string;
|
|
45
|
+
headerLayout?: 'inline' | 'docked';
|
|
46
|
+
/** Override the surface default draft mode. Modal defaults to `true` (staged commit). */
|
|
47
|
+
draft?: boolean;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
type BodyProps = Omit<DatePickerModalProps, 'isOpen' | 'onClose' | 'value' | 'onChange' | 'draft'>;
|
|
51
|
+
|
|
52
|
+
function DatePickerModalBody({
|
|
53
|
+
children,
|
|
54
|
+
style,
|
|
55
|
+
label = 'Select date',
|
|
56
|
+
disableStatusBar,
|
|
57
|
+
disableStatusBarPadding,
|
|
58
|
+
confirmLabel = 'OK',
|
|
59
|
+
cancelLabel = 'Cancel',
|
|
60
|
+
editIcon = 'pencil',
|
|
61
|
+
calendarIcon = 'calendar',
|
|
62
|
+
headerLayout,
|
|
63
|
+
...rest
|
|
64
|
+
}: BodyProps) {
|
|
65
|
+
const ctx = useDatePickerContext();
|
|
66
|
+
const dimensions = useWindowDimensions();
|
|
67
|
+
const [editing, setEditing] = useState(false);
|
|
68
|
+
|
|
69
|
+
const { containerStyle, headerStyle, barStyle, modalStyle } = useMemo(() => {
|
|
70
|
+
const isHeaderBackgroundLight = true;
|
|
71
|
+
return {
|
|
72
|
+
containerStyle: [StyleSheet.absoluteFill, styles.container],
|
|
73
|
+
headerStyle: [
|
|
74
|
+
datePickerModalStyles.headlineContainer,
|
|
75
|
+
{
|
|
76
|
+
paddingTop:
|
|
77
|
+
disableStatusBarPadding || Platform.OS !== 'android'
|
|
78
|
+
? datePickerModalStyles.headlineContainer.paddingTop
|
|
79
|
+
: (datePickerModalStyles.headlineContainer.paddingTop as number) +
|
|
80
|
+
(StatusBar.currentHeight || 0),
|
|
81
|
+
},
|
|
82
|
+
],
|
|
83
|
+
barStyle: (isHeaderBackgroundLight
|
|
84
|
+
? 'dark-content'
|
|
85
|
+
: 'light-content') as StatusBarStyle,
|
|
86
|
+
modalStyle: [
|
|
87
|
+
datePickerModalStyles.content,
|
|
88
|
+
dimensions.width > 650 ? { maxHeight: 600 } : { borderRadius: 0 },
|
|
89
|
+
style,
|
|
90
|
+
],
|
|
91
|
+
};
|
|
92
|
+
}, [dimensions.width, disableStatusBarPadding, style]);
|
|
93
|
+
|
|
94
|
+
const draft =
|
|
95
|
+
ctx.draftValue && typeof ctx.draftValue === 'object' && 'start' in ctx.draftValue
|
|
96
|
+
? null
|
|
97
|
+
: ctx.draftValue;
|
|
98
|
+
|
|
99
|
+
const summaryLabel = useMemo(() => {
|
|
100
|
+
if (editing) return 'Enter dates';
|
|
101
|
+
if (!draft) return 'Select date';
|
|
102
|
+
return format(draft, 'EEE, MMM d');
|
|
103
|
+
}, [draft, editing]);
|
|
104
|
+
|
|
105
|
+
const body = editing ? (
|
|
106
|
+
<View style={datePickerModalStyles.inputContainer}>
|
|
107
|
+
<DateField autoFocus>
|
|
108
|
+
<TextInput.Label>Date</TextInput.Label>
|
|
109
|
+
</DateField>
|
|
110
|
+
</View>
|
|
111
|
+
) : (
|
|
112
|
+
children ?? <DateCalendar headerLayout={headerLayout} showOutsideDays={false} />
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
return (
|
|
116
|
+
<View style={containerStyle} pointerEvents="box-none">
|
|
117
|
+
<Portal>
|
|
118
|
+
<Modal
|
|
119
|
+
{...rest}
|
|
120
|
+
isOpen={ctx.open}
|
|
121
|
+
onClose={ctx.cancel}
|
|
122
|
+
style={modalStyle}
|
|
123
|
+
elevation={0}>
|
|
124
|
+
<>
|
|
125
|
+
{disableStatusBar ? null : (
|
|
126
|
+
<StatusBar translucent={true} barStyle={barStyle} />
|
|
127
|
+
)}
|
|
128
|
+
<View style={datePickerModalStyles.frame}>
|
|
129
|
+
<View style={headerStyle}>
|
|
130
|
+
<View style={datePickerModalStyles.headerCopy}>
|
|
131
|
+
<Text style={datePickerModalStyles.headline}>{label}</Text>
|
|
132
|
+
<Text
|
|
133
|
+
style={datePickerModalStyles.supporting}
|
|
134
|
+
numberOfLines={1}
|
|
135
|
+
adjustsFontSizeToFit={true}>
|
|
136
|
+
{summaryLabel}
|
|
137
|
+
</Text>
|
|
138
|
+
</View>
|
|
139
|
+
<IconButton
|
|
140
|
+
name={editing ? calendarIcon : editIcon}
|
|
141
|
+
accessibilityLabel={
|
|
142
|
+
editing ? 'Show calendar' : 'Enter date manually'
|
|
143
|
+
}
|
|
144
|
+
onPress={() => setEditing(prev => !prev)}
|
|
145
|
+
style={datePickerModalStyles.modeToggle}
|
|
146
|
+
/>
|
|
147
|
+
</View>
|
|
148
|
+
<View style={datePickerModalStyles.body}>{body}</View>
|
|
149
|
+
<DatePickerActions
|
|
150
|
+
cancelLabel={cancelLabel}
|
|
151
|
+
confirmLabel={confirmLabel}
|
|
152
|
+
/>
|
|
153
|
+
</View>
|
|
154
|
+
</>
|
|
155
|
+
</Modal>
|
|
156
|
+
</Portal>
|
|
157
|
+
</View>
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function DatePickerModalLayer({
|
|
162
|
+
base,
|
|
163
|
+
draft: draftProp,
|
|
164
|
+
bodyProps,
|
|
165
|
+
}: {
|
|
166
|
+
base: DatePickerContextType;
|
|
167
|
+
draft: boolean | undefined;
|
|
168
|
+
bodyProps: BodyProps;
|
|
169
|
+
}) {
|
|
170
|
+
const effectiveDraft = draftProp ?? base.providerDraft ?? true;
|
|
171
|
+
const ctx = useMemo(() => withDraftLayer(base, effectiveDraft), [base, effectiveDraft]);
|
|
172
|
+
if (!base.open) return null;
|
|
173
|
+
return (
|
|
174
|
+
<DatePickerContext value={ctx}>
|
|
175
|
+
<DatePickerModalBody {...bodyProps} />
|
|
176
|
+
</DatePickerContext>
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function DatePickerModalAdapter({
|
|
181
|
+
draft,
|
|
182
|
+
bodyProps,
|
|
183
|
+
}: {
|
|
184
|
+
draft: boolean | undefined;
|
|
185
|
+
bodyProps: BodyProps;
|
|
186
|
+
}) {
|
|
187
|
+
const base = useDatePickerContext();
|
|
188
|
+
return <DatePickerModalLayer base={base} draft={draft} bodyProps={bodyProps} />;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function DatePickerModalInner({
|
|
192
|
+
isOpen: isOpenProp,
|
|
193
|
+
onClose: onCloseProp,
|
|
194
|
+
value: valueProp,
|
|
195
|
+
onChange: onChangeProp,
|
|
196
|
+
draft: draftProp,
|
|
197
|
+
...rest
|
|
198
|
+
}: DatePickerModalProps) {
|
|
199
|
+
const outer = useOptionalDatePickerContext();
|
|
200
|
+
|
|
201
|
+
const onOpenChange = useCallback(
|
|
202
|
+
(next: boolean) => {
|
|
203
|
+
if (!next) onCloseProp?.();
|
|
204
|
+
},
|
|
205
|
+
[onCloseProp],
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
if (outer) {
|
|
209
|
+
return <DatePickerModalLayer base={outer} draft={draftProp} bodyProps={rest} />;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return (
|
|
213
|
+
<DatePickerProvider
|
|
214
|
+
value={valueProp}
|
|
215
|
+
onChange={onChangeProp}
|
|
216
|
+
open={isOpenProp}
|
|
217
|
+
onOpenChange={onOpenChange}>
|
|
218
|
+
<DatePickerModalAdapter draft={draftProp} bodyProps={rest} />
|
|
219
|
+
</DatePickerProvider>
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const styles = StyleSheet.create({
|
|
224
|
+
container: { backgroundColor: 'transparent' },
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
const DatePickerModalDefault = memo(DatePickerModalInner);
|
|
228
|
+
|
|
229
|
+
export default DatePickerModalDefault;
|
|
230
|
+
|
|
231
|
+
export const DatePickerModal = getRegisteredComponentWithFallback(
|
|
232
|
+
'DatePickerModal',
|
|
233
|
+
DatePickerModalDefault,
|
|
234
|
+
);
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { ReactNode, RefObject } from 'react';
|
|
2
|
+
import { memo, useMemo } from 'react';
|
|
3
|
+
import { View } from 'react-native';
|
|
4
|
+
import { StyleSheet } from 'react-native-unistyles';
|
|
5
|
+
|
|
6
|
+
import { getRegisteredComponentWithFallback } from '../../core';
|
|
7
|
+
import { Popover, type PopoverProps } from '../Popover';
|
|
8
|
+
import { DatePickerContext, useOptionalDatePickerContext, withDraftLayer } from './context';
|
|
9
|
+
import { DatePickerActions } from './DatePickerActions';
|
|
10
|
+
|
|
11
|
+
export type DatePickerPopoverProps = Omit<
|
|
12
|
+
PopoverProps,
|
|
13
|
+
'children' | 'triggerRef' | 'isOpen' | 'onClose'
|
|
14
|
+
> & {
|
|
15
|
+
children: ReactNode;
|
|
16
|
+
triggerRef?: RefObject<any>;
|
|
17
|
+
isOpen?: boolean;
|
|
18
|
+
onClose?: () => void;
|
|
19
|
+
/** Override the surface default draft mode. Popover defaults to `false` (auto-save). */
|
|
20
|
+
draft?: boolean;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function DatePickerPopoverInner({
|
|
24
|
+
children,
|
|
25
|
+
triggerRef: triggerRefProp,
|
|
26
|
+
isOpen: isOpenProp,
|
|
27
|
+
onClose: onCloseProp,
|
|
28
|
+
draft: draftProp,
|
|
29
|
+
...rest
|
|
30
|
+
}: DatePickerPopoverProps) {
|
|
31
|
+
const base = useOptionalDatePickerContext();
|
|
32
|
+
|
|
33
|
+
const effectiveDraft = draftProp ?? base?.providerDraft ?? false;
|
|
34
|
+
const ctx = useMemo(
|
|
35
|
+
() => (base ? withDraftLayer(base, effectiveDraft) : null),
|
|
36
|
+
[base, effectiveDraft],
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const triggerRef = triggerRefProp ?? ctx?.triggerRef;
|
|
40
|
+
const isOpen = isOpenProp ?? ctx?.open ?? false;
|
|
41
|
+
const onClose = onCloseProp ?? (() => ctx?.setOpen(false));
|
|
42
|
+
|
|
43
|
+
if (!triggerRef || !ctx) return null;
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<DatePickerContext value={ctx}>
|
|
47
|
+
<Popover
|
|
48
|
+
align="end"
|
|
49
|
+
position="bottom"
|
|
50
|
+
offset={20}
|
|
51
|
+
{...rest}
|
|
52
|
+
style={[styles.popover, rest.style]}
|
|
53
|
+
triggerRef={triggerRef}
|
|
54
|
+
isOpen={isOpen}
|
|
55
|
+
onClose={onClose}>
|
|
56
|
+
<View>
|
|
57
|
+
{children}
|
|
58
|
+
<DatePickerActions />
|
|
59
|
+
</View>
|
|
60
|
+
</Popover>
|
|
61
|
+
</DatePickerContext>
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const styles = StyleSheet.create(theme => ({
|
|
66
|
+
popover: {
|
|
67
|
+
borderRadius: theme.shapes.corner.large,
|
|
68
|
+
overflow: 'hidden',
|
|
69
|
+
},
|
|
70
|
+
}));
|
|
71
|
+
|
|
72
|
+
const DatePickerPopoverDefault = memo(DatePickerPopoverInner);
|
|
73
|
+
|
|
74
|
+
export default DatePickerPopoverDefault;
|
|
75
|
+
|
|
76
|
+
export const DatePickerPopover = getRegisteredComponentWithFallback(
|
|
77
|
+
'DatePickerPopover',
|
|
78
|
+
DatePickerPopoverDefault,
|
|
79
|
+
);
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import { memo, useCallback, useMemo, useRef, useState } from 'react';
|
|
3
|
+
|
|
4
|
+
import { getRegisteredComponentWithFallback } from '../../core';
|
|
5
|
+
import { useControlledValue } from '../../hooks';
|
|
6
|
+
import type { ValidRangeType } from '../DatePickerInline';
|
|
7
|
+
import type { DatePickerContextType, DatePickerMode, DatePickerValue, RangeValue } from './context';
|
|
8
|
+
import { DatePickerContext, withDraftLayer } from './context';
|
|
9
|
+
|
|
10
|
+
const DATE_FORMAT_BY_MODE: Record<DatePickerMode, string> = {
|
|
11
|
+
date: 'dd/MM/yyyy',
|
|
12
|
+
time: 'HH:mm',
|
|
13
|
+
datetime: 'dd/MM/yyyy HH:mm',
|
|
14
|
+
range: 'dd/MM/yyyy',
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const emptyRange: RangeValue = { start: null, end: null };
|
|
18
|
+
|
|
19
|
+
const nullValueFor = (mode: DatePickerMode): DatePickerValue =>
|
|
20
|
+
mode === 'range' ? emptyRange : null;
|
|
21
|
+
|
|
22
|
+
export type DatePickerProviderProps = {
|
|
23
|
+
mode?: DatePickerMode;
|
|
24
|
+
value?: DatePickerValue;
|
|
25
|
+
defaultValue?: DatePickerValue;
|
|
26
|
+
onChange?: (value: DatePickerValue) => void;
|
|
27
|
+
|
|
28
|
+
draft?: boolean;
|
|
29
|
+
open?: boolean;
|
|
30
|
+
defaultOpen?: boolean;
|
|
31
|
+
onOpenChange?: (open: boolean) => void;
|
|
32
|
+
|
|
33
|
+
locale?: string;
|
|
34
|
+
validRange?: ValidRangeType;
|
|
35
|
+
is24Hour?: boolean;
|
|
36
|
+
dateFormat?: string;
|
|
37
|
+
disabled?: boolean;
|
|
38
|
+
|
|
39
|
+
children: ReactNode;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
function DatePickerProviderInner({
|
|
43
|
+
mode = 'date',
|
|
44
|
+
value: valueProp,
|
|
45
|
+
defaultValue,
|
|
46
|
+
onChange,
|
|
47
|
+
draft: draftProp,
|
|
48
|
+
open: openProp,
|
|
49
|
+
defaultOpen = false,
|
|
50
|
+
onOpenChange,
|
|
51
|
+
locale,
|
|
52
|
+
validRange,
|
|
53
|
+
is24Hour = false,
|
|
54
|
+
dateFormat,
|
|
55
|
+
disabled,
|
|
56
|
+
children,
|
|
57
|
+
}: DatePickerProviderProps) {
|
|
58
|
+
const triggerRef = useRef<any>(null);
|
|
59
|
+
|
|
60
|
+
const [value, setCommittedValue] = useControlledValue<DatePickerValue>({
|
|
61
|
+
value: valueProp,
|
|
62
|
+
defaultValue: defaultValue ?? nullValueFor(mode),
|
|
63
|
+
onChange,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const [open, setOpen] = useControlledValue<boolean>({
|
|
67
|
+
value: openProp,
|
|
68
|
+
defaultValue: defaultOpen,
|
|
69
|
+
onChange: onOpenChange,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const [draftBuffer, setDraftBuffer] = useState<DatePickerValue>(value);
|
|
73
|
+
|
|
74
|
+
const prevOpenRef = useRef(open);
|
|
75
|
+
const prevValueRef = useRef(value);
|
|
76
|
+
|
|
77
|
+
if (open && !prevOpenRef.current && draftBuffer !== value) {
|
|
78
|
+
setDraftBuffer(value);
|
|
79
|
+
}
|
|
80
|
+
if (prevValueRef.current !== value && draftBuffer !== value) {
|
|
81
|
+
setDraftBuffer(value);
|
|
82
|
+
}
|
|
83
|
+
prevOpenRef.current = open;
|
|
84
|
+
prevValueRef.current = value;
|
|
85
|
+
|
|
86
|
+
const commitValue = useCallback(
|
|
87
|
+
(next: DatePickerValue) => {
|
|
88
|
+
setCommittedValue(next);
|
|
89
|
+
setDraftBuffer(next);
|
|
90
|
+
},
|
|
91
|
+
[setCommittedValue],
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const resolvedFormat = dateFormat ?? DATE_FORMAT_BY_MODE[mode];
|
|
95
|
+
const defaultDraft = draftProp ?? true;
|
|
96
|
+
|
|
97
|
+
const ctx = useMemo<DatePickerContextType>(() => {
|
|
98
|
+
const base: DatePickerContextType = {
|
|
99
|
+
mode,
|
|
100
|
+
value,
|
|
101
|
+
draftBuffer,
|
|
102
|
+
setCommittedValue,
|
|
103
|
+
setDraftBuffer,
|
|
104
|
+
providerDraft: draftProp,
|
|
105
|
+
|
|
106
|
+
// Placeholders — withDraftLayer rebuilds these.
|
|
107
|
+
draft: defaultDraft,
|
|
108
|
+
draftValue: value,
|
|
109
|
+
setValue: () => {},
|
|
110
|
+
commit: () => {},
|
|
111
|
+
cancel: () => {},
|
|
112
|
+
|
|
113
|
+
commitValue,
|
|
114
|
+
|
|
115
|
+
open,
|
|
116
|
+
setOpen,
|
|
117
|
+
triggerRef,
|
|
118
|
+
locale,
|
|
119
|
+
validRange,
|
|
120
|
+
is24Hour,
|
|
121
|
+
dateFormat: resolvedFormat,
|
|
122
|
+
disabled,
|
|
123
|
+
};
|
|
124
|
+
return withDraftLayer(base, defaultDraft);
|
|
125
|
+
}, [
|
|
126
|
+
mode,
|
|
127
|
+
value,
|
|
128
|
+
draftBuffer,
|
|
129
|
+
setCommittedValue,
|
|
130
|
+
draftProp,
|
|
131
|
+
defaultDraft,
|
|
132
|
+
commitValue,
|
|
133
|
+
open,
|
|
134
|
+
setOpen,
|
|
135
|
+
locale,
|
|
136
|
+
validRange,
|
|
137
|
+
is24Hour,
|
|
138
|
+
resolvedFormat,
|
|
139
|
+
disabled,
|
|
140
|
+
]);
|
|
141
|
+
|
|
142
|
+
return <DatePickerContext value={ctx}>{children}</DatePickerContext>;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const DatePickerProviderDefault = memo(DatePickerProviderInner);
|
|
146
|
+
|
|
147
|
+
export default DatePickerProviderDefault;
|
|
148
|
+
|
|
149
|
+
export const DatePickerProvider = getRegisteredComponentWithFallback(
|
|
150
|
+
'DatePickerProvider',
|
|
151
|
+
DatePickerProviderDefault,
|
|
152
|
+
);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ReactElement } from 'react';
|
|
2
|
+
import { memo, useCallback } from 'react';
|
|
3
|
+
|
|
4
|
+
import { getRegisteredComponentWithFallback } from '../../core';
|
|
5
|
+
import { Slot } from '../Slot';
|
|
6
|
+
import { useDatePickerContext } from './context';
|
|
7
|
+
|
|
8
|
+
const DatePickerTriggerDefault = memo(({ children }: { children: ReactElement }) => {
|
|
9
|
+
const { open, setOpen, disabled, triggerRef } = useDatePickerContext();
|
|
10
|
+
|
|
11
|
+
const onPress = useCallback(() => setOpen(!open), [open, setOpen]);
|
|
12
|
+
|
|
13
|
+
return (
|
|
14
|
+
<Slot ref={triggerRef} onPress={onPress} disabled={disabled}>
|
|
15
|
+
{children}
|
|
16
|
+
</Slot>
|
|
17
|
+
);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export const DatePickerTrigger = getRegisteredComponentWithFallback(
|
|
21
|
+
'DatePickerTrigger',
|
|
22
|
+
DatePickerTriggerDefault,
|
|
23
|
+
);
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { RefObject } from 'react';
|
|
2
|
+
import { createContext, useContext } from 'react';
|
|
3
|
+
|
|
4
|
+
import type { ValidRangeType } from '../DatePickerInline';
|
|
5
|
+
import { registerPortalContext } from '../Portal';
|
|
6
|
+
|
|
7
|
+
export type DatePickerMode = 'date' | 'time' | 'datetime' | 'range';
|
|
8
|
+
|
|
9
|
+
export type DateValue = Date | null;
|
|
10
|
+
export type RangeValue = { start: Date | null; end: Date | null };
|
|
11
|
+
export type DatePickerValue = DateValue | RangeValue;
|
|
12
|
+
|
|
13
|
+
export type DatePickerContextType = {
|
|
14
|
+
mode: DatePickerMode;
|
|
15
|
+
|
|
16
|
+
// Raw state shared across layers.
|
|
17
|
+
value: DatePickerValue;
|
|
18
|
+
draftBuffer: DatePickerValue;
|
|
19
|
+
setCommittedValue: (value: DatePickerValue) => void;
|
|
20
|
+
setDraftBuffer: (value: DatePickerValue) => void;
|
|
21
|
+
|
|
22
|
+
// User's explicit `draft` prop on <DatePicker.Provider>. Undefined if not passed.
|
|
23
|
+
providerDraft: boolean | undefined;
|
|
24
|
+
|
|
25
|
+
// Resolved for this layer — surfaces override these via withDraftLayer.
|
|
26
|
+
draft: boolean;
|
|
27
|
+
draftValue: DatePickerValue;
|
|
28
|
+
setValue: (value: DatePickerValue) => void;
|
|
29
|
+
commit: () => void;
|
|
30
|
+
cancel: () => void;
|
|
31
|
+
|
|
32
|
+
// Bypass setter for typed-entry fields — always commits, ignores draft.
|
|
33
|
+
commitValue: (value: DatePickerValue) => void;
|
|
34
|
+
|
|
35
|
+
open: boolean;
|
|
36
|
+
setOpen: (open: boolean) => void;
|
|
37
|
+
triggerRef: RefObject<any>;
|
|
38
|
+
locale?: string;
|
|
39
|
+
validRange?: ValidRangeType;
|
|
40
|
+
is24Hour: boolean;
|
|
41
|
+
dateFormat: string;
|
|
42
|
+
disabled?: boolean;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** Rebuilds draft-derived fields on top of a base context for a specific layer's draft mode. */
|
|
46
|
+
export function withDraftLayer(base: DatePickerContextType, draft: boolean): DatePickerContextType {
|
|
47
|
+
return {
|
|
48
|
+
...base,
|
|
49
|
+
draft,
|
|
50
|
+
draftValue: draft ? base.draftBuffer : base.value,
|
|
51
|
+
setValue: next => {
|
|
52
|
+
if (draft) base.setDraftBuffer(next);
|
|
53
|
+
else base.setCommittedValue(next);
|
|
54
|
+
},
|
|
55
|
+
commit: () => {
|
|
56
|
+
if (draft) base.setCommittedValue(base.draftBuffer);
|
|
57
|
+
base.setOpen(false);
|
|
58
|
+
},
|
|
59
|
+
cancel: () => {
|
|
60
|
+
base.setDraftBuffer(base.value);
|
|
61
|
+
base.setOpen(false);
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export const DatePickerContext = createContext<DatePickerContextType | null>(null);
|
|
67
|
+
|
|
68
|
+
export function useDatePickerContext(): DatePickerContextType {
|
|
69
|
+
const ctx = useContext(DatePickerContext);
|
|
70
|
+
if (!ctx) {
|
|
71
|
+
throw new Error(
|
|
72
|
+
'useDatePickerContext must be used within a <DatePickerProvider>. Wrap your date/time components with <DatePickerProvider>.',
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
return ctx;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function useOptionalDatePickerContext(): DatePickerContextType | null {
|
|
79
|
+
return useContext(DatePickerContext);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
registerPortalContext(DatePickerContext);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { DateCalendar } from './DateCalendar';
|
|
2
|
+
import { DatePickerActions } from './DatePickerActions';
|
|
3
|
+
import { DatePickerModal } from './DatePickerModal';
|
|
4
|
+
import { DatePickerPopover } from './DatePickerPopover';
|
|
5
|
+
import { DatePickerProvider } from './DatePickerProvider';
|
|
6
|
+
import { DatePickerTrigger } from './DatePickerTrigger';
|
|
7
|
+
|
|
8
|
+
export const DatePicker = {
|
|
9
|
+
Provider: DatePickerProvider,
|
|
10
|
+
Trigger: DatePickerTrigger,
|
|
11
|
+
Actions: DatePickerActions,
|
|
12
|
+
Calendar: DateCalendar,
|
|
13
|
+
Modal: DatePickerModal,
|
|
14
|
+
Popover: DatePickerPopover,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type {
|
|
18
|
+
DatePickerContextType,
|
|
19
|
+
DatePickerMode,
|
|
20
|
+
DatePickerValue,
|
|
21
|
+
DateValue,
|
|
22
|
+
RangeValue,
|
|
23
|
+
} from './context';
|
|
24
|
+
export { DatePickerContext, useDatePickerContext, useOptionalDatePickerContext } from './context';
|
|
25
|
+
export type { DateCalendarProps } from './DateCalendar';
|
|
26
|
+
export type { DatePickerActionsProps } from './DatePickerActions';
|
|
27
|
+
export { DatePickerActions } from './DatePickerActions';
|
|
28
|
+
export type { DatePickerModalProps } from './DatePickerModal';
|
|
29
|
+
export { DatePickerModal } from './DatePickerModal';
|
|
30
|
+
export type { DatePickerPopoverProps } from './DatePickerPopover';
|
|
31
|
+
export { DatePickerPopover } from './DatePickerPopover';
|
|
32
|
+
export type { DatePickerProviderProps } from './DatePickerProvider';
|
|
33
|
+
export { DatePickerProvider } from './DatePickerProvider';
|
|
34
|
+
export { DatePickerTrigger } from './DatePickerTrigger';
|
|
35
|
+
export {
|
|
36
|
+
datePickerHeaderItemStyles,
|
|
37
|
+
datePickerModalContentHeaderStyles,
|
|
38
|
+
datePickerModalEditStyles,
|
|
39
|
+
datePickerModalHeaderBackgroundStyles,
|
|
40
|
+
datePickerModalHeaderStyles,
|
|
41
|
+
datePickerModalStyles,
|
|
42
|
+
datePickerMonthItemStyles,
|
|
43
|
+
datePickerMonthPickerStyles,
|
|
44
|
+
} from './utils';
|