A Reactstrap based, zero dependencies, date picker.
Based on react-bootstrap-date-picker.
Demo and docs at reactstrap-date-picker.
reactstrap-date-picker
is compatible with React 0.14.x, 0.15.x and 0.16.x.
npm install reactstrap-date-picker
import React from 'react
import {FormGroup, Label, FormText} from 'reactstrap
var DatePicker = require("reactstrap-date-picker");
class App extends React.Component {
constructor(props) {
super(props)
this.state= {
value: new Date().toISOString()
}
}
handleChange(value, formattedValue) {
this.setState({
value: value, // ISO String, ex: "2016-11-19T12:00:00.000Z"
formattedValue: formattedValue // Formatted String, ex: "11/19/2016"
})
}
componentDidUpdate() {
// Access ISO String and formatted values from the DOM.
var hiddenInputElement = document.getElementById("example-datepicker");
console.log(hiddenInputElement.value); // ISO String, ex: "2016-11-19T12:00:00.000Z"
console.log(hiddenInputElement.getAttribute('data-formattedvalue')) // Formatted String, ex: "11/19/2016"
}
render() {
return (
<FormGroup>
<Label>My Date Picker</Label>
<DatePicker id = "example-datepicker"
value = {this.state.value}
onChange= {(v,f) => this.handleChange(v, f)} />
<FormText>Help</FormText>
</FormGroup>
)
}
}
reactstrap-date-picker
's public component.
ISO date string representing the current value.
- Optional
- Type:
string
. - Example:
"2016-05-19T12:00:00.000Z"
.
ISO date string representing the default value. Cannot be set with 'value'.
- Optional
- Type:
string
- Example:
"2016-05-19T12:00:00.000Z"
Date format. Any combination of DD, MM, YYYY and separator.
- Optional
- Type:
string
- Examples:
"MM/DD/YYYY"
,"YYYY/MM/DD"
,"MM-DD-YYYY"
, or"DD MM YYYY"
Makes the calendar's week to start on a specified day. 0 = Sunday, 1 = Monday, etc.
- Optional
- Type:
number
- Example:
4
ISO date string to set the lowest allowable date value.
- Optional
- Type:
string
- Example:
"2016-05-19T12:00:00.000Z"
ISO date string to set the highest allowable date value.
- Optional
- Type:
string
- Example:
"2016-05-19T12:00:00.000Z"
Change callback function.
- Optional
- Type:
function
- Callback Arguments:
value
: ISO date string representing the selected value.- Type:
String
- Example:
"2016-05-19T12:00:00.000Z"
- Type:
formattedValue
: String representing the formatted value as defined by thedateFormat
property.- Type:
String
- Example:
"05/19/2016"
- Type:
Focus callback function.
- Optional
- Type:
function
- Callback Arguments:
event
: Focus event.- Type:
Event
- Type:
Blur callback function.
- Optional
- Type:
function
- Callback Arguments:
event
: Blur event.- Type:
Event
- Type:
Defines what happens when clear button is clicked.
- Optional
- Type:
function
Form control is the shown HTML input, and you can customize it.
Style object passed to the Form Control input element.
- Optional
- Type:
object
- Example:
{width: "100%"}
Class name passed to the Form Control input element.
- Optional
- Type:
string
- Example:
example-class
autoComplete
attribute passed to the Form Control input element.
- Optional
- Type:
string
- Example:
off
Whether or not component starts with focus.
- Optional
- Type:
bool
- Example:
false
Whether or not component is disabled.
- Optional
- Type:
bool
- Example:
false
Size of the input
- Optional
- Type:
string
- Examples:
lg
,sm
, ...
You can also override it completely and pass your own component:
Overwrite the default Form Control component with your own component.
- Optional
- Type:
React.Component
- Example:
<CustomControl />
Toggles the visibility of the clearButton
- Optional
- Type:
bool
- Default:
false
Character or component to use for the clear button.
- Optional
- Type:
string
orReactClass
- Default:
"×"
You can also customize the popup's Calendar:
Character or component to use for the calendar's previous button.
- Optional
- Type:
string
orReactClass
- Default:
"<"
Character or component to use for the calendar's next button.
- Optional
- Type:
string
orReactClass
- Default:
">"
CSS padding value for calendar date cells.
- Optional
- Type:
string
- Default:
"5px"
Array of day names to use in the calendar. Starting on Sunday.
- Optional
- Type:
array
- Default:
['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
Array of month names to use in the calendar.
- Optional
- Type:
array
- Default:
['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
Overlay placement for the popover calendar.
- Optional
- Type:
string
orfunction
- Default:
"top"
Overlay container for the popover calendar. When placing the date-picker in a scrolling container, set this prop to some ancestor of the scrolling container.
- Optional
- Type: A DOM element or a component
- Example:
document.body
Toggles the visibility of the today-button.
- Optional
- Type:
boolean
- Default:
false
Label for the today-button
- Optional
- Type:
string
- Default:
"Today"
You can also customize reactstrap-date-picker
trough element's id
or class
attributes.
reactstrap-date-picker
renders several elements, all contained within a reactstrap InputGroup.
Such elements will have its unique id
attribute, plus reactstrap-date-picker
custom class
names (prefixed by rdp-*
).
The rendered DOM structure seems like this:
<div class="input-group rdp-input-group" id="rdp-input-group-SUFFIX">
<input class="form-control rdp-form-control" id="props.formControl.id or rdp-form-control-SUFFIX" />
<div class="rdp-overlay">
<div>
<div class="rdp-popover">
<div class="popover">
<div class="popover-inner">
<div class="popover-header">
<div class="rdp-header">
<div class="rdp-header-previous-wrapper"/>
<span/>
<div class="rdp-header-next-wrapper"/>
</div>
</div>
<div class="popover-body">
<table class="rdp-calendar">
</table>
</div>
</div>
<span class="arrow">
</div>
</div>
</div>
</div>
<input class="rdp-hidden" id="props.id or rdp-hidden-SUFFIX" />
<div class="input-group-addon rdp-addon">
<span class="input-group-text">
</div>
</div>
This SUFFIX
is:
· props.name
· if props.name
is not passed, then use props.id
· if props.id
is not passed, then take a global counter of active reactstrap-date-picker
instances
So, the idea is, depending on your needs:
· if you don't need handle id
s at all, reactstrap-date-picker
will render unique id
with no problem
· if you need a basic id
usage, for example accessing the reactstrap-date-picker
's value from the DOM, then
you just have to pass props.id
and get the value from the element with that id
· if you will perform more complex operations, then use props.name
or props.id
, and pay attention to the
previous DOM structure and the SUFFIX
presences
npm run demo
And visit http://localhost:3002 on your browser
npm run test