react-native-molecules 0.5.0-beta.16 → 0.5.0-beta.18
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/Menu/Menu.tsx +3 -18
- 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 +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/Popover/Popover.native.tsx +0 -185
- 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
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { getRegisteredComponentWithFallback } from '../../core';
|
|
2
|
-
import DateTimePickerDefault from './DateTimePicker';
|
|
3
|
-
|
|
4
|
-
export const DateTimePicker = getRegisteredComponentWithFallback(
|
|
5
|
-
'DateTimePicker',
|
|
6
|
-
DateTimePickerDefault,
|
|
7
|
-
);
|
|
8
|
-
|
|
9
|
-
export type { Props as DateTimePickerProps } from './DateTimePicker';
|
|
10
|
-
export { dateTimePickerStyles } from './utils';
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { StyleSheet } from 'react-native-unistyles';
|
|
2
|
-
|
|
3
|
-
import { getRegisteredComponentStylesWithFallback } from '../../core';
|
|
4
|
-
|
|
5
|
-
const dateTimePickerStylesDefault = StyleSheet.create({
|
|
6
|
-
container: {},
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
export const dateTimePickerStyles = getRegisteredComponentStylesWithFallback(
|
|
10
|
-
'DateTimePicker',
|
|
11
|
-
dateTimePickerStylesDefault,
|
|
12
|
-
);
|
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
import { Fragment, memo, useCallback, useEffect, useLayoutEffect, useRef } from 'react';
|
|
2
|
-
import { AppState, Dimensions, Platform, Pressable, StyleSheet, View } from 'react-native';
|
|
3
|
-
import { ScopedTheme, UnistylesRuntime } from 'react-native-unistyles';
|
|
4
|
-
|
|
5
|
-
import { Portal } from '../Portal';
|
|
6
|
-
import {
|
|
7
|
-
DEFAULT_ARROW_SIZE,
|
|
8
|
-
popoverDefaultStyles,
|
|
9
|
-
type PopoverProps,
|
|
10
|
-
useArrowStyles,
|
|
11
|
-
usePopover,
|
|
12
|
-
} from './common';
|
|
13
|
-
|
|
14
|
-
const Popover = ({
|
|
15
|
-
triggerRef,
|
|
16
|
-
children,
|
|
17
|
-
isOpen,
|
|
18
|
-
onClose,
|
|
19
|
-
position = 'bottom',
|
|
20
|
-
align = 'center',
|
|
21
|
-
style,
|
|
22
|
-
showArrow = false,
|
|
23
|
-
arrowSize = DEFAULT_ARROW_SIZE,
|
|
24
|
-
inverted = false,
|
|
25
|
-
triggerDimensions,
|
|
26
|
-
offset = 8,
|
|
27
|
-
...rest
|
|
28
|
-
}: PopoverProps) => {
|
|
29
|
-
const {
|
|
30
|
-
popoverLayoutRef,
|
|
31
|
-
targetLayoutRef,
|
|
32
|
-
actualPositionRef,
|
|
33
|
-
calculatedPosition,
|
|
34
|
-
calculateAndSetPosition,
|
|
35
|
-
handlePopoverLayout,
|
|
36
|
-
} = usePopover({
|
|
37
|
-
isOpen,
|
|
38
|
-
position,
|
|
39
|
-
align,
|
|
40
|
-
showArrow,
|
|
41
|
-
arrowSize,
|
|
42
|
-
offset,
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
const popoverRef = useRef<View>(null);
|
|
46
|
-
|
|
47
|
-
const measureTarget = useCallback(() => {
|
|
48
|
-
if (triggerRef.current) {
|
|
49
|
-
triggerRef.current.measure(
|
|
50
|
-
(
|
|
51
|
-
_fx: number,
|
|
52
|
-
_fy: number,
|
|
53
|
-
width: number,
|
|
54
|
-
height: number,
|
|
55
|
-
px: number,
|
|
56
|
-
py: number,
|
|
57
|
-
) => {
|
|
58
|
-
if (width !== 0 || height !== 0) {
|
|
59
|
-
const newLayout = { x: px, y: py, width, height };
|
|
60
|
-
const changed =
|
|
61
|
-
!targetLayoutRef.current ||
|
|
62
|
-
targetLayoutRef.current.x !== newLayout.x ||
|
|
63
|
-
targetLayoutRef.current.y !== newLayout.y ||
|
|
64
|
-
targetLayoutRef.current.width !== newLayout.width ||
|
|
65
|
-
targetLayoutRef.current.height !== newLayout.height;
|
|
66
|
-
|
|
67
|
-
if (changed) {
|
|
68
|
-
targetLayoutRef.current = newLayout;
|
|
69
|
-
calculateAndSetPosition();
|
|
70
|
-
}
|
|
71
|
-
} else {
|
|
72
|
-
targetLayoutRef.current = null;
|
|
73
|
-
calculateAndSetPosition();
|
|
74
|
-
}
|
|
75
|
-
},
|
|
76
|
-
() => {
|
|
77
|
-
console.error('Failed to measure target element for Popover.');
|
|
78
|
-
targetLayoutRef.current = null;
|
|
79
|
-
calculateAndSetPosition();
|
|
80
|
-
},
|
|
81
|
-
);
|
|
82
|
-
} else {
|
|
83
|
-
targetLayoutRef.current = null;
|
|
84
|
-
calculateAndSetPosition();
|
|
85
|
-
}
|
|
86
|
-
}, [triggerRef, calculateAndSetPosition, targetLayoutRef]);
|
|
87
|
-
|
|
88
|
-
useLayoutEffect(() => {
|
|
89
|
-
if (isOpen) {
|
|
90
|
-
measureTarget();
|
|
91
|
-
}
|
|
92
|
-
}, [isOpen, measureTarget, triggerDimensions]);
|
|
93
|
-
|
|
94
|
-
useEffect(() => {
|
|
95
|
-
if (!isOpen) return;
|
|
96
|
-
const subscription = Dimensions.addEventListener('change', measureTarget);
|
|
97
|
-
return () => {
|
|
98
|
-
if (typeof subscription?.remove === 'function') {
|
|
99
|
-
subscription.remove();
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
}, [isOpen, measureTarget]);
|
|
103
|
-
|
|
104
|
-
useEffect(() => {
|
|
105
|
-
if (!isOpen || Platform.OS === 'web') return;
|
|
106
|
-
const handleAppStateChange = (nextAppState: string) => {
|
|
107
|
-
if (nextAppState === 'active') {
|
|
108
|
-
setTimeout(measureTarget, 50);
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
const subscription = AppState.addEventListener('change', handleAppStateChange);
|
|
112
|
-
return () => {
|
|
113
|
-
if (typeof subscription?.remove === 'function') {
|
|
114
|
-
subscription.remove();
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
}, [isOpen, measureTarget]);
|
|
118
|
-
|
|
119
|
-
const arrowStyles = useArrowStyles({
|
|
120
|
-
showArrow,
|
|
121
|
-
arrowSize,
|
|
122
|
-
style,
|
|
123
|
-
calculatedPosition,
|
|
124
|
-
targetLayoutRef,
|
|
125
|
-
popoverLayoutRef,
|
|
126
|
-
actualPositionRef,
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
const popoverStyle = calculatedPosition ?? popoverDefaultStyles;
|
|
130
|
-
const Wrapper = inverted ? ScopedTheme : Fragment;
|
|
131
|
-
|
|
132
|
-
if (!isOpen && popoverStyle.opacity === 0) {
|
|
133
|
-
return null;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const handleOutsidePress = () => {
|
|
137
|
-
if (isOpen && onClose) {
|
|
138
|
-
onClose();
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
return (
|
|
143
|
-
<Portal>
|
|
144
|
-
<Wrapper
|
|
145
|
-
{...(inverted
|
|
146
|
-
? { name: UnistylesRuntime.themeName === 'dark' ? 'light' : 'dark' }
|
|
147
|
-
: ({} as { name: 'light' }))}>
|
|
148
|
-
<Pressable onPress={handleOutsidePress} style={styles.overlay} />
|
|
149
|
-
|
|
150
|
-
<View
|
|
151
|
-
ref={popoverRef}
|
|
152
|
-
onLayout={handlePopoverLayout}
|
|
153
|
-
style={[styles.popoverContainer, style, popoverStyle]}
|
|
154
|
-
{...rest}>
|
|
155
|
-
{children}
|
|
156
|
-
{showArrow && popoverStyle.opacity === 1 && <View style={arrowStyles} />}
|
|
157
|
-
</View>
|
|
158
|
-
</Wrapper>
|
|
159
|
-
</Portal>
|
|
160
|
-
);
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
const styles = StyleSheet.create({
|
|
164
|
-
overlay: {
|
|
165
|
-
position: 'absolute',
|
|
166
|
-
top: 0,
|
|
167
|
-
bottom: 0,
|
|
168
|
-
left: 0,
|
|
169
|
-
right: 0,
|
|
170
|
-
backgroundColor: 'transparent',
|
|
171
|
-
},
|
|
172
|
-
popoverContainer: {
|
|
173
|
-
...popoverDefaultStyles,
|
|
174
|
-
backgroundColor: 'white',
|
|
175
|
-
borderRadius: 4,
|
|
176
|
-
shadowColor: 'rgba(0, 0, 0, 1)',
|
|
177
|
-
shadowOffset: { width: 0, height: 2 },
|
|
178
|
-
shadowOpacity: 0.3,
|
|
179
|
-
shadowRadius: 3.84,
|
|
180
|
-
elevation: 5,
|
|
181
|
-
zIndex: 100,
|
|
182
|
-
},
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
export default memo(Popover);
|
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
import { forwardRef, memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
-
import type { BlurEvent, FocusEvent } from 'react-native';
|
|
3
|
-
|
|
4
|
-
import { useToggle } from '../../hooks';
|
|
5
|
-
import { IconButton, type IconButtonProps } from '../IconButton';
|
|
6
|
-
import { TextInput, type TextInputProps } from '../TextInput';
|
|
7
|
-
import type { TimePickerModalProps } from '../TimePickerModal';
|
|
8
|
-
import { TimePickerModal } from '../TimePickerModal/TimePickerModal';
|
|
9
|
-
import { sanitizeTime } from './sanitizeTime';
|
|
10
|
-
import { getFormattedTime, getOutputTime, styles, timeFormat } from './utils';
|
|
11
|
-
|
|
12
|
-
export type Props = TextInputProps & {
|
|
13
|
-
time: string;
|
|
14
|
-
onTimeChange: (time: string) => void;
|
|
15
|
-
is24Hour?: boolean;
|
|
16
|
-
withModal?: boolean;
|
|
17
|
-
modalProps?: Omit<TimePickerModalProps, 'time' | 'onConfirm' | 'isOpen' | 'onClose' | 'ref'>;
|
|
18
|
-
iconButtonProps?: Omit<Partial<IconButtonProps>, 'onPress' | 'ref'>;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const TimePickerField = (
|
|
22
|
-
{
|
|
23
|
-
time,
|
|
24
|
-
onTimeChange: onTimeChangeProp,
|
|
25
|
-
is24Hour = false,
|
|
26
|
-
withModal = true,
|
|
27
|
-
style,
|
|
28
|
-
onBlur: onBlurProp,
|
|
29
|
-
modalProps,
|
|
30
|
-
iconButtonProps,
|
|
31
|
-
disabled,
|
|
32
|
-
onFocus: onFocusProp,
|
|
33
|
-
children,
|
|
34
|
-
...rest
|
|
35
|
-
}: Props,
|
|
36
|
-
ref: any,
|
|
37
|
-
) => {
|
|
38
|
-
const componentStyles = useMemo(() => [styles.root, style], [style]);
|
|
39
|
-
|
|
40
|
-
const [timeString, setTimeString] = useState(() => getFormattedTime({ time, is24Hour }));
|
|
41
|
-
const isBlurredRef = useRef(true);
|
|
42
|
-
|
|
43
|
-
const currentTimeFormat = useMemo(() => timeFormat[!is24Hour ? '12' : '24'], [is24Hour]);
|
|
44
|
-
|
|
45
|
-
const { state: isOpen, handleOpen: onOpenModal, handleClose: onCloseModal } = useToggle(false);
|
|
46
|
-
|
|
47
|
-
const onChangeText = useCallback(
|
|
48
|
-
(_text: string) => {
|
|
49
|
-
const text = sanitizeTime(_text, is24Hour);
|
|
50
|
-
setTimeString(_text);
|
|
51
|
-
|
|
52
|
-
if (disabled || !text) return;
|
|
53
|
-
|
|
54
|
-
const outputTime = getOutputTime({ time: text || time, is24Hour });
|
|
55
|
-
|
|
56
|
-
onTimeChangeProp(outputTime);
|
|
57
|
-
},
|
|
58
|
-
[disabled, is24Hour, onTimeChangeProp, time],
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
const onConfirmTime = useCallback(
|
|
62
|
-
(newTime: string) => {
|
|
63
|
-
onCloseModal();
|
|
64
|
-
|
|
65
|
-
onTimeChangeProp(newTime);
|
|
66
|
-
},
|
|
67
|
-
[onTimeChangeProp, onCloseModal],
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
const onBlur = useCallback(
|
|
71
|
-
(e: BlurEvent) => {
|
|
72
|
-
isBlurredRef.current = true;
|
|
73
|
-
onBlurProp?.(e);
|
|
74
|
-
|
|
75
|
-
if (disabled) return;
|
|
76
|
-
|
|
77
|
-
// const outputTime = getOutputTime({ time: timeString || time, is24Hour });
|
|
78
|
-
// onTimeChangeProp(outputTime);
|
|
79
|
-
|
|
80
|
-
// if (time === outputTime) {
|
|
81
|
-
setTimeString(sanitizeTime(getFormattedTime({ time, is24Hour }), is24Hour));
|
|
82
|
-
// }
|
|
83
|
-
},
|
|
84
|
-
[disabled, is24Hour, onBlurProp, time],
|
|
85
|
-
);
|
|
86
|
-
|
|
87
|
-
const onFocus = useCallback(
|
|
88
|
-
(e: FocusEvent) => {
|
|
89
|
-
isBlurredRef.current = false;
|
|
90
|
-
|
|
91
|
-
onFocusProp?.(e);
|
|
92
|
-
},
|
|
93
|
-
[onFocusProp],
|
|
94
|
-
);
|
|
95
|
-
|
|
96
|
-
const rightElement = useMemo(() => {
|
|
97
|
-
if (!withModal) return null;
|
|
98
|
-
|
|
99
|
-
return (
|
|
100
|
-
<>
|
|
101
|
-
<IconButton
|
|
102
|
-
name="clock-outline"
|
|
103
|
-
onPress={onOpenModal}
|
|
104
|
-
disabled={disabled}
|
|
105
|
-
{...iconButtonProps}
|
|
106
|
-
/>
|
|
107
|
-
<TimePickerModal
|
|
108
|
-
{...modalProps}
|
|
109
|
-
time={getFormattedTime({ time: time, is24Hour: true })}
|
|
110
|
-
isOpen={isOpen}
|
|
111
|
-
onClose={onCloseModal}
|
|
112
|
-
onConfirm={onConfirmTime}
|
|
113
|
-
is24Hour={is24Hour}
|
|
114
|
-
/>
|
|
115
|
-
</>
|
|
116
|
-
);
|
|
117
|
-
}, [
|
|
118
|
-
disabled,
|
|
119
|
-
iconButtonProps,
|
|
120
|
-
is24Hour,
|
|
121
|
-
isOpen,
|
|
122
|
-
modalProps,
|
|
123
|
-
onCloseModal,
|
|
124
|
-
onConfirmTime,
|
|
125
|
-
onOpenModal,
|
|
126
|
-
time,
|
|
127
|
-
withModal,
|
|
128
|
-
]);
|
|
129
|
-
|
|
130
|
-
// only change masked string when the input is not focus so as to not interrup the user
|
|
131
|
-
useEffect(() => {
|
|
132
|
-
if (!isBlurredRef.current) return;
|
|
133
|
-
|
|
134
|
-
setTimeString(getFormattedTime({ time, is24Hour }));
|
|
135
|
-
}, [is24Hour, time]);
|
|
136
|
-
|
|
137
|
-
return (
|
|
138
|
-
<TextInput
|
|
139
|
-
ref={ref}
|
|
140
|
-
placeholder={currentTimeFormat.format}
|
|
141
|
-
{...rest}
|
|
142
|
-
disabled={disabled}
|
|
143
|
-
value={timeString}
|
|
144
|
-
onFocus={onFocus}
|
|
145
|
-
onChangeText={onChangeText}
|
|
146
|
-
style={componentStyles}
|
|
147
|
-
onBlur={onBlur}>
|
|
148
|
-
<TextInput.Right>{rightElement}</TextInput.Right>
|
|
149
|
-
{children}
|
|
150
|
-
</TextInput>
|
|
151
|
-
);
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
export default memo(forwardRef(TimePickerField));
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { getRegisteredComponentWithFallback } from '../../core';
|
|
2
|
-
import TimePickerFieldDefault from './TimePickerField';
|
|
3
|
-
|
|
4
|
-
export const TimePickerField = getRegisteredComponentWithFallback(
|
|
5
|
-
'TimePickerField',
|
|
6
|
-
TimePickerFieldDefault,
|
|
7
|
-
);
|
|
8
|
-
|
|
9
|
-
export { type Props as TimePickerFieldProps } from './TimePickerField';
|
|
10
|
-
export { sanitizeTimeString, styles } from './utils';
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { format, parse, set } from 'date-fns';
|
|
2
|
-
import { StyleSheet } from 'react-native-unistyles';
|
|
3
|
-
|
|
4
|
-
export declare type MaskItem = string | RegExp | [RegExp];
|
|
5
|
-
export declare type MaskArray = Array<MaskItem>;
|
|
6
|
-
export declare type Mask = MaskArray | ((value?: string) => MaskArray);
|
|
7
|
-
|
|
8
|
-
import { getRegisteredComponentStylesWithFallback } from '../../core';
|
|
9
|
-
|
|
10
|
-
const timepickerFieldStylesDefault = StyleSheet.create({
|
|
11
|
-
root: {},
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
export const styles = getRegisteredComponentStylesWithFallback(
|
|
15
|
-
'TimePickerField',
|
|
16
|
-
timepickerFieldStylesDefault,
|
|
17
|
-
);
|
|
18
|
-
|
|
19
|
-
export const timeMask24Hour: Mask = (text: string = '') => {
|
|
20
|
-
const cleanTime = text.replace(/\D+/g, '');
|
|
21
|
-
|
|
22
|
-
const hourFirstDigit = /[012]/; // only 0,1 or 2
|
|
23
|
-
let hourSecondDigit = /\d/; // any number
|
|
24
|
-
|
|
25
|
-
if (cleanTime.charAt(0) === '2') {
|
|
26
|
-
hourSecondDigit = /[0123]/; // only 0,1,2 or 3
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const minuteFirstDigit = /[012345]/; // only 0,1,2,3,4 or 5
|
|
30
|
-
const minuteSecondDigit = /\d/; // any number
|
|
31
|
-
|
|
32
|
-
return [hourFirstDigit, hourSecondDigit, ':', minuteFirstDigit, minuteSecondDigit];
|
|
33
|
-
};
|
|
34
|
-
export const timeMask12Hour: Mask = (text: string = '') => {
|
|
35
|
-
const cleanTime = text.replace(/\D+/g, '');
|
|
36
|
-
|
|
37
|
-
const hourFirstDigit = /[01]/; // only 0,1 or 2
|
|
38
|
-
let hourSecondDigit = /\d/; // any number
|
|
39
|
-
|
|
40
|
-
if (cleanTime.charAt(0) === '1') {
|
|
41
|
-
hourSecondDigit = /[012]/; // only 0,1,2 or 3
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const minuteFirstDigit = /[012345]/; // only 0,1,2,3,4 or 5
|
|
45
|
-
const minuteSecondDigit = /\d/; // any number
|
|
46
|
-
|
|
47
|
-
return [hourFirstDigit, hourSecondDigit, ':', minuteFirstDigit, minuteSecondDigit, /[ap]/, 'm'];
|
|
48
|
-
};
|
|
49
|
-
export const timeFormat = {
|
|
50
|
-
'24': {
|
|
51
|
-
format: 'HH:mm',
|
|
52
|
-
mask: timeMask24Hour,
|
|
53
|
-
},
|
|
54
|
-
'12': {
|
|
55
|
-
format: 'hh:mmaaa',
|
|
56
|
-
mask: timeMask12Hour,
|
|
57
|
-
},
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
const referenceDate = new Date('2022-01-01T00:00:00.000Z');
|
|
61
|
-
|
|
62
|
-
export const sanitizeTimeString = (time: string): string => time.replace(/[^\d:]/g, '');
|
|
63
|
-
|
|
64
|
-
export const getFormattedTime = ({ time, is24Hour }: { time: string; is24Hour: boolean }) => {
|
|
65
|
-
if (!time) {
|
|
66
|
-
return '';
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const [hour = '0', minute = '0'] = sanitizeTimeString(time).split(':');
|
|
70
|
-
|
|
71
|
-
const newHour = hour.padStart(2, '0');
|
|
72
|
-
const newMinute = minute.padStart(2, '0');
|
|
73
|
-
|
|
74
|
-
return format(
|
|
75
|
-
set(referenceDate, { hours: +newHour, minutes: +newMinute }),
|
|
76
|
-
timeFormat[is24Hour ? '24' : '12'].format,
|
|
77
|
-
);
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
export const getOutputTime = ({ time, is24Hour }: { time: string; is24Hour: boolean }) => {
|
|
81
|
-
if (!time) return '';
|
|
82
|
-
|
|
83
|
-
const formattedTime = sanitizeTimeString(getFormattedTime({ time, is24Hour }));
|
|
84
|
-
const isPM = time.replace(/[\d:]/g, '').includes('p');
|
|
85
|
-
|
|
86
|
-
return format(
|
|
87
|
-
parse(
|
|
88
|
-
formattedTime + (is24Hour ? '' : isPM ? 'pm' : 'am'),
|
|
89
|
-
timeFormat[is24Hour ? '24' : '12'].format,
|
|
90
|
-
referenceDate,
|
|
91
|
-
),
|
|
92
|
-
'HH:mm',
|
|
93
|
-
);
|
|
94
|
-
};
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import { memo, useCallback, useEffect, useMemo, useState } from 'react';
|
|
2
|
-
import { KeyboardAvoidingView, Platform, View } from 'react-native';
|
|
3
|
-
|
|
4
|
-
import { format } from '../../utils/date-fns';
|
|
5
|
-
import { Button } from '../Button';
|
|
6
|
-
import { IconButton } from '../IconButton';
|
|
7
|
-
import { Modal, type ModalProps } from '../Modal';
|
|
8
|
-
import { Portal } from '../Portal';
|
|
9
|
-
import { Text } from '../Text';
|
|
10
|
-
import TimePicker from '../TimePicker/TimePicker';
|
|
11
|
-
import {
|
|
12
|
-
clockTypes,
|
|
13
|
-
getTimeInputTypeIcon,
|
|
14
|
-
inputTypes,
|
|
15
|
-
type PossibleClockTypes,
|
|
16
|
-
type PossibleInputTypes,
|
|
17
|
-
reverseInputTypes,
|
|
18
|
-
} from '../TimePicker/timeUtils';
|
|
19
|
-
import { styles } from './utils';
|
|
20
|
-
|
|
21
|
-
export type Props = Omit<ModalProps, 'children'> & {
|
|
22
|
-
is24Hour?: boolean;
|
|
23
|
-
label?: string;
|
|
24
|
-
uppercase?: boolean;
|
|
25
|
-
cancelLabel?: string;
|
|
26
|
-
confirmLabel?: string;
|
|
27
|
-
time?: string;
|
|
28
|
-
onConfirm: (time: string) => any;
|
|
29
|
-
keyboardIcon?: string;
|
|
30
|
-
clockIcon?: string;
|
|
31
|
-
isLandscape?: boolean;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export function TimePickerModal({
|
|
35
|
-
isOpen,
|
|
36
|
-
onClose,
|
|
37
|
-
onConfirm: onConfirmProp,
|
|
38
|
-
time: timeProp,
|
|
39
|
-
label = 'Select time',
|
|
40
|
-
uppercase = false,
|
|
41
|
-
cancelLabel = 'Cancel',
|
|
42
|
-
confirmLabel = 'Ok',
|
|
43
|
-
is24Hour,
|
|
44
|
-
keyboardIcon = 'keyboard-outline',
|
|
45
|
-
clockIcon = 'clock-outline',
|
|
46
|
-
isLandscape = false,
|
|
47
|
-
style,
|
|
48
|
-
...rest
|
|
49
|
-
}: Props) {
|
|
50
|
-
const [inputType, setInputType] = useState<PossibleInputTypes>(inputTypes.picker);
|
|
51
|
-
const [focused, setFocused] = useState<PossibleClockTypes>(clockTypes.hours);
|
|
52
|
-
const [time, setTime] = useState(() => timeProp || format(new Date(), 'HH:mm'));
|
|
53
|
-
|
|
54
|
-
const modelStyle = useMemo(() => [styles.modalContent, style], [style]);
|
|
55
|
-
|
|
56
|
-
const onFocusInput = useCallback((type: PossibleClockTypes) => setFocused(type), []);
|
|
57
|
-
|
|
58
|
-
const onChange = useCallback(
|
|
59
|
-
(params: { focused?: PossibleClockTypes | undefined; time: string }) => {
|
|
60
|
-
if (params.focused) {
|
|
61
|
-
setFocused(params.focused);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
setTime(params.time);
|
|
65
|
-
},
|
|
66
|
-
[setFocused, setTime],
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
const onConfirm = useCallback(() => onConfirmProp(time), [onConfirmProp, time]);
|
|
70
|
-
|
|
71
|
-
useEffect(() => {
|
|
72
|
-
setTime(timeProp || format(new Date(), 'HH:mm'));
|
|
73
|
-
}, [timeProp]);
|
|
74
|
-
|
|
75
|
-
return (
|
|
76
|
-
<Portal>
|
|
77
|
-
<Modal {...rest} isOpen={isOpen} style={modelStyle} onClose={onClose}>
|
|
78
|
-
<KeyboardAvoidingView
|
|
79
|
-
style={styles.keyboardView}
|
|
80
|
-
behavior={Platform.OS === 'ios' ? 'padding' : undefined}>
|
|
81
|
-
<View style={styles.labelContainer}>
|
|
82
|
-
<Text style={styles.label}>{uppercase ? label.toUpperCase() : label}</Text>
|
|
83
|
-
</View>
|
|
84
|
-
<View style={styles.timePickerContainer}>
|
|
85
|
-
<TimePicker
|
|
86
|
-
is24Hour={is24Hour}
|
|
87
|
-
inputType={inputType}
|
|
88
|
-
focused={focused}
|
|
89
|
-
time={time}
|
|
90
|
-
onTimeChange={onChange}
|
|
91
|
-
onFocusInput={onFocusInput}
|
|
92
|
-
isLandscape={isLandscape}
|
|
93
|
-
/>
|
|
94
|
-
</View>
|
|
95
|
-
<View style={styles.footer}>
|
|
96
|
-
<IconButton
|
|
97
|
-
name={getTimeInputTypeIcon(inputType, {
|
|
98
|
-
keyboard: keyboardIcon,
|
|
99
|
-
picker: clockIcon,
|
|
100
|
-
})}
|
|
101
|
-
onPress={() => setInputType(reverseInputTypes[inputType])}
|
|
102
|
-
style={styles.inputTypeToggle}
|
|
103
|
-
accessibilityLabel="toggle keyboard"
|
|
104
|
-
/>
|
|
105
|
-
<View style={styles.fill} />
|
|
106
|
-
<Button onPress={onClose}>
|
|
107
|
-
<Button.Text>{cancelLabel}</Button.Text>
|
|
108
|
-
</Button>
|
|
109
|
-
<Button onPress={onConfirm}>
|
|
110
|
-
<Button.Text>{confirmLabel}</Button.Text>
|
|
111
|
-
</Button>
|
|
112
|
-
</View>
|
|
113
|
-
</KeyboardAvoidingView>
|
|
114
|
-
</Modal>
|
|
115
|
-
</Portal>
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export default memo(TimePickerModal);
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { getRegisteredComponentWithFallback } from '../../core';
|
|
2
|
-
import TimePickerModalDefault from './TimePickerModal';
|
|
3
|
-
|
|
4
|
-
export const TimePickerModal = getRegisteredComponentWithFallback(
|
|
5
|
-
'TimePickerModal',
|
|
6
|
-
TimePickerModalDefault,
|
|
7
|
-
);
|
|
8
|
-
|
|
9
|
-
export type { Props as TimePickerModalProps } from './TimePickerModal';
|
|
10
|
-
export { styles } from './utils';
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { StyleSheet } from 'react-native-unistyles';
|
|
2
|
-
|
|
3
|
-
import { getRegisteredComponentStylesWithFallback } from '../../core';
|
|
4
|
-
|
|
5
|
-
const timepickerModalStylesDefault = StyleSheet.create(theme => ({
|
|
6
|
-
keyboardView: {
|
|
7
|
-
justifyContent: 'center',
|
|
8
|
-
alignItems: 'center',
|
|
9
|
-
flex: 1,
|
|
10
|
-
},
|
|
11
|
-
modalContent: {
|
|
12
|
-
minWidth: 287,
|
|
13
|
-
width: undefined,
|
|
14
|
-
maxWidth: undefined,
|
|
15
|
-
flex: undefined,
|
|
16
|
-
borderRadius: theme.shapes.corner.extraLarge,
|
|
17
|
-
},
|
|
18
|
-
labelContainer: {
|
|
19
|
-
height: 28,
|
|
20
|
-
justifyContent: 'flex-end',
|
|
21
|
-
paddingLeft: theme.spacings['6'],
|
|
22
|
-
paddingRight: theme.spacings['6'],
|
|
23
|
-
alignSelf: 'flex-start',
|
|
24
|
-
},
|
|
25
|
-
label: {
|
|
26
|
-
letterSpacing: 1,
|
|
27
|
-
fontSize: theme.typescale.labelLarge.fontSize,
|
|
28
|
-
color: theme.colors.onSurface,
|
|
29
|
-
fontWeight: theme.typescale.labelLarge.fontWeight,
|
|
30
|
-
},
|
|
31
|
-
timePickerContainer: {
|
|
32
|
-
padding: theme.spacings['6'],
|
|
33
|
-
},
|
|
34
|
-
footer: {
|
|
35
|
-
flexDirection: 'row',
|
|
36
|
-
alignItems: 'center',
|
|
37
|
-
padding: theme.spacings['2'],
|
|
38
|
-
width: '100%',
|
|
39
|
-
},
|
|
40
|
-
inputTypeToggle: { margin: theme.spacings['1'] },
|
|
41
|
-
fill: { flex: 1 },
|
|
42
|
-
}));
|
|
43
|
-
|
|
44
|
-
export const styles = getRegisteredComponentStylesWithFallback(
|
|
45
|
-
'TimePickerModal',
|
|
46
|
-
timepickerModalStylesDefault,
|
|
47
|
-
);
|