Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@uxf/datepicker

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@uxf/datepicker

A set of React hooks to create an awesome datepicker.

  • 11.10.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@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!

FAQs

Package last updated on 22 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc