Last active
June 1, 2022 00:55
-
-
Save cesargdm/ceb486d73e43b8ecd5ca5fc45c2bd834 to your computer and use it in GitHub Desktop.
DateTimePicker.web.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DateTimePicker.web.ts | |
import React, { useEffect, useRef, ReactElement } from 'react' | |
import { TextInput } from 'react-native' | |
import { format } from 'date-fns' | |
interface Props { | |
minimumDate: Date | |
maximumDate: Date | |
value: string | |
onChange: (value: string) => void | |
// ...more | |
} | |
function DateInput(props: Props): ReactElement { | |
const { minimumDate, maximumDate, ...moreProps } = props | |
const inputRef = useRef<TextInput>(null) | |
useEffect(() => { | |
if (inputRef?.current) { | |
inputRef?.current?.setNativeProps({ | |
type: 'date', | |
min: format(minimumDate, 'yyyy-MM-dd'), | |
max: format(maximumDate, 'yyyy-MM-dd'), | |
pattern: 'd{4}-d{2}-d{2}', | |
}) | |
} | |
}, [inputRef?.current]) | |
return <TextInput ref={inputRef} type="date" {...moreProps} /> | |
} | |
export default DateInput |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment