-
-
Notifications
You must be signed in to change notification settings - Fork 416
Expand file tree
/
Copy pathDatePickerAndroid.js
More file actions
107 lines (92 loc) · 3.29 KB
/
Copy pathDatePickerAndroid.js
File metadata and controls
107 lines (92 loc) · 3.29 KB
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
import React, { useCallback, useEffect, useRef } from 'react'
import { NativeEventEmitter } from 'react-native'
import { useModal } from './modal'
import { getNativeComponent, getNativeModule } from './modules'
const NativeComponent = getNativeComponent()
const NativeModule = getNativeModule()
const height = 180
const timeModeWidth = 240
const defaultWidth = 310
/** @type {React.FC<PlatformPickerProps>} */
export const DatePickerAndroid = React.memo((props) => {
const thisId = useRef(Math.random().toString()).current
const onChange = useCallback(
/**
* @typedef {{date: string, id: string, dateString: string}} Data
* @param {{ nativeEvent: Data } | Data & { nativeEvent: undefined }} e
*/
(e) => {
const { date, id, dateString } = e.nativeEvent ?? e
const newArch = id !== null
if (newArch && id !== thisId) return
const jsDate = fromIsoWithTimeZoneOffset(date)
if (props.onDateChange) props.onDateChange(jsDate)
if (props.onDateStringChange) props.onDateStringChange(dateString)
},
[props, thisId]
)
const onSpinnerStateChanged = useCallback(
/**
* @typedef {{ spinnerState: "spinning" | "idle", id: string }} SpinnerStateData
* @param {{ nativeEvent: SpinnerStateData } | SpinnerStateData & { nativeEvent: undefined }} e
*/
(e) => {
const { spinnerState, id } = e.nativeEvent ?? e
const newArch = id !== null
if (newArch && id !== thisId) return
props.onStateChange && props.onStateChange(spinnerState)
},
[props, thisId]
)
useEffect(() => {
const eventEmitter = new NativeEventEmitter(NativeModule)
const subscriptionDateChange = eventEmitter.addListener('dateChange', onChange)
const subscriptionSpinnerStateChange = eventEmitter.addListener('spinnerStateChange', onSpinnerStateChanged)
return () => {
subscriptionDateChange.remove();
subscriptionSpinnerStateChange.remove();
}
}, [onChange, onSpinnerStateChanged])
/** @type {NativeProps} */
const modifiedProps = {
...props,
date: toIsoWithTimeZoneOffset(props.date),
id: thisId,
minimumDate: toIsoWithTimeZoneOffset(props.minimumDate),
maximumDate: toIsoWithTimeZoneOffset(props.maximumDate),
timezoneOffsetInMinutes: getTimezoneOffsetInMinutes(props),
style: getStyle(props),
onChange,
onStateChange: onSpinnerStateChanged,
}
useModal({ props: modifiedProps, id: thisId })
if (props.modal) return null
return <NativeComponent {...modifiedProps} />
})
/** @param {PlatformPickerProps} props */
const getStyle = (props) => {
const width = props.mode === 'time' ? timeModeWidth : defaultWidth
return [{ width, height }, props.style]
}
/** @param {PlatformPickerProps} props */
const getTimezoneOffsetInMinutes = (props) => {
// eslint-disable-next-line eqeqeq
if (props.timeZoneOffsetInMinutes == undefined) return undefined
return props.timeZoneOffsetInMinutes
}
/**
* @template {Date | undefined} T
* @param {T} date
* @returns {T extends Date ? string : undefined}
* */
const toIsoWithTimeZoneOffset = (date) => {
/** @ts-ignore */
if (!date) return undefined
/** @ts-ignore */
return date.toISOString()
}
/** @param {string} timestamp */
const fromIsoWithTimeZoneOffset = (timestamp) => {
return new Date(timestamp)
}
export default DatePickerAndroid