/
DatePickerModal.tsx
201 lines (184 loc) · 5.1 KB
/
DatePickerModal.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import * as React from 'react'
import {
Modal,
StyleSheet,
TouchableWithoutFeedback,
useWindowDimensions,
View,
} from 'react-native'
import Calendar, {
BaseCalendarProps,
CalendarDate,
ModeType,
RangeChange,
SingleChange,
} from './Calendar'
import AnimatedCrossView from './AnimatedCrossView'
import DatePickerModalHeader, { HeaderPickProps } from './DatePickerModalHeader'
import CalendarEdit from './CalendarEdit'
import { useTheme } from 'react-native-paper'
interface DatePickerModalProps extends HeaderPickProps, BaseCalendarProps {
mode: ModeType
visible: boolean
onDismiss: () => any
inputFormat?: string
animationType?: 'slide' | 'fade' | 'none'
}
export type LocalState = {
startDate: CalendarDate
endDate: CalendarDate
date: CalendarDate
}
interface DatePickerModalRangeProps
extends BaseCalendarProps,
DatePickerModalProps {
mode: 'range'
startDate: Date | null | undefined
endDate: Date | null | undefined
onChange?: RangeChange
onConfirm: RangeChange
}
interface DatePickerModalSingleProps
extends BaseCalendarProps,
DatePickerModalProps {
mode: 'single'
date?: Date | null | undefined
onChange?: SingleChange
onConfirm: SingleChange
}
export function DatePickerModal(
props: DatePickerModalRangeProps | DatePickerModalSingleProps
) {
const theme = useTheme()
const dimensions = useWindowDimensions()
const { visible, onDismiss, mode, onChange, onConfirm, animationType } = props
const anyProps = props as any
// use local state to add only onConfirm state changes
const [state, setState] = React.useState<LocalState>({
date: anyProps.date,
startDate: anyProps.startDate,
endDate: anyProps.endDate,
})
// update local state if changed from outside or if modal is opened
React.useEffect(() => {
setState({
date: anyProps.date,
startDate: anyProps.startDate,
endDate: anyProps.endDate,
})
}, [anyProps.date, anyProps.startDate, anyProps.endDate])
const [collapsed, setCollapsed] = React.useState<boolean>(true)
const onInnerChange = React.useCallback(
(params) => {
onChange && onChange(params)
setState(params)
},
[onChange, setState]
)
const onInnerConfirm = React.useCallback(() => {
if (mode === 'single') {
onConfirm({ date: state.date } as any)
}
if (mode === 'range') {
onConfirm({ startDate: state.startDate, endDate: state.endDate } as any)
}
}, [state, mode, onConfirm])
const onToggleCollapse = React.useCallback(() => {
setCollapsed((prev) => !prev)
}, [setCollapsed])
return (
<Modal
animationType={animationType}
transparent={true}
visible={visible}
onRequestClose={onDismiss}
presentationStyle="overFullScreen"
//@ts-ignore
statusBarTranslucent={true}
>
<>
<TouchableWithoutFeedback onPress={onDismiss}>
<View
style={[
StyleSheet.absoluteFill,
styles.modalBackground,
{ backgroundColor: theme.colors.backdrop },
]}
/>
</TouchableWithoutFeedback>
<View
style={[StyleSheet.absoluteFill, styles.modalRoot]}
pointerEvents="box-none"
>
<View
style={[
styles.modalContent,
{ backgroundColor: theme.colors.surface },
dimensions.width > 650 ? styles.modalContentBig : null,
]}
>
<DatePickerModalHeader
state={state}
mode={mode}
onSave={onInnerConfirm}
onDismiss={onDismiss}
collapsed={collapsed}
onToggle={onToggleCollapse}
saveLabel={props.saveLabel}
headerSeparator={props.headerSeparator}
label={props.label}
startLabel={props.startLabel}
endLabel={props.endLabel}
/>
<AnimatedCrossView
visible={visible}
collapsed={collapsed}
calendar={
<Calendar
mode={mode}
startDate={state.startDate}
endDate={state.endDate}
date={state.date}
onChange={onInnerChange}
scrollMode={props.scrollMode}
/>
}
calendarEdit={
<CalendarEdit
mode={mode}
state={state}
label={props.label}
startLabel={props.startLabel}
endLabel={props.endLabel}
collapsed={collapsed}
onChange={onInnerChange}
/>
}
/>
</View>
</View>
</>
</Modal>
)
}
const styles = StyleSheet.create({
modalRoot: {
justifyContent: 'flex-start',
alignItems: 'stretch',
flex: 1,
},
modalBackground: {
flex: 1,
},
modalContent: {
flex: 1,
},
modalContentBig: {
maxWidth: 600,
maxHeight: 800,
borderRadius: 10,
width: '100%',
overflow: 'hidden',
},
})
export default React.memo(DatePickerModal)