A date picker built especially for React, with love.
Click for LIVE DEMO!
$ npm install react-date-picker
Require the css!!!
import 'react-date-picker/index.css'
import DatePicker from 'react-date-picker'
let date = '2014-10-10' //or Date.now()
function onChange(dateString, { dateMoment, timestamp}){
//...
}
<DatePicker
minDate='2014-04-04'
maxDate='2015-10-10'
date={date}
onChange={onChange}
/>
See changelog
For the old v4
README, see v4
Theming is done by requiring a specific css file.
By default, react-date-picker/index.css
contains both structural styles and the default theme.
If you want to load a specific theme, make sure you load
import 'react-date-picker/base.css'
first (which contains only structural css rules), and then any css theme file. For now, there is one theme available, named very obviously "default"
:
react-date-picker/theme/default.css
Don't forget to include index.css or index.sass!
For rendering the date picker with a custom locale, there are two options
The first option is to simply require the appropriate momentjs locale before require
-ing react-date-picker
Example:
//make sure you require this first!
const nl = require('moment/locale/nl')
//and then require the date picker - it will use
//the locale you previously required
import DatePicker from 'react-date-picker'
The second option is specifying the locale
prop on the date picker. This assumes you have momentjs with the locale already into page (for example, you are using moment-with-locales.js
)
<DatePicker locale="fr" />
weekDayNames
- either an array of week day names, or a function that returns an array. In case you specifyweekDayNames
as an array, it should have Sunday as the first day. If not specified, will be built withmomentjs
usingmoment.weekdaysShort()
weekStartDay
: Number - Sun 0, Monday 1, etc... If not specified, the locale specific value will be uesd.locale
: StringtodayText
: StringgotoSelectedText
: String
-
hideFooter
: Boolean - by default footer is shown, so specify this to true if you don't want the footer -
hideHeader
: Boolean - by default header is shown, so specify this to true if you don't want the header -
date
: Date / String / Moment / Number - for controlled behavior. Make sure you also specify anonChange
prop -
range
: Date / String / Moment / Number - for controlled behaviour make sure you also specify anonRangeChange
prop -
defaultDate
: Date / String / Moment / Number - for uncontrolled behavior -
defaultRange
: Date / String / Moment / Number - for uncontrolled behavior -
minDate
: Date / String / Moment / Number -
maxDate
: Date / String / Moment / Number -
dateFormat
: String see moment.js formats. Default date format is 'YYYY-MM-DD' -
onChange
: Function (dateText, moment, event) - called when the user selects a date -
onRangechange
: Function (arrayText, arratMoment, event) - called when the user selects a range -
onSelect
: Function(dateText, moment, view) - called when the user selects a year/month -
onNav
: Function(dateText, moment, view, direction) - called when the user navigates to the next/previous month/year/decade. -
renderDay
: Function - (optional) A function that should return a React DOM for the day cell. The first param is the props object. You can use this to have full control over what gets rendered for a day. -
onRenderDay
: Function - (optional) A function that can manipulate the props object for a day, and SHOULD return a new props object. Use this for custom day styling. You can use this to take full control over the styles/css classes/attributes applied to the day cell in the month view. -
views
: Object - an object that maps view names to factory functions. See the exportedDatePicker.views
-
weekNumbers
: Boolean (default false) - Shows weeknumbers - it's locale aware. -
weekNumberName
: String / ReactElement (defaults to '') - What to render in the view header cell, above all week numbers. -
renderWeekNumber
: Function - A function to render week number. Has the same behavior asrenderDay
. -
onWeekChange
: Function(weekDates, event) - When a week is clicked you get an array of the dates in that week -
highlightRangeOnMouseMove
: Boolean (default true) - when hover mouse over a date, shows the range that will be selected if the date is clicked
dayFormat
- The format in which days should be rendered (on the MonthView)monthFormat
- The format in which months should be rendered (on the YearView)yearFormat
- The format in which years should be rendered (on the DecadeView)
-
defaultViewDate
: Date / String / Moment / Number - a date for the period to show in the picker. If none specified, defaults todate
or to the current date. -
viewDate
: Date / String / Moment / Number - controlled version fordefaultViewDate
-
onViewDateChange
: Function(dateText, moment , view) - called when navigating to another viewDate. -
defaultView
: String - the view to render initially in the datepicker - if no defaultView is specified, the "month" view is rendered. Possible values: "month", "year", "decade". -
view
: String - controlled version fordefaultView
. -
onViewChange
: Function - function called when the view is changed. If using the controlledview
version, make sure you update theview
prop in this function if you want to navigate to another view as expected. -
navOnDateClick
: Boolean - defaults to true. If false, will not navigate to the date that was clicked, even if that date is in the prev/next month -
alwaysShowPrevWeek
Boolean - defaults to false. If false, when the month starts on the first day of the week (weekStartDay
), no days from the previous month will be displayed. Otherwise the first row will display the last week from the previous month.
highlightWeekends
: Boolean - defaults to false. By default, weekend days have the'dp-weekend'
className, but this will also add the'dp-weekend-hightlight'
className with a default redish color.
const onRenderDay = (props) => {
if (props.date.isBefore('2010-01-01')){
props.className += ' invalid'
}
props.style.border = '1px solid red'
return props
}
<DatePicker
onChange={this.onChange}
onRenderDay={onRenderDay}
/>
When contributing, please work on the src
dir.
You'll need to run the following commands:
$ npm i # install all depedencies
$ npm run dev
# starts webpack-dev-server, which does all the bundling and live reloading
Now navigate to localhost:8080 With this setup, you have an environment which live-reloads all your changes, so you have a rapid development cycle.
react-date-picker
uses the awesome moment.js
library ( Big thanks!)