Created
December 16, 2022 12:19
-
-
Save longdog/ca141088dd5491375bcdc0343f15a134 to your computer and use it in GitHub Desktop.
ANTd DatePicker wrapper with strings as values (any moment.js converts are inside component)
This file contains hidden or 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
import { DatePicker } from "antd"; | |
import moment, { Moment } from "moment"; | |
import { useEffect, useState } from "react"; | |
export type FormFieldProps<InVal, OutVal> = { | |
value?: InVal; | |
disabled?: boolean; | |
onChange?: (value?: OutVal) => void; | |
}; | |
export type FormField<InVal, OutVal = InVal> = React.FC< | |
FormFieldProps<InVal, OutVal> | |
>; | |
/** | |
ANTd DatePicker wrapper with strings as values | |
*/ | |
export const StringDatePicker: FormField<string> = ({ value, onChange }) => { | |
const [momentVal, setMomentVal] = useState<Moment>(); | |
useEffect(() => setMomentVal(moment(value)), [value]); | |
const onChangeMoment = (_: any, str: string) => onChange!(str); | |
return <DatePicker value={momentVal} onChange={onChangeMoment} />; | |
}; | |
export const StringDateRange: FormField<[string, string]> = ({ value, onChange }) => { | |
const [momentVal, setMomentVal] = useState<[Moment, Moment]>(); | |
useEffect( | |
() => setMomentVal([moment(value?.[0]), moment(value?.[1])]), | |
[value] | |
); | |
const onChangeMoment = (_: any, str: [string, string]) => onChange!(str); | |
return <DatePicker.RangePicker value={momentVal} onChange={onChangeMoment} />; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment