Last active
August 12, 2022 17:12
-
-
Save altovoy/b4d4622a08f9bbab7a782f4a0e0cdf8f to your computer and use it in GitHub Desktop.
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
import { useDateSelect as defaultUseDateSelect } from "react-ymd-date-select" | |
import { useMemo } from "react" | |
const getMonthDays = (year, month) => { | |
return new Date(year, month, 0).getDate() | |
} | |
export const useDateSelect = (...params) => { | |
const dateSelect = defaultUseDateSelect(...params) | |
const { yearValue, monthValue, dayOptions: defaultDayOptions, dayValue, onDayChange } = dateSelect | |
const { dayOptions } = useMemo(() => { | |
const monthDays = getMonthDays(yearValue, monthValue) | |
if (dayValue > monthDays) { | |
onDayChange("") | |
} | |
return { | |
dayOptions: defaultDayOptions.filter((dayOption) => dayOption.value <= monthDays), | |
monthDays, | |
} | |
}, [yearValue, monthValue]) | |
return { | |
...dateSelect, | |
dayOptions, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment