mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react'
2import {Keyboard, View} from 'react-native'
3import DatePicker from 'react-native-date-picker'
4import {msg, Trans} from '@lingui/macro'
5import {useLingui} from '@lingui/react'
6
7import {atoms as a, useTheme} from '#/alf'
8import {Button, ButtonText} from '#/components/Button'
9import * as Dialog from '#/components/Dialog'
10import {DateFieldProps} from '#/components/forms/DateField/types'
11import {toSimpleDateString} from '#/components/forms/DateField/utils'
12import * as TextField from '#/components/forms/TextField'
13import {DateFieldButton} from './index.shared'
14
15export * as utils from '#/components/forms/DateField/utils'
16export const LabelText = TextField.LabelText
17
18/**
19 * Date-only input. Accepts a date in the format YYYY-MM-DD, and reports date
20 * changes in the same format.
21 *
22 * For dates of unknown format, convert with the
23 * `utils.toSimpleDateString(Date)` export of this file.
24 */
25export function DateField({
26 value,
27 onChangeDate,
28 testID,
29 label,
30 isInvalid,
31 accessibilityHint,
32}: DateFieldProps) {
33 const {_} = useLingui()
34 const t = useTheme()
35 const control = Dialog.useDialogControl()
36
37 const onChangeInternal = React.useCallback(
38 (date: Date | undefined) => {
39 if (date) {
40 const formatted = toSimpleDateString(date)
41 onChangeDate(formatted)
42 }
43 },
44 [onChangeDate],
45 )
46
47 return (
48 <>
49 <DateFieldButton
50 label={label}
51 value={value}
52 onPress={() => {
53 Keyboard.dismiss()
54 control.open()
55 }}
56 isInvalid={isInvalid}
57 accessibilityHint={accessibilityHint}
58 />
59 <Dialog.Outer control={control} testID={testID}>
60 <Dialog.Handle />
61 <Dialog.Inner label={label}>
62 <View style={a.gap_lg}>
63 <View style={[a.relative, a.w_full, a.align_center]}>
64 <DatePicker
65 timeZoneOffsetInMinutes={0}
66 theme={t.name === 'light' ? 'light' : 'dark'}
67 date={new Date(value)}
68 onDateChange={onChangeInternal}
69 mode="date"
70 testID={`${testID}-datepicker`}
71 aria-label={label}
72 accessibilityLabel={label}
73 accessibilityHint={accessibilityHint}
74 />
75 </View>
76 <Button
77 label={_(msg`Done`)}
78 onPress={() => control.close()}
79 size="large"
80 color="primary"
81 variant="solid">
82 <ButtonText>
83 <Trans>Done</Trans>
84 </ButtonText>
85 </Button>
86 </View>
87 </Dialog.Inner>
88 </Dialog.Outer>
89 </>
90 )
91}