Skip to content

Commit

Permalink
enhance: (DatePicker) adjust the logic of default selected date when …
Browse files Browse the repository at this point in the history
…current time is out of bound
  • Loading branch information
awmleer committed Feb 28, 2022
1 parent 1fe3d86 commit 6f256ff
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/components/date-picker/date-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
defaultRenderLabel,
} from './date-picker-utils'
import type { Precision, DatePickerFilter } from './date-picker-utils'
import { bound } from '../../utils/bound'

export type DatePickerProps = Pick<
PickerProps,
Expand Down Expand Up @@ -65,10 +66,15 @@ export const DatePicker: FC<DatePickerProps> = p => {

const now = useMemo(() => new Date(), [])

const pickerValue = useMemo(
() => convertDateToStringArray(value ?? now, props.precision),
[value, props.precision]
)
const pickerValue = useMemo(() => {
let date = value
if (date === null) {
date = new Date(
bound(now.getDate(), props.min.getDate(), props.max.getDate())
)
}
return convertDateToStringArray(date, props.precision)
}, [value, props.precision])

const onConfirm = useCallback(
(val: PickerValue[]) => {
Expand Down

0 comments on commit 6f256ff

Please sign in to comment.