0.0.3 • Published 1 year ago

sp-date-picker-build-test v0.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

sp-date-picker

Date picker components that allows you to select a day or date range. It comes with two components and utility functions to help you with date manipulation.

  • RangePicker
  • DayPicker
  • date utils

Storybook

Screenshots

range picker

day picker

Demo Video

https://user-images.githubusercontent.com/53928959/225493481-6937f8c1-c5db-429d-b05f-932cbf9e5e9c.mp4

Installation

To Be Added

Usage

DatePicker

// 1. Use the hook provided
const datePicker = useDatePicker()

// 2. Extract what you need from the hook
const { handleOpen, startDate, endDate } = datePicker

// 3. Create handlers that open/close the date picker
const handleDatePickerOpen = useCallback(() => {
  handleOpen(true)
}, [handleOpen])
const handleDatePickerClose = useCallback(() => {
  handleOpen(false)
}, [handleOpen])

// ...

return (
  // 4. Pass the value returned from the hook to the date picker
  // 5. Don't forget to pass the onCloseClick handler
  <DatePicker {...datePicker} onCloseClick={handleDatePickerClose} />
)

DayPicker

// 1. Use the hook provided
const dayPicker = useDayPicker()

// 2. Extract what you need from the hook
const { selectedDay, handleOpen } = dayPicker

// 3. Create handlers that open/close the date picker
const handleDayPickerOpen = useCallback(() => {
  handleOpen(true)
}, [handleOpen])
const handleDayPickerClose = useCallback(() => {
  handleOpen(false)
}, [handleOpen])

// ...

// 4. Pass the value returned from the hook to the date picker
// 5. Don't forget to pass the onCloseClick handler
return <DayPicker {...dayPicker} onCloseClick={handleDayPickerClose} />

Options

There are the props you might be interested in.

DatePicker

OptionRequiredDescriptionDefaultType
titleTitle of the date pickerPick dates range!string
dateoCurrently selected date (used on day picker)Date
startDateoCurrently selected start date (used on range picker)Date
endDateoCurrently selected end date (used on range picker)Date
openoWhether the date picker is open or notfalseboolean
onConfirmClickoClled when confirm button is clicked({ date, startDate, endDate}) => void
onCloseClickoCalled when close button is clicked() => void
onBackdropClickCalled when backdrop is clicked() => void
isRangeWhether the date picker is a range picker or nottrueboolean
disablePastWhether to disable past dates when start date is setfalseboolean
styleCSS styles to be applied to the date pickerCSSProperties
customSelectMonthMotionMotion to be applied to the month selectionAnimationProps (framer-motion)
customSelectDayMotionMotion to be applied to the day selectionAnimationProps (framer-motion)

DayPicker

OptionRequiredDescriptionDefaultType
titleTitle of the date pickerPick a day!string
selectedDayoCurrently selected day-1number
openoWhether the date picker is open or notfalseboolean
onDayClickoCalled when a day is clicked(day: number) => void
onCloseClickoCalled when close button is clicked() => void
onBackdropClickCalled when backdrop is clicked() => void
styleCSS styles to be applied to the date pickerCSSProperties
customMotionMotion to be applied to the picker containerAnimationProps (framer-motion)

Most of props are given by the hook provided. Use javascript deconstruction to extract what you need. See the example below.

// DatePicker - range picker
const datePicker = useDatePicker()
const { startDate, endDate } = datePicker

// DatePicker - day picker
const datePicker = useDatePicker()
const { date } = datePicker

// DayPicker
const datePicker = useDayPicker()
const { selectedDay } = datePicker

Extract handlers to create open/close handlers.

const datePicker = useDayPicker()
const { handleOpen } = datePicker

const handleDatePickerOpen = useCallback(() => {
  handleOpen(true)
}, [handleOpen])

const handleDatePickerClose = useCallback(() => {
  handleOpen(false)
}, [handleOpen])

Date Utilities

FunctionDescription
getEmptyDaysReturns the number of empty days in a month prior to the very first day
getTotalDaysReturns the total number of days in a month
getDayNamesReturns an array of day names in default locale
getFirstSundayReturns the day that is the first Sunday of the month
isSundayCheck if a date is a Sunday
isSaturdayCheck if a date is a Saturday
isWeekendCheck if a date is a weekend (Saturday or Sunday)
getYearMonthDateGet a date format of YYYY-MM-DD
getYearMonthGet a date format of YYYY-MM
getYearGet a date format of YYYY
getMonthGet a date format of MM
getDateGet a date format of DD

Todo

  • Fix failing tests
  • Add more tests if needed