Skip to content

Instantly share code, notes, and snippets.

@altovoy
Last active August 12, 2022 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save altovoy/b4d4622a08f9bbab7a782f4a0e0cdf8f to your computer and use it in GitHub Desktop.
Save altovoy/b4d4622a08f9bbab7a782f4a0e0cdf8f to your computer and use it in GitHub Desktop.
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