about summary refs log tree commit diff
path: root/src/components/forms/DateField/index.tsx
blob: 9bd1c7b813419fdaa2cdf1e892ae325662f968f5 (plain) (blame)
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
import React from 'react'
import {View} from 'react-native'
import DatePicker from 'react-native-date-picker'

import {atoms, useTheme} from '#/alf'
import {DateFieldProps} from '#/components/forms/DateField/types'
import {toSimpleDateString} from '#/components/forms/DateField/utils'
import * as TextField from '#/components/forms/TextField'

export * as utils from '#/components/forms/DateField/utils'
export const Label = TextField.Label

/**
 * Date-only input. Accepts a date in the format YYYY-MM-DD, and reports date
 * changes in the same format.
 *
 * For dates of unknown format, convert with the
 * `utils.toSimpleDateString(Date)` export of this file.
 */
export function DateField({
  value,
  onChangeDate,
  testID,
  label,
}: DateFieldProps) {
  const t = useTheme()

  const onChangeInternal = React.useCallback(
    (date: Date | undefined) => {
      if (date) {
        const formatted = toSimpleDateString(date)
        onChangeDate(formatted)
      }
    },
    [onChangeDate],
  )

  return (
    <View style={[atoms.relative, atoms.w_full]}>
      <DatePicker
        theme={t.name === 'light' ? 'light' : 'dark'}
        date={new Date(value)}
        onDateChange={onChangeInternal}
        mode="date"
        testID={`${testID}-datepicker`}
        aria-label={label}
        accessibilityLabel={label}
        accessibilityHint={undefined}
      />
    </View>
  )
}