about summary refs log tree commit diff
path: root/src/view/com/util/forms/DateInput.web.tsx
blob: 89dff5510c25743e6bb2564aca8f2d5664ac5a9c (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
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
import React, {useState, useCallback} from 'react'
import {
  StyleProp,
  StyleSheet,
  TextInput as RNTextInput,
  TextStyle,
  View,
  ViewStyle,
} from 'react-native'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {useTheme} from 'lib/ThemeContext'
import {usePalette} from 'lib/hooks/usePalette'

interface Props {
  testID?: string
  value: Date
  onChange: (date: Date) => void
  buttonType?: string
  buttonStyle?: StyleProp<ViewStyle>
  buttonLabelType?: string
  buttonLabelStyle?: StyleProp<TextStyle>
  accessibilityLabel: string
  accessibilityHint: string
  accessibilityLabelledBy?: string
}

export function DateInput(props: Props) {
  const theme = useTheme()
  const pal = usePalette('default')
  const palError = usePalette('error')
  const [value, setValue] = useState(props.value.toLocaleDateString())
  const [isValid, setIsValid] = useState(true)

  const onChangeInternal = useCallback(
    (v: string) => {
      setValue(v)
      const d = new Date(v)
      if (!isNaN(Number(d))) {
        setIsValid(true)
        props.onChange(d)
      } else {
        setIsValid(false)
      }
    },
    [setValue, setIsValid, props],
  )

  return (
    <View style={[isValid ? pal.border : palError.border, styles.container]}>
      <FontAwesomeIcon
        icon={['far', 'calendar']}
        style={[pal.textLight, styles.icon]}
      />
      <RNTextInput
        testID={props.testID}
        style={[pal.text, styles.textInput]}
        placeholderTextColor={pal.colors.textLight}
        autoCapitalize="none"
        autoCorrect={false}
        keyboardAppearance={theme.colorScheme}
        onChangeText={v => onChangeInternal(v)}
        value={value}
        accessibilityLabel={props.accessibilityLabel}
        accessibilityHint={props.accessibilityHint}
        accessibilityLabelledBy={props.accessibilityLabelledBy}
      />
    </View>
  )
}

const styles = StyleSheet.create({
  container: {
    borderWidth: 1,
    borderRadius: 6,
    flexDirection: 'row',
    alignItems: 'center',
    paddingHorizontal: 4,
  },
  icon: {
    marginLeft: 10,
  },
  textInput: {
    flex: 1,
    width: '100%',
    paddingVertical: 10,
    paddingHorizontal: 10,
    fontSize: 17,
    letterSpacing: 0.25,
    fontWeight: '400',
    borderRadius: 10,
  },
})