Coder Social home page Coder Social logo

uselessdev / datepicker Goto Github PK

View Code? Open in Web Editor NEW
186.0 6.0 26.0 1.47 MB

A simple datepicker with Chakra-UI and date-fns

Home Page: https://uselessdev-datepicker.netlify.app

License: Other

JavaScript 2.17% TypeScript 97.83%
react datepicker chakra-ui date-fns

datepicker's Introduction

Datepicker

All Contributors

A simple datepicker component build with date-fns and Chakra-UI.

Table of Contents

Requisites

You need to install date-fns and chakra-ui in order to use this library.

yarn add date-fns

To install chakra-ui follow their guide here.

Installation and Usage

After install these dependencies you can now install the library and use this as below:

yarn add @uselessdev/datepicker

Before to use this you can create your own theme or use the default one.

import { ChakraProvider } from '@chakra-ui/react'
import {
  Calendar,
  CalendarDefaultTheme,
  CalendarControls,
  CalendarPrevButton,
  CalendarNextButton,
  CalendarMonths,
  CalendarMonth,
  CalendarMonthName,
  CalendarWeek,
  CalendarDays,
} from '@uselessdev/datepicker'

export function App() {
  const [dates, setDates] = useState()

  const handleSelectDate = (values) => setDates(values)

  return (
    return (
      <ChakraProvider theme={CalendarDefaultTheme}>
        <Calendar value={dates} onSelectDate={handleSelectDate}>
          <CalendarControls>
            <CalendarPrevButton />
            <CalendarNextButton />
          </CalendarControls>

          <CalendarMonths>
            <CalendarMonth>
              <CalendarMonthName />
              <CalendarWeek />
              <CalendarDays />
            </CalendarMonth>
          </CalendarMonths>
        </Calendar>
      </ChakraProvider>
    )
  )
}

note that the example above doens't render an input but only the calendar

If you want to use this with inputs and a popover you can see this example

Customizing

You can fully customize the Calendar component using the extendTheme provided by chakra-ui, you can see an example below.

In your theme you can overrides the default theme (you can see all available components keys for theme customization here)

import { extendTheme } from '@chakra-ui/react'
import { CalendarDefaultTheme } from '@uselessdev/datepicker'

export const theme = extendTheme(CalendarDefaultTheme, {
  components: {
    Calendar: {
      parts: ['calendar'],

      baseStyle: {
        calendar: {
          borderWidth: '6px',
          borderColor: 'pink.400',
          rounded: 'none',
          shadow: 'none',
          boxShadow: '32px 16px 0 6px #3B4DCC'
        },
      },
    },

    CalendarControl: {
      parts: ['button'],

      baseStyle: {
        button: {
          h: 6,
          px: 2,
          rounded: 'none',
          fontSize: 'sm',
          color: 'white',
          bgColor: 'pink.400',

          _hover: {
            bgColor: 'pink.200',
          },

          _focus: {
            outline: 'none',
          },
        },
      },
    }
  },
})

Now you can use this theme in ChakraProvider:

import { ChakraProvider } from '@chakra-ui/react'
import { theme } from './theme'

function App() {
  return (
    <ChakraProvider theme={theme}>
      {/* children... */}
    </ChakraProvider>
  )
}

Theses changes will produce the following results in Calendar:

Customized calendar

Available components theme keys

Key name Description Parts
Calendar A multipart component this is reponsible for the calendar it self. calendar, months
CalendarMonth Responsible to style one month block. month, name, week, weekday, days
CalendarDay Applies styles to individual day. This is the only single part component. --
CalendarControl Applies styles to prev and next months. controls, button

License

This code is under the Apache-2.0 License

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Wallace Batista
Wallace Batista

πŸ’» πŸ€”
Leonardo Elias
Leonardo Elias

πŸ’»
kivi
kivi

πŸ’»
Guilherme Teixeira
Guilherme Teixeira

πŸ’»
Brano Zavracky
Brano Zavracky

πŸ’»
O. Qudah
O. Qudah

πŸ“–
Tom Chen
Tom Chen

πŸ“– πŸ’»
Alexandre Stahmer
Alexandre Stahmer

πŸ’»
Raphael da Rocha Pinto Barboza
Raphael da Rocha Pinto Barboza

πŸ’»
Greg Leuch
Greg Leuch

πŸ’»

This project follows the all-contributors specification. Contributions of any kind welcome!

datepicker's People

Contributors

allcontributors[bot] avatar astahmer avatar basicpixel avatar branikclimbs avatar curiouslycory avatar ggteixeira avatar gleuch avatar kivi avatar leonardoelias avatar raphaelrochap avatar tomchentw avatar uselessdev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

datepicker's Issues

Full week selection

Is it possible to select full weeks?
If not, could you point out how such a feature could be implemented?

Something like this:

image

Use the Locale on date-fns functions

Issue

#38 is a good demonstration for this bug. If I change locale to the one that has Monday as their first day of the week, the calendar will not respect that. Eg. Italian locale https://github.com/date-fns/date-fns/blob/v2.25.0/src/locale/it/index.js

JPEG-1

Has an options.weekStartsOn = 1. With #38, it gives:

JPEG-2

Where Dom (domenica) is Sunday in Italian and Lun (lunedì) is Monday in Italina

Solution

Whenever date-fns functions are used, they should given the locale object.
https://date-fns.org/v2.28.0/docs/Locale

I will see if I could submit a PR for this. Thank you

How can we custom the date format e.g. dd/MM/yyyy

Problem

I need to create Date Range Picker with dd/MM/yyyy format, but It's hard to find docs about it.

Suggested Solution

Create a full docs of usage coverage, start with how to custom the format of the input date.

Different version on npm and in the source code

The bug is that the 201c0c9 commit is not actually reflected in the current version(2.7.0) of the package.

This can be easily verified:

  • Here is the link to the npm code (open .cjs.dev.js or .cjs.prod.js file) where you need to find the line var styles = react.useStyleConfig('CalendarDay', {, and see that there is no interval here.
  • If you download the code and run npm run build, in the source code at around line +- 541 (or it's better to find it by the line I provided above), there will be a parameter interval.

Skipping years

First of all, great and well-written package!

I wonder if it's possible to skip years, not only months. It's a requirement I have for my app, and wonder if this needs to be added or could be easily customized?

Show current date

It would be nice to be able to see the current day.
Something like this:
asd

Date format dd/MM/yyyy with input and singleDateSelection

Hi!
I'm trying to use it in a NextJS project and I'm facing some issues.
When I try to implement with format dd/MM/yyyy with input as the example given I face an error as followed.
image

Any hints on how to fix it?

Thanks in advance.

starting week on Monday

Hi,

I would like display a month view with weeks starting on Monday. The default is starting on Sunday.
Is that somehow possible or would that needed to be developed?
I could not find any hint in documentation/storybook or in source.

Thanks
v

Duplicate key issue in US locale

There's a key issue where it's using the weekday names. In the US locale there are 2 S's and 2 T's causing duplicate key issues.
Created PR to resolve: #69

dependency problem while running npm i

i get the following error while running npm I:
I'm using:

    "framer-motion": "^10.2.4",
    "@uselessdev/datepicker": "^2.7.0",
    "@chakra-ui/react": "^2.5.1",

npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR! 
npm ERR! While resolving: @uselessdev/[email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/framer-motion
npm ERR!   framer-motion@"^10.2.4" from the root project
npm ERR!   peer framer-motion@">=4.0.0" from @chakra-ui/[email protected]
npm ERR!   node_modules/@chakra-ui/accordion
npm ERR!     @chakra-ui/accordion@"2.1.9" from @chakra-ui/[email protected]
npm ERR!     node_modules/@chakra-ui/react
npm ERR!       @chakra-ui/react@"^2.5.1" from the root project
npm ERR!       1 more (@uselessdev/datepicker)
npm ERR!   8 more (@chakra-ui/menu, @chakra-ui/modal, @chakra-ui/popover, ...)
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer framer-motion@"^6" from @uselessdev/[email protected]
npm ERR! node_modules/@uselessdev/datepicker
npm ERR!   @uselessdev/datepicker@"^2.7.0" from the root project
npm ERR! 
npm ERR! Conflicting peer dependency: [email protected]
npm ERR! node_modules/framer-motion
npm ERR!   peer framer-motion@"^6" from @uselessdev/[email protected]
npm ERR!   node_modules/@uselessdev/datepicker
npm ERR!     @uselessdev/datepicker@"^2.7.0" from the root project

TypeError: can't access proprty "start" after following guide

Following the installation and usage guide, using the component throws the following error:

Uncaught TypeError: can't access property "start", value is undefined
    Calendar$1 uselessdev-datepicker.esm.js:294
    React 11
    workLoop scheduler.development.js:266
    flushWork scheduler.development.js:239
    performWorkUntilDeadline scheduler.development.js:533
    js scheduler.development.js:571
    js scheduler.development.js:633
    __require chunk-MB5FDF5C.js:12
    js index.js:6
    __require chunk-MB5FDF5C.js:12
    React 2
    __require chunk-MB5FDF5C.js:12
    js React
    __require chunk-MB5FDF5C.js:12
    js React
    __require chunk-MB5FDF5C.js:12
    <anonymous> react-dom_client:1
uselessdev-datepicker.esm.js:294:6
The above error occurred in the <Calendar$1> component:

Calendar$1@https://8fqih0.csb.app/node_modules/@uselessdev/datepicker/dist/uselessdev-datepicker.esm.js:326:18
EnvironmentProvider@https://8fqih0.csb.app/node_modules/@chakra-ui/react-env/dist/index.esm.js:119:62
ColorModeProvider@https://8fqih0.csb.app/node_modules/@chakra-ui/color-mode/dist/index.esm.js:183:15
ThemeProvider@https://8fqih0.csb.app/node_modules/@emotion/react/dist/emotion-element-b63ca7c6.cjs.dev.js:106:21
ThemeProvider@https://8fqih0.csb.app/node_modules/@chakra-ui/system/dist/index.esm.js:182:21
ChakraProvider@https://8fqih0.csb.app/node_modules/@chakra-ui/provider/dist/index.esm.js:20:177
ChakraProvider@https://8fqih0.csb.app/node_modules/@chakra-ui/react/dist/index.esm.js:110:24
App@https://8fqih0.csb.app/src/App.js:26:41

Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries. 
    in Unknown index.js:27:25

This is replicated in the following codesandbox:

https://codesandbox.io/embed/uselessdev-datepicker-demo-8fqih0

Using this code:

import { useState } from "react";
import { ChakraProvider } from "@chakra-ui/react";
import {
  Calendar,
  CalendarDefaultTheme,
  CalendarControls,
  CalendarPrevButton,
  CalendarNextButton,
  CalendarMonths,
  CalendarMonth,
  CalendarMonthName,
  CalendarWeek,
  CalendarDays
} from "@uselessdev/datepicker";

export default function App() {
  const [dates, setDates] = useState();

  const handleSelectDate = (values) => setDates(values);

  return (
    <>
      hello world
      <ChakraProvider theme={CalendarDefaultTheme}>
        <Calendar value={dates} onSelectDate={handleSelectDate}>
          <CalendarControls>
            <CalendarPrevButton />
            <CalendarNextButton />
          </CalendarControls>

          <CalendarMonths>
            <CalendarMonth>
              <CalendarMonthName />
              <CalendarWeek />
              <CalendarDays />
            </CalendarMonth>
          </CalendarMonths>
        </Calendar>
      </ChakraProvider>
    </>
  );
}

Disable dates after/before certain date

It would be useful if props disablePastDates, disableFutureDates could take a date as an option. Alternatively, it could be new props called disablePastDatesFrom, disableFutureDatesFrom.
In my case, I need to allow users to select only a certain range let's say from January first to February second. I need all other dates to be disabled.
I can make PR for that if it is okay.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.