bsky-app/src/view/com/util/forms/DateInput.tsx
Hailey 2d9a5db967
Switch date picker libraries (#3126)
* Revert other base PR changes, switch date picker library

rm expo-linear-gradient

Revert "remove unused packages, switch to `expo-linear-gradient`"

This reverts commit 20a0ffcf

Revert "upgrade expo deps"

This reverts commit a43dca92caa120d45fb771431752dd2db3b37ab5.

Revert other library changes

This reverts commit 5219f66e

Revert "re-add normalize-url"

This reverts commit 654019c4babe2e455f6069a6c725eb51140ab1ab.

Revert "add `expo-haptics`"

This reverts commit ff3a0399b1c7eae07b83946f13543eedf7cdfe82.

Revert "add `expo-clipboard`"

This reverts commit 440ae91632153e22ff79050d8ac803a7305e88a0.

Revert "add `expo-file-system`"

This reverts commit c0fe0c94534564982399c22299a8a19052bf3e54.

fix android alf

use modal on android

migrate to `react-native-date-picker`

rm `@reactnativecommunity/datetimepicker`

add `react-native-date-picker`

add `expo-file-system`

add `expo-clipboard`

add `expo-haptics`

re-add normalize-url

rm blur view

upgrade expo deps

remove unused packages, switch to `expo-linear-gradient`

* rm old library

* Use UTC for dates

---------

Co-authored-by: Eric Bailey <git@esb.lol>
2024-03-06 13:13:03 -06:00

109 lines
3 KiB
TypeScript

import React, {useState, useCallback} from 'react'
import {StyleProp, StyleSheet, TextStyle, View, ViewStyle} from 'react-native'
import {
FontAwesomeIcon,
FontAwesomeIconStyle,
} from '@fortawesome/react-native-fontawesome'
import {isIOS, isAndroid} from 'platform/detection'
import {Button, ButtonType} from './Button'
import {Text} from '../text/Text'
import {TypographyVariant} from 'lib/ThemeContext'
import {useTheme} from 'lib/ThemeContext'
import {usePalette} from 'lib/hooks/usePalette'
import {getLocales} from 'expo-localization'
import DatePicker from 'react-native-date-picker'
const LOCALE = getLocales()[0]
interface Props {
testID?: string
value: Date
onChange: (date: Date) => void
buttonType?: ButtonType
buttonStyle?: StyleProp<ViewStyle>
buttonLabelType?: TypographyVariant
buttonLabelStyle?: StyleProp<TextStyle>
accessibilityLabel: string
accessibilityHint: string
accessibilityLabelledBy?: string
handleAsUTC?: boolean
}
export function DateInput(props: Props) {
const [show, setShow] = useState(false)
const theme = useTheme()
const pal = usePalette('default')
const formatter = React.useMemo(() => {
return new Intl.DateTimeFormat(LOCALE.languageTag, {
timeZone: props.handleAsUTC ? 'UTC' : undefined,
})
}, [props.handleAsUTC])
const onChangeInternal = useCallback(
(date: Date) => {
setShow(false)
props.onChange(date)
},
[setShow, props],
)
const onPress = useCallback(() => {
setShow(true)
}, [setShow])
const onCancel = useCallback(() => {
setShow(false)
}, [])
return (
<View>
{isAndroid && (
<Button
type={props.buttonType}
style={props.buttonStyle}
onPress={onPress}
accessibilityLabel={props.accessibilityLabel}
accessibilityHint={props.accessibilityHint}
accessibilityLabelledBy={props.accessibilityLabelledBy}>
<View style={styles.button}>
<FontAwesomeIcon
icon={['far', 'calendar']}
style={pal.textLight as FontAwesomeIconStyle}
/>
<Text
type={props.buttonLabelType}
style={[pal.text, props.buttonLabelStyle]}>
{formatter.format(props.value)}
</Text>
</View>
</Button>
)}
{(isIOS || show) && (
<DatePicker
timeZoneOffsetInMinutes={0}
modal={isAndroid}
open={isAndroid}
theme={theme.colorScheme}
date={props.value}
onDateChange={onChangeInternal}
onConfirm={onChangeInternal}
onCancel={onCancel}
mode="date"
testID={props.testID ? `${props.testID}-datepicker` : undefined}
accessibilityLabel={props.accessibilityLabel}
accessibilityHint={props.accessibilityHint}
accessibilityLabelledBy={props.accessibilityLabelledBy}
/>
)}
</View>
)
}
const styles = StyleSheet.create({
button: {
flexDirection: 'row',
alignItems: 'center',
gap: 10,
},
})