11.50.0 • Published 6 months ago

@uxf/datepicker v11.50.0

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
6 months ago

@uxf/datepicker

npm size quality license

A set of React hooks to create an awesome datepicker.

Quick start

Main logic component

export const DatePicker: FC = () => {
    // prepare state to handle selected date
    const [selectedDate, setSelectedDate] = useState<OnDateChangeType>(null);

    const {
        activeMonths,
        firstDayOfWeek,
    } = useDatePicker({
        selectedDate,
        minBookingDate: new Date(),
        onDateChange: setSelectedDate,
    });

    // you can use DatePickerContext that helps you avoid passing callbacks throught components
    return (
        <DatePickerContext.Provider value={{ addPropsHere }}>
              {activeMonths.map(month => (
                    <DateRangePickerMonth
                        firstDayOfWeek={firstDayOfWeek}
                        key={`${month.year}-${month.month}`}
                        month={month.month}
                        year={month.year}
                    />
              ))}
        </DatePickerContext.Provider>
    );
};

Month component

export const DateRangePickerMonth: FC<UseMonthProps> = ({
    firstDayOfWeek,
    month,
    year,
}) => {
    const { days, monthLabel } = useMonth({
        year,
        month,
        firstDayOfWeek,
    });

    return (
        <div>
            <p>{monthLabel}</p>
            <div style={{ display: "grid" }}>
                {days.map((day, index) => {
                    return <DateRangePickerDay date={day.date} key={day.dayLabel + index} day={day.dayLabel} />;
                })}
            </div>
        </div>
    );
};

Day component

export const DateRangePickerDay: FC<{ day: string; date: Date }> = ({ day, date }) => {
    const dayRef = useRef(null);
    const {
        focusedDate,
        isDateFocused,
        isDateSelected,
        isDateHovered,
        isDateBlocked,
        onDateSelect,
        onDateFocus,
        onDateHover,
    } = useContext(DatePickerContext);

    const { disabledDate, isSelected, isWithinHoverRange, onClick, isToday } =
        useDay<HTMLDivElement>({
            date,
            dayRef,
            focusedDate,
            isDateFocused,
            isDateSelected,
            isDateHovered,
            isDateBlocked,
            onDateFocus,
            onDateSelect,
            onDateHover,
        });

    if (!day) {
        return <div />;
    }

    return (
        <button
            type="button"
            ref={dayRef}
            onClick={onClick}
            className= {clsx({
                "day__disabled": disabledDate,
                "day__selected": isSelected,
                "day__today": isToday,
            })}
        >
            {day}
        </button>
    );
};

API

useDatePicker()

UseDatePickerProps

nametyperequireddefaultdescription
firstDayOfWeekFirstDayOfWeek0
initialVisibleMonthDate
isDateBlocked(date: Date) => boolean() => false
maxBookingDateDate
minBookingDateDate
numberOfMonthsnumber1
onDateChange(data: OnDateChangeType): voidyes
selectedDateDate \| nullyes
unavailableDatesDate[][]@deprecated
datesConfigDatesConfig[][]

UseDatePickerReturnType

nametypedescription
canGoToMonth(month: Date) => boolean
canGoToNextMonthboolean
canGoToNextYearboolean
canGoToPrevMonthboolean
canGoToPrevYearboolean
canGoToYear(month: Date) => boolean
goToDate(date: Date) => void
goToNextMonths() => void
goToNextMonthsByOneMonth() => void
goToNextYear() => void
goToPrevMonths() => void
goToPrevMonthsByOneMonth() => void
goToPrevYear() => void
activeMonthsMonthType[]
firstDayOfWeekFirstDayOfWeek
focusedDateDate \| null
hoveredDateDate \| null
isDateBlocked(date: Date) => boolean
isDateFocused(date: Date) => boolean
isDateHovered(date: Date) => boolean
isDateSelected(date: Date) => boolean
numberOfMonthsnumber
onDateFocus(date: Date) => void
onDateHover(date: Date \| null) => void
onDateSelect(date: Date) => void
onResetDates() => void

useDateRangePicker()

UseDateRangePickerProps

nametyperequireddefaultdescription
changeActiveMonthOnSelectbooleanfalse
endDateDate \| nullyes
exactMinBookingDaysbooleanfalse
firstDayOfWeekFirstDayOfWeek0
focusedInputFocusedInputyesstartDate
initialVisibleMonthDate
isDateBlocked(date: Date) => boolean() => false
maxBookingDateDate
minBookingDateDate
minBookingDatesnumber1
numberOfMonthsnumber2
onDatesChange(data: OnDatesChangeType): voidyes
startDateDate \| nullyes
unavailableDatesDate[][]@deprecated
datesConfigDatesConfig[][]

UseDateRangePickerReturnType

nametypedescription
canGoToMonth(month: Date) => boolean
canGoToNextMonthboolean
canGoToNextYearboolean
canGoToPrevMonthboolean
canGoToPrevYearboolean
canGoToYear(month: Date) => boolean
goToDate(date: Date) => void
goToNextMonths() => void
goToNextMonthsByOneMonth() => void
goToNextYear() => void
goToPrevMonths() => void
goToPrevMonthsByOneMonth() => void
goToPrevYear() => void
activeMonthsMonthType[]
firstDayOfWeekFirstDayOfWeek
focusedDateDate \| null
hoveredDateDate \| null
isDateBlocked(date: Date) => boolean
isDateFocused(date: Date) => boolean
isDateHovered(date: Date) => boolean
isDateSelected(date: Date) => boolean
isEndDate(date: Date) => boolean
isStartDate(date: Date) => boolean
numberOfMonthsnumber
onDateFocus(date: Date) => void
onDateHover(date: Date \| null) => void
onDateSelect(date: Date) => void
onResetDates() => void

useDecade()

UseDecadeProps

nametyperequireddefaultdescription
decadeLabelFormat(start: Date, end: Date) => string
yearnumberyes
yearLabelFormat'date: Date) => string

UseDecadeReturnType

nametypedescription
years{ yearLabel : string; date: Date }[]
decadeLabelstring

useYear()

UseYearProps

nametyperequireddefaultdescription
yearnumberyes
yearLabelFormat(date: Date) => string
monthLabelFormat(date: Date) => string

UseYearReturnType

nametypedescription
months{ monthLabel : string; date: Date }[]
yearLabelstring

useMonth()

UseMonthProps

nametyperequireddefaultdescription
yearnumberyes
monthnumberyes
firstDayOfWeekFirstDayOfWeek0
dayLabelFormat(date: Date) => string
weekdayLabelFormat(date: Date) => string
monthLabelFormat(date: Date) => string

UseMonthReturnType

nametypedescription
days{currentMonth: boolean; dayLabel: string; date: Date })[]
monthLabelstring
weekdayLabelsstring[]

useDay()

UseDayProps

nametyperequireddefaultdescription
dateDateyes
dayRefRefObject
focusedDateDate \| nullyes
isDateBlocked(date: Date) => booleanyes
isDateFocused(date: Date) => booleanyes
isDateHovered(date: Date) => booleanyes
isDateSelected(date: Date) => booleanyes
onDateFocus(date: Date) => voidyes
onDateHover(date: Date) => voidyes
onDateSelect(date: Date) => voidyes
unavailableDatesDate[]@deprecated
datesConfigDatesConfig[]

UseDayReturnType

nametypedescription
disabledDateboolean
isFirstDisabledboolean
isLastDisabledboolean
disabledDateboolean
isHoveredboolean
isSelectedboolean
isTodayboolean
isWithinHoverRangeboolean
onClick() => void
onKeyDown(e: KeyboardEvent) => void
onMouseEnter() => void
tabIndexnumber
flagsFlag[]

Known issues

  • keyboard navigation doesn't work for year and decade view
  • the demo needs a little more love!
11.50.0

6 months ago

11.45.0

7 months ago

11.32.0

10 months ago

11.25.0

12 months ago

11.11.3

1 year ago

11.10.0

1 year ago

10.8.0

2 years ago

10.0.0-beta.80

2 years ago

10.0.0

2 years ago

10.0.0-beta.35

2 years ago

10.0.0-beta.41

2 years ago

11.0.0

2 years ago

10.0.0-beta.37

2 years ago

11.1.0

2 years ago

10.0.0-beta.9

2 years ago

1.2.1

2 years ago

1.2.0

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

4 years ago

1.1.1

4 years ago

1.1.2

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago