npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-hook-pickers

v0.2.2

Published

Have you ever get tired of customizing the date and time pickers provided by the libraries? And you thought of building your own date picker, however you also don't have that much time or it's painful to build everything from scratch for you. All you just

Downloads

23

Readme

React Hook Pickers

Have you ever get tired of customizing the date and time pickers provided by the libraries? And you thought of building your own date picker, however you also don't have that much time or it's painful to build everything from scratch for you. All you just want to do is custom UI and forget about the state handling, generating the dates for each month, years, etc. But your UI/UX designers won't let you, because their design is cool. Here's the react-hook-pickers to rescue you from all the hassle.

react-hook-pickers provide hooks for building Custom Date Picker UI without worrying about handling states. This library is inspired by react-hook-form and downshift.


Installation

npm i react-hook-pickers

Usage

DatePicker

react-hook-pickers gives two primitive hooks for building date pickers, useDatePickerState and useDatePicker.

useDatePickerState

useDatePickerState exposes the necessary states that useDatePicker is needed. You can also control the states exposed by useDatePickerState hook too.

import { useDatePickerState } from 'react-hook-pickers'

const initialDate = new Date()
const datePickerStates = useDatePickerState(initialDate)

useDatePicker

This is the actual UI building block for the date picker. It exposes getCalendarProps and getCalendarViewControllers functions to build the DatePicker UI. getCalendarProps will give you the necessary data and props that is needed to build a Calendar UI. getCalendarViewControllers will give you the utilities functions to control the Calendar UI. While getCalendarViewControllers is not necessary if you know what you are doing, you can directly control the states given by the useDatePickerState.

import { useDatePickerState, useDatePicker } from 'react-hook-pickers'

const initialDate = new Date()
const datePickerStates = useDatePickerState(initialDate)
const { today, getCalendarProps, getCalendarViewControllers } = useDatePicker(
  datePickerStates,
)

DatePickerProvider and useDatePickerProvider

This is the Context wrapper for the useDatePicker. You just provide datePickerState from the useDatePickerState to DatePickerProvider and you can use useDatePickerProvider anywhere inside it. With this, you can forget about props drilling.

import {
  useDatePickerState,
  DatePickerProvider,
  useDatePickerProvider,
} from 'react-hook-pickers'

const ChildComponent = () => {
  const {
    datePickerState,
    today,
    getCalendarProps,
    getCalendarViewControllers,
  } = useDatePickerProvider()
  return <>...</>
}

const ParentComponent = () => {
  const initialDate = new Date()
  const datePickerState = useDatePickerState(initialDate)
  return (
    <DatePickerProvider datePickerState={datePickerState}>
      <ChildComponent />
    </DatePickerProvider>
  )
}

Example

Edit react-hook-pickers__examples


API

  • useDatePickerState function, required parameter Date object as initial date.
// useDatePickerState hook return values
selectedDate: Date;
viewDate: Date;
setSelectedDate: React.Dispatch<React.SetStateAction<Date>>;
setViewDate: React.Dispatch<React.SetStateAction<Date>>;
reset: () => void;
  • useDatePicker function, required return values of useDatePickerState.
// useDatePicker hook return values
today: Date
getCalendarProps: () => {
    daysOfWeek: {
        DDDD: DDDD;
        DDD: DDD;
        D: D;
    }[];
    months: {
        MMMM: MMMM;
        MMM: MMM;
    }[];
    datesOfMonthBtnProps: ({ onClick, btnProps }: CalendarBtnProps) => {
      key: string;
      id: string;
      'data-testid': string;
      'data-day': 0 | 1 | 2 | 3 | 4 | 5 | 6;
      'data-date': string;
      children: string;
      isToday: boolean;
      isSelected: boolean;
      ...;
    }[];
    fillUpDatesOfPrevMonthBtnProps: ({ onClick, btnProps, }: CalendarBtnProps) => {
      key: string;
      id: string;
      'data-testid': string;
      'data-day': 0 | 1 | 2 | 3 | 4 | 5 | 6;
      'data-date': string;
      children: string;
      isToday: boolean;
      isSelected: boolean;
      ...;
    }[];
    fillUpDatesOfNextMonthBtnProps: ({ onClick, btnProps, }: CalendarBtnProps) => {
      key: string;
      id: string;
      'data-testid': string;
      'data-day': 0 | 1 | 2 | 3 | 4 | 5 | 6;
      'data-date': string;
      children: string;
      isToday: boolean;
      isSelected: boolean;
      ...;
    }[];
}
getCalendarViewControllers: () => {
  goToMonth: (month: Month) => void;
  goToYear: (year: number) => void;
  goToPrevMonth: () => void;
  goToNextMonth: () => void;
  goToToday: () => void;
  goToSelectedDate: () => void;
  resetCalendarView: () => void;
}

Types

// date
type D = 'S' | 'M' | 'T' | 'W' | 'F' // days of week shortest name
type DDD = 'Sun' | 'Mon' | ... | 'Sat' // days of week short names
type DDDD = 'Sunday' | 'Monday' | ... | 'Saturday' // days of week full names
type DateNumber = 1 | 2 | 3 | ... | 31 // all available dates
// month
type MMM = 'Jan' | 'Feb' | ... | 'Dec' // month short names
type MMMM = 'January' | 'February' | ... | 'December' // month full names
type Month = 0 | 1 | 2 | ... | 11 // JavaScript month indexes

Roadmap

  • [x] Date Picker (WIP)
  • [ ] Documentation
  • [ ] Date Range Picker (although it is possible to build date range picker with the current useDatePickerState hook, it would be nice to give the pre configured hooks for the date range picker.)
  • [ ] Accessibility
  • [ ] Time Picker
  • [ ] Primitive UI Components