Skip to content

Commit

Permalink
Merge pull request #350 from Hacker0x01/today
Browse files Browse the repository at this point in the history
Today Button
  • Loading branch information
martijnrusschen committed Feb 12, 2016
2 parents bdb72e8 + 5d61bdc commit ce29276
Showing 8 changed files with 90 additions and 3 deletions.
5 changes: 5 additions & 0 deletions docs-site/src/example_components.jsx
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ import Placement from "./examples/placement";
import DateRange from "./examples/date_range";
import TabIndex from "./examples/tab_index";
import YearDropdown from "./examples/year_dropdown";
import Today from "./examples/today";

import "react-datepicker/dist/react-datepicker.css";
import "./style.scss";
@@ -51,6 +52,10 @@ export default React.createClass({
title: "Custom class name",
component: <CustomClassName />
},
{
title: "Today button",
component: <Today />
},
{
title: "Placeholder text",
component: <PlaceholderText />
38 changes: 38 additions & 0 deletions docs-site/src/examples/today.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from "react";
import DatePicker from "react-datepicker";
import moment from "moment";

export default React.createClass({
displayName: "Default",

getInitialState() {
return {
startDate: moment()
};
},

handleChange(date) {
this.setState({
startDate: date
});
},

render() {
return <div className="row">
<pre className="column example__code">
<code className="jsx">
{"<DatePicker"}<br />
{"showTodayButton={'Vandaag'}"}<br />
{"selected={this.state.startDate}"}<br />
{"onChange={this.handleChange} />"}
</code>
</pre>
<div className="column">
<DatePicker
todayButton={"Vandaag"}
selected={this.state.startDate}
onChange={this.handleChange} />
</div>
</div>;
}
});
5 changes: 5 additions & 0 deletions docs/datepicker.md
Original file line number Diff line number Diff line change
@@ -96,6 +96,11 @@ defaultValue: `[
]`


### `todayButton`

type: `string`


### `weekStart`

type: `string`
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ app.use(require("webpack-dev-middleware")(compiler, {

app.use(require("webpack-hot-middleware")(compiler));

app.use(express.static("docs"));
app.use(express.static("docs-site"));

app.listen(8080, "localhost", function(err) {
if (err) {
15 changes: 15 additions & 0 deletions src/calendar.jsx
Original file line number Diff line number Diff line change
@@ -128,6 +128,20 @@ var Calendar = React.createClass({
);
},

renderTodayButton() {
const { moment, onSelect } = this.props;

if (!this.props.todayButton) {
return;
}

return (
<div className="datepicker__today-button" onClick={() => onSelect(moment())}>
{this.props.todayButton}
</div>
);
},

render() {
return (
<div className="datepicker">
@@ -156,6 +170,7 @@ var Calendar = React.createClass({
selected={this.props.selected}
startDate={this.props.startDate}
endDate={this.props.endDate} />
{this.renderTodayButton()}
</div>
);
}
6 changes: 4 additions & 2 deletions src/datepicker.jsx
Original file line number Diff line number Diff line change
@@ -28,7 +28,8 @@ var DatePicker = React.createClass({
onBlur: React.PropTypes.func,
onFocus: React.PropTypes.func,
tabIndex: React.PropTypes.number,
filterDate: React.PropTypes.func
filterDate: React.PropTypes.func,
todayButton: React.PropTypes.string
},

getDefaultProps() {
@@ -124,7 +125,8 @@ var DatePicker = React.createClass({
onClickOutside={this.handleCalendarClickOutside}
includeDates={this.props.includeDates}
weekStart={this.props.weekStart}
showYearDropdown={this.props.showYearDropdown} />;
showYearDropdown={this.props.showYearDropdown}
todayButton={this.props.todayButton} />;
},

renderClearButton() {
9 changes: 9 additions & 0 deletions src/stylesheets/datepicker.scss
Original file line number Diff line number Diff line change
@@ -295,3 +295,12 @@
top: 50%;
}
}

.datepicker__today-button {
background: $background-color;
border-top: 1px solid $border-color;
cursor: pointer;
text-align: center;
font-weight: bold;
padding: 5px 0;
}
13 changes: 13 additions & 0 deletions test/calendar_test.js
Original file line number Diff line number Diff line change
@@ -60,4 +60,17 @@ describe("Calendar", function() {
var yearReadView = TestUtils.findRenderedComponentWithType(calendar, YearDropdown);
expect(yearReadView).to.exist;
});

it("should not show the today button by default", function() {
var calendar = TestUtils.renderIntoDocument(getCalendar());
var todayButton = TestUtils.scryRenderedDOMComponentsWithClass(calendar, "datepicker__today-button");
expect(todayButton.length).to.equal(0);
});

it("should show the today button if toggled on", function() {
var calendar = TestUtils.renderIntoDocument(getCalendar({ todayButton: "Vandaag" }));
var todayButton = TestUtils.findRenderedDOMComponentWithClass(calendar, "datepicker__today-button");
expect(todayButton).to.exist;
expect(todayButton.textContent).to.equal("Vandaag");
});
});

0 comments on commit ce29276

Please sign in to comment.