Skip to content

Instantly share code, notes, and snippets.

@cesargdm
Last active June 1, 2022 00:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cesargdm/ceb486d73e43b8ecd5ca5fc45c2bd834 to your computer and use it in GitHub Desktop.
Save cesargdm/ceb486d73e43b8ecd5ca5fc45c2bd834 to your computer and use it in GitHub Desktop.
DateTimePicker.web.ts
// 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