react-native-molecules 0.5.0-beta.16 → 0.5.0-beta.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/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 +292 -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/ListItem/ListItem.tsx +3 -1
- package/components/ListItem/utils.ts +1 -1
- package/components/LoadingIndicator/index.tsx +1 -1
- package/components/Popover/Popover.native.tsx +4 -25
- package/components/Popover/Popover.tsx +4 -26
- package/components/Popover/utils.ts +41 -0
- package/components/Select/Select.tsx +7 -8
- package/components/Select/context.tsx +72 -0
- package/components/Select/index.ts +1 -0
- package/components/Select/utils.ts +0 -71
- package/components/Slot/compose-refs.tsx +2 -0
- 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/TimePicker.tsx +53 -9
- 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 +50 -0
- package/hooks/useActionState.tsx +19 -8
- package/package.json +6 -1
- 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 -130
- package/components/DatePickerInput/DatePickerInputModal.tsx +0 -48
- package/components/DatePickerInput/DatePickerInputWithoutModal.tsx +0 -73
- package/components/DatePickerInput/DateRangeInput.tsx +0 -88
- package/components/DatePickerInput/index.tsx +0 -11
- package/components/DatePickerInput/types.ts +0 -26
- package/components/DatePickerInput/utils.ts +0 -24
- package/components/DatePickerModal/AnimatedCrossView.tsx +0 -94
- package/components/DatePickerModal/CalendarEdit.tsx +0 -140
- 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/TimePickerField/TimePickerField.tsx +0 -154
- package/components/TimePickerField/index.tsx +0 -10
- package/components/TimePickerField/utils.ts +0 -94
- package/components/TimePickerModal/TimePickerModal.tsx +0 -119
- package/components/TimePickerModal/index.tsx +0 -10
- package/components/TimePickerModal/utils.ts +0 -47
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { memo, useCallback, useMemo, useRef } from 'react';
|
|
2
|
+
|
|
3
|
+
import type { DatePickerValue, RangeValue } from '../DatePicker/context';
|
|
4
|
+
import { useDatePickerContext } from '../DatePicker/context';
|
|
5
|
+
import type { TextInputProps } from '../TextInput';
|
|
6
|
+
import { TextInputWithMask } from '../TextInputWithMask';
|
|
7
|
+
import { useDateFieldState } from './useDateFieldState';
|
|
8
|
+
|
|
9
|
+
export type DateFieldInputMode = 'start' | 'end';
|
|
10
|
+
|
|
11
|
+
export type DateFieldProps = Omit<
|
|
12
|
+
TextInputProps,
|
|
13
|
+
'value' | 'defaultValue' | 'onChangeText' | 'inputMode'
|
|
14
|
+
> & {
|
|
15
|
+
inputMode?: DateFieldInputMode;
|
|
16
|
+
dateFormat?: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const DEFAULT_DATE_FORMAT = 'dd/MM/yyyy';
|
|
20
|
+
|
|
21
|
+
const isRange = (value: DatePickerValue): value is RangeValue =>
|
|
22
|
+
value !== null && typeof value === 'object' && 'start' in value && 'end' in value;
|
|
23
|
+
|
|
24
|
+
const preserveTime = (next: Date | null, prev: Date | null): Date | null => {
|
|
25
|
+
if (!next || !prev) return next;
|
|
26
|
+
|
|
27
|
+
const combined = new Date(next);
|
|
28
|
+
|
|
29
|
+
combined.setHours(
|
|
30
|
+
prev.getHours(),
|
|
31
|
+
prev.getMinutes(),
|
|
32
|
+
prev.getSeconds(),
|
|
33
|
+
prev.getMilliseconds(),
|
|
34
|
+
);
|
|
35
|
+
return combined;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
function DateField({
|
|
39
|
+
ref,
|
|
40
|
+
inputMode = 'start',
|
|
41
|
+
dateFormat = DEFAULT_DATE_FORMAT,
|
|
42
|
+
disabled: disabledProp,
|
|
43
|
+
onBlur,
|
|
44
|
+
onFocus,
|
|
45
|
+
...rest
|
|
46
|
+
}: DateFieldProps) {
|
|
47
|
+
const {
|
|
48
|
+
mode,
|
|
49
|
+
value,
|
|
50
|
+
commitValue,
|
|
51
|
+
validRange,
|
|
52
|
+
disabled: providerDisabled,
|
|
53
|
+
} = useDatePickerContext();
|
|
54
|
+
const isBlurredRef = useRef(true);
|
|
55
|
+
|
|
56
|
+
const disabled = disabledProp ?? providerDisabled;
|
|
57
|
+
|
|
58
|
+
const fieldValue = useMemo<Date | null>(() => {
|
|
59
|
+
if (mode === 'range') {
|
|
60
|
+
if (!isRange(value)) return null;
|
|
61
|
+
return inputMode === 'end' ? value.end : value.start;
|
|
62
|
+
}
|
|
63
|
+
return isRange(value) ? null : value;
|
|
64
|
+
}, [value, mode, inputMode]);
|
|
65
|
+
|
|
66
|
+
const onChange = useCallback(
|
|
67
|
+
(next: Date | null) => {
|
|
68
|
+
if (mode === 'range') {
|
|
69
|
+
const current: RangeValue = isRange(value) ? value : { start: null, end: null };
|
|
70
|
+
commitValue({ ...current, [inputMode]: next });
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
commitValue(preserveTime(next, isRange(value) ? null : value));
|
|
74
|
+
},
|
|
75
|
+
[mode, value, inputMode, commitValue],
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
const {
|
|
79
|
+
formattedValue,
|
|
80
|
+
onChangeText,
|
|
81
|
+
onBlur: onInnerBlur,
|
|
82
|
+
onFocus: onInnerFocus,
|
|
83
|
+
} = useDateFieldState({
|
|
84
|
+
value: fieldValue,
|
|
85
|
+
validRange,
|
|
86
|
+
inputMode,
|
|
87
|
+
dateFormat,
|
|
88
|
+
onChange,
|
|
89
|
+
onBlur,
|
|
90
|
+
onFocus,
|
|
91
|
+
isBlurredRef,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
<TextInputWithMask
|
|
96
|
+
{...rest}
|
|
97
|
+
ref={ref}
|
|
98
|
+
disabled={disabled}
|
|
99
|
+
value={formattedValue}
|
|
100
|
+
placeholder={dateFormat}
|
|
101
|
+
keyboardType="number-pad"
|
|
102
|
+
mask={dateFormat}
|
|
103
|
+
onChangeText={onChangeText}
|
|
104
|
+
onBlur={onInnerBlur}
|
|
105
|
+
onFocus={onInnerFocus}
|
|
106
|
+
/>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export default memo(DateField);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { getRegisteredComponentWithFallback } from '../../core';
|
|
2
|
+
import DateFieldDefault from './DateField';
|
|
3
|
+
|
|
4
|
+
export const DateField = getRegisteredComponentWithFallback('DateField', DateFieldDefault);
|
|
5
|
+
|
|
6
|
+
export type { DateFieldInputMode, DateFieldProps } from './DateField';
|
|
@@ -10,34 +10,31 @@ import { useRangeChecker } from '../DatePickerInline/dateUtils';
|
|
|
10
10
|
const formatValue = (value: Date | null | undefined, dateFormat: string) =>
|
|
11
11
|
!isNil(value) ? format(value, dateFormat) || '' : '';
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
// locale,
|
|
15
|
-
value,
|
|
16
|
-
validRange,
|
|
17
|
-
inputMode = 'start',
|
|
18
|
-
onChange,
|
|
19
|
-
dateFormat,
|
|
20
|
-
onBlur: onBlurProp,
|
|
21
|
-
onFocus: onFocusProp,
|
|
22
|
-
isBlurredRef,
|
|
23
|
-
}: {
|
|
24
|
-
onChange?: (d: Date | null) => void;
|
|
25
|
-
onBlur?: (e: BlurEvent) => void;
|
|
26
|
-
onFocus?: (e: FocusEvent) => void;
|
|
27
|
-
// locale: undefined | string;
|
|
13
|
+
type Props = {
|
|
28
14
|
value?: Date | null;
|
|
29
15
|
validRange?: ValidRangeType;
|
|
30
16
|
inputMode: 'start' | 'end';
|
|
31
17
|
dateFormat: string;
|
|
32
18
|
isBlurredRef: RefObject<boolean>;
|
|
33
|
-
|
|
34
|
-
|
|
19
|
+
onChange?: (value: Date | null) => void;
|
|
20
|
+
onBlur?: (e: BlurEvent) => void;
|
|
21
|
+
onFocus?: (e: FocusEvent) => void;
|
|
22
|
+
};
|
|
35
23
|
|
|
24
|
+
export function useDateFieldState({
|
|
25
|
+
value,
|
|
26
|
+
validRange,
|
|
27
|
+
inputMode,
|
|
28
|
+
dateFormat,
|
|
29
|
+
isBlurredRef,
|
|
30
|
+
onChange,
|
|
31
|
+
onBlur: onBlurProp,
|
|
32
|
+
onFocus: onFocusProp,
|
|
33
|
+
}: Props) {
|
|
34
|
+
const { isDisabled, isWithinValidRange } = useRangeChecker(validRange);
|
|
36
35
|
const [formattedValue, setFormattedValue] = useState(() => formatValue(value, dateFormat));
|
|
37
36
|
const formattedValueRef = useLatest(formattedValue);
|
|
38
37
|
|
|
39
|
-
// const [error, setError] = useState<null | string>(null);
|
|
40
|
-
|
|
41
38
|
const onChangeText = useCallback(
|
|
42
39
|
(date: string) => {
|
|
43
40
|
const parsedDate = parse(date, dateFormat, new Date());
|
|
@@ -51,45 +48,18 @@ export default function useDateInput({
|
|
|
51
48
|
);
|
|
52
49
|
|
|
53
50
|
if (!isValid(parsedDate)) {
|
|
54
|
-
// TODO: Translate
|
|
55
|
-
// setError(`Date format must be ${dateFormat}`);
|
|
56
51
|
onChange?.(date ? value ?? null : null);
|
|
57
|
-
|
|
58
52
|
return;
|
|
59
53
|
}
|
|
60
54
|
|
|
61
55
|
const finalDate = inputMode === 'end' ? endOfDay(parsedDate) : parsedDate;
|
|
62
56
|
|
|
63
|
-
if (isDisabled(finalDate)) {
|
|
64
|
-
// TODO: Translate
|
|
65
|
-
// setError('Day is not allowed');
|
|
57
|
+
if (isDisabled(finalDate) || !isWithinValidRange(finalDate)) {
|
|
66
58
|
onChange?.(null);
|
|
67
|
-
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
if (!isWithinValidRange(finalDate)) {
|
|
71
|
-
// TODO: Translate
|
|
72
|
-
// const errors =
|
|
73
|
-
// validStart && validEnd
|
|
74
|
-
// ? [
|
|
75
|
-
// `${`Must be between ${format(validStart, dateFormat)} - ${format(
|
|
76
|
-
// validEnd,
|
|
77
|
-
// dateFormat,
|
|
78
|
-
// )})`}`,
|
|
79
|
-
// ]
|
|
80
|
-
// : [
|
|
81
|
-
// validStart ? `Must be later then ${validStart}` : '',
|
|
82
|
-
// validEnd ? `Must be earlier then ${validEnd}` : '',
|
|
83
|
-
// ];
|
|
84
|
-
|
|
85
|
-
// setError(errors.filter(n => n).join(' '));
|
|
86
|
-
onChange?.(null);
|
|
87
|
-
|
|
88
59
|
return;
|
|
89
60
|
}
|
|
90
61
|
|
|
91
62
|
onChange?.(finalDate);
|
|
92
|
-
// setError(null);
|
|
93
63
|
},
|
|
94
64
|
[
|
|
95
65
|
dateFormat,
|
|
@@ -128,8 +98,6 @@ export default function useDateInput({
|
|
|
128
98
|
}, [value, dateFormat, isBlurredRef]);
|
|
129
99
|
|
|
130
100
|
return {
|
|
131
|
-
onChange,
|
|
132
|
-
// error,
|
|
133
101
|
formattedValue,
|
|
134
102
|
onChangeText,
|
|
135
103
|
onBlur,
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { memo } from 'react';
|
|
2
|
+
|
|
3
|
+
import { getRegisteredComponentWithFallback } from '../../core';
|
|
4
|
+
import {
|
|
5
|
+
DatePickerInline,
|
|
6
|
+
type DatePickerInlineProps,
|
|
7
|
+
type RangeChange,
|
|
8
|
+
type SingleChange,
|
|
9
|
+
} from '../DatePickerInline';
|
|
10
|
+
import { useOptionalDatePickerContext } from './context';
|
|
11
|
+
|
|
12
|
+
export type DateCalendarProps = DatePickerInlineProps;
|
|
13
|
+
|
|
14
|
+
const DateCalendarDefault = memo((props: DateCalendarProps) => {
|
|
15
|
+
const ctx = useOptionalDatePickerContext();
|
|
16
|
+
|
|
17
|
+
const hasExplicitState =
|
|
18
|
+
props.date !== undefined ||
|
|
19
|
+
props.startDate !== undefined ||
|
|
20
|
+
props.endDate !== undefined ||
|
|
21
|
+
props.dates !== undefined ||
|
|
22
|
+
props.onChange !== undefined;
|
|
23
|
+
|
|
24
|
+
const isRange = props.mode === 'range' || ctx?.mode === 'range';
|
|
25
|
+
const effectiveHeaderLayout = props.headerLayout ?? (ctx ? 'docked' : 'inline');
|
|
26
|
+
const showOutsideDays =
|
|
27
|
+
props.showOutsideDays ?? (effectiveHeaderLayout === 'docked' && !isRange);
|
|
28
|
+
|
|
29
|
+
if (!ctx || hasExplicitState) {
|
|
30
|
+
return <DatePickerInline {...props} showOutsideDays={showOutsideDays} />;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const locale = props.locale ?? ctx.locale;
|
|
34
|
+
const validRange = props.validRange ?? ctx.validRange;
|
|
35
|
+
|
|
36
|
+
if (ctx.mode === 'range') {
|
|
37
|
+
const range =
|
|
38
|
+
ctx.draftValue && typeof ctx.draftValue === 'object' && 'start' in ctx.draftValue
|
|
39
|
+
? ctx.draftValue
|
|
40
|
+
: { start: null, end: null };
|
|
41
|
+
const onChange: RangeChange = ({ startDate, endDate }) => {
|
|
42
|
+
ctx.setValue({ start: startDate ?? null, end: endDate ?? null });
|
|
43
|
+
};
|
|
44
|
+
return (
|
|
45
|
+
<DatePickerInline
|
|
46
|
+
{...props}
|
|
47
|
+
mode="range"
|
|
48
|
+
startDate={range.start ?? undefined}
|
|
49
|
+
endDate={range.end ?? undefined}
|
|
50
|
+
onChange={onChange}
|
|
51
|
+
locale={locale}
|
|
52
|
+
validRange={validRange}
|
|
53
|
+
headerLayout={props.headerLayout ?? 'docked'}
|
|
54
|
+
showOutsideDays={showOutsideDays}
|
|
55
|
+
/>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const single =
|
|
60
|
+
ctx.draftValue && typeof ctx.draftValue === 'object' && 'start' in ctx.draftValue
|
|
61
|
+
? null
|
|
62
|
+
: ctx.draftValue;
|
|
63
|
+
const onChange: SingleChange = ({ date }) => {
|
|
64
|
+
ctx.setValue(date ?? null);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<DatePickerInline
|
|
69
|
+
{...props}
|
|
70
|
+
mode="single"
|
|
71
|
+
date={single ?? undefined}
|
|
72
|
+
onChange={onChange}
|
|
73
|
+
locale={locale}
|
|
74
|
+
validRange={validRange}
|
|
75
|
+
headerLayout={props.headerLayout ?? 'docked'}
|
|
76
|
+
showOutsideDays={showOutsideDays}
|
|
77
|
+
/>
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
DateCalendarDefault.displayName = 'DateCalendar';
|
|
82
|
+
|
|
83
|
+
export const DateCalendar = getRegisteredComponentWithFallback('DateCalendar', DateCalendarDefault);
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
import { View } from 'react-native';
|
|
3
|
+
import { StyleSheet } from 'react-native-unistyles';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
getRegisteredComponentStylesWithFallback,
|
|
7
|
+
getRegisteredComponentWithFallback,
|
|
8
|
+
} from '../../core';
|
|
9
|
+
import { Button } from '../Button';
|
|
10
|
+
import { useDatePickerContext } from './context';
|
|
11
|
+
|
|
12
|
+
export type DatePickerActionsProps = {
|
|
13
|
+
cancelLabel?: string;
|
|
14
|
+
confirmLabel?: string;
|
|
15
|
+
onCancel?: () => void;
|
|
16
|
+
onConfirm?: () => void;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function DatePickerActionsDefault({
|
|
20
|
+
cancelLabel = 'Cancel',
|
|
21
|
+
confirmLabel = 'OK',
|
|
22
|
+
onCancel,
|
|
23
|
+
onConfirm,
|
|
24
|
+
}: DatePickerActionsProps) {
|
|
25
|
+
const { draft, commit, cancel } = useDatePickerContext();
|
|
26
|
+
|
|
27
|
+
const handleCancel = useCallback(() => {
|
|
28
|
+
onCancel?.();
|
|
29
|
+
cancel();
|
|
30
|
+
}, [onCancel, cancel]);
|
|
31
|
+
|
|
32
|
+
const handleConfirm = useCallback(() => {
|
|
33
|
+
onConfirm?.();
|
|
34
|
+
commit();
|
|
35
|
+
}, [onConfirm, commit]);
|
|
36
|
+
|
|
37
|
+
if (!draft) return null;
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<View style={datePickerActionsStyles.footer}>
|
|
41
|
+
<Button variant="text" onPress={handleCancel}>
|
|
42
|
+
<Button.Text>{cancelLabel}</Button.Text>
|
|
43
|
+
</Button>
|
|
44
|
+
<Button variant="text" onPress={handleConfirm}>
|
|
45
|
+
<Button.Text>{confirmLabel}</Button.Text>
|
|
46
|
+
</Button>
|
|
47
|
+
</View>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export default DatePickerActionsDefault;
|
|
52
|
+
|
|
53
|
+
export const DatePickerActions = getRegisteredComponentWithFallback(
|
|
54
|
+
'DatePickerActions',
|
|
55
|
+
DatePickerActionsDefault,
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
const datePickerActionsStylesDefault = StyleSheet.create(theme => ({
|
|
59
|
+
footer: {
|
|
60
|
+
flexDirection: 'row',
|
|
61
|
+
alignItems: 'center',
|
|
62
|
+
justifyContent: 'flex-end',
|
|
63
|
+
columnGap: theme.spacings['2'],
|
|
64
|
+
paddingHorizontal: theme.spacings['4'],
|
|
65
|
+
paddingTop: theme.spacings['0'],
|
|
66
|
+
paddingBottom: theme.spacings['2'],
|
|
67
|
+
},
|
|
68
|
+
}));
|
|
69
|
+
|
|
70
|
+
const datePickerActionsStyles = getRegisteredComponentStylesWithFallback(
|
|
71
|
+
'DatePickerActions',
|
|
72
|
+
datePickerActionsStylesDefault,
|
|
73
|
+
);
|
|
@@ -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
|
+
);
|