mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React, {useState, useCallback} from 'react'
2import {StyleProp, StyleSheet, TextStyle, View, ViewStyle} from 'react-native'
3import {
4 FontAwesomeIcon,
5 FontAwesomeIconStyle,
6} from '@fortawesome/react-native-fontawesome'
7import {isIOS, isAndroid} from 'platform/detection'
8import {Button, ButtonType} from './Button'
9import {Text} from '../text/Text'
10import {TypographyVariant} from 'lib/ThemeContext'
11import {useTheme} from 'lib/ThemeContext'
12import {usePalette} from 'lib/hooks/usePalette'
13import {getLocales} from 'expo-localization'
14import DatePicker from 'react-native-date-picker'
15
16const LOCALE = getLocales()[0]
17
18interface Props {
19 testID?: string
20 value: Date
21 onChange: (date: Date) => void
22 buttonType?: ButtonType
23 buttonStyle?: StyleProp<ViewStyle>
24 buttonLabelType?: TypographyVariant
25 buttonLabelStyle?: StyleProp<TextStyle>
26 accessibilityLabel: string
27 accessibilityHint: string
28 accessibilityLabelledBy?: string
29 handleAsUTC?: boolean
30}
31
32export function DateInput(props: Props) {
33 const [show, setShow] = useState(false)
34 const theme = useTheme()
35 const pal = usePalette('default')
36
37 const formatter = React.useMemo(() => {
38 return new Intl.DateTimeFormat(LOCALE.languageTag, {
39 timeZone: props.handleAsUTC ? 'UTC' : undefined,
40 })
41 }, [props.handleAsUTC])
42
43 const onChangeInternal = useCallback(
44 (date: Date) => {
45 setShow(false)
46 props.onChange(date)
47 },
48 [setShow, props],
49 )
50
51 const onPress = useCallback(() => {
52 setShow(true)
53 }, [setShow])
54
55 const onCancel = useCallback(() => {
56 setShow(false)
57 }, [])
58
59 return (
60 <View>
61 {isAndroid && (
62 <Button
63 type={props.buttonType}
64 style={props.buttonStyle}
65 onPress={onPress}
66 accessibilityLabel={props.accessibilityLabel}
67 accessibilityHint={props.accessibilityHint}
68 accessibilityLabelledBy={props.accessibilityLabelledBy}>
69 <View style={styles.button}>
70 <FontAwesomeIcon
71 icon={['far', 'calendar']}
72 style={pal.textLight as FontAwesomeIconStyle}
73 />
74 <Text
75 type={props.buttonLabelType}
76 style={[pal.text, props.buttonLabelStyle]}>
77 {formatter.format(props.value)}
78 </Text>
79 </View>
80 </Button>
81 )}
82 {(isIOS || show) && (
83 <DatePicker
84 timeZoneOffsetInMinutes={0}
85 modal={isAndroid}
86 open={isAndroid}
87 theme={theme.colorScheme}
88 date={props.value}
89 onDateChange={onChangeInternal}
90 onConfirm={onChangeInternal}
91 onCancel={onCancel}
92 mode="date"
93 testID={props.testID ? `${props.testID}-datepicker` : undefined}
94 accessibilityLabel={props.accessibilityLabel}
95 accessibilityHint={props.accessibilityHint}
96 accessibilityLabelledBy={props.accessibilityLabelledBy}
97 />
98 )}
99 </View>
100 )
101}
102
103const styles = StyleSheet.create({
104 button: {
105 flexDirection: 'row',
106 alignItems: 'center',
107 gap: 10,
108 },
109})