Skip to content

Commit 6abcc6f

Browse files
committed
Default values.
1 parent c45bf39 commit 6abcc6f

File tree

6 files changed

+62
-6
lines changed

6 files changed

+62
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ DatePicker component. Renders as a [React-Bootstrap InputGroup](https://react-bo
8484
* **Optional**
8585
* **Type:** `string`
8686
* **Example:** `"2016-05-19T12:00:00.000Z"`
87+
* `defaultValue` - ISO date string representing the default value. Cannot be set with 'value'.
88+
* **Optional**
89+
* **Type:** `string`
90+
* **Example:** `"2016-05-19T12:00:00.000Z"`
8791
* `autoFocus` - Whether or not component starts with focus.
8892
* **Optional**
8993
* **Type:** `bool`

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ var Calendar = _react2.default.createClass({
9494
var year = this.props.displayDate.getFullYear();
9595
var month = this.props.displayDate.getMonth();
9696
var firstDay = new Date(year, month, 1);
97-
var startingDay = this.props.weekStartsOnMonday ? firstDay.getDay() - 1 : firstDay.getDay();
97+
var startingDay = this.props.weekStartsOnMonday ? firstDay.getDay() === 0 ? 6 : firstDay.getDay() - 1 : firstDay.getDay();
9898
var monthLength = daysInMonth[month];
9999
if (month == 1) {
100100
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"picker"
1212
],
1313
"main": "lib/index.js",
14-
"version": "3.8.0",
14+
"version": "3.8.1",
1515
"description": "React-Bootstrap based date picker.",
1616
"directories": {
1717
"test": "test"
@@ -41,6 +41,7 @@
4141
"karma-coverage": "*",
4242
"karma-coveralls": "*",
4343
"karma-firefox-launcher": "*",
44+
"karma-safari-launcher": "*",
4445
"karma-mocha": "*",
4546
"karma-sourcemap-loader": "*",
4647
"karma-webpack": "*",

src/index.jsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ const Calendar = React.createClass({
116116
export default React.createClass({
117117
displayName: "DatePicker",
118118
propTypes: {
119+
defaultValue: React.PropTypes.string,
119120
value: React.PropTypes.string,
120121
cellPadding: React.PropTypes.string,
121122
placeholder: React.PropTypes.string,
@@ -141,7 +142,6 @@ export default React.createClass({
141142
]),
142143
calendarPlacement: React.PropTypes.string,
143144
dateFormat: React.PropTypes.string // 'MM/DD/YYYY', 'DD/MM/YYYY', 'YYYY/MM/DD', 'DD-MM-YYYY'
144-
145145
},
146146
getDefaultProps() {
147147
const language = typeof window !== "undefined" && window.navigator ? (window.navigator.userLanguage || window.navigator.language || '').toLowerCase() : '';
@@ -163,7 +163,10 @@ export default React.createClass({
163163
}
164164
},
165165
getInitialState() {
166-
var state = this.makeDateValues(this.props.value);
166+
if(this.props.value && this.props.defaultValue) {
167+
throw new Error("Conflicting DatePicker properties 'value' and 'defaultValue'");
168+
}
169+
var state = this.makeDateValues(this.props.value || this.props.defaultValue);
167170
if(this.props.weekStartsOnMonday) {
168171
state.dayLabels = this.props.dayLabels.slice(1).concat(this.props.dayLabels.slice(0,1))
169172
} else {

test/core.test.jsx

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ describe("Date Picker", function() {
650650
checkMonthAndYear(date.toISOString());
651651
}
652652
}
653+
ReactDOM.unmountComponentAtNode(container);
653654
}));
654655
it("should display the correct day of the week in the calendar when starting on Monday.", co.wrap(function *(){
655656
const id = UUID.v4();
@@ -697,6 +698,52 @@ describe("Date Picker", function() {
697698
checkMonthAndYear(date.toISOString());
698699
}
699700
}
701+
ReactDOM.unmountComponentAtNode(container);
702+
}));
703+
it("should set a default value", co.wrap(function *(){
704+
const id = UUID.v4();
705+
const defaultValue = new Date().toISOString();
706+
let value = null;
707+
let formattedValue = null;
708+
const App = React.createClass({
709+
handleChange: function(newValue, newFormattedValue){
710+
value = newValue;
711+
formattedValue = newFormattedValue;
712+
},
713+
render: function(){
714+
return <div>
715+
<DatePicker defaultValue={defaultValue} id={id} onChange={this.handleChange} />
716+
</div>;
717+
}
718+
});
719+
yield new Promise(function(resolve, reject){
720+
ReactDOM.render(<App />, container, resolve);
721+
});
722+
const hiddenInputElement = document.getElementById(id);
723+
assertIsoStringsHaveSameDate(hiddenInputElement.value, defaultValue);
724+
assert.equal(value, null);
725+
const inputElement = document.querySelector("input.form-control");
726+
TestUtils.Simulate.change(inputElement);
727+
assert.notEqual(value, null);
728+
ReactDOM.unmountComponentAtNode(container);
729+
}));
730+
it("should error if value and default value are both set.", co.wrap(function *(){
731+
const value = new Date().toISOString();
732+
const App = React.createClass({
733+
render: function(){
734+
return <div>
735+
<DatePicker value={value} defaultValue={value} />
736+
</div>;
737+
}
738+
});
739+
try {
740+
yield new Promise(function(resolve, reject){
741+
ReactDOM.render(<App />, container, resolve);
742+
});
743+
throw new Error("Value and default value should not be set simultaneously");
744+
} catch (e) {
745+
assert(e.message.indexOf("Conflicting") !== -1)
746+
}
747+
ReactDOM.unmountComponentAtNode(container);
700748
}));
701-
702749
});

test/karma.conf.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = function (karma) {
99
plugins: [
1010
'karma-firefox-launcher',
1111
'karma-chrome-launcher',
12+
'karma-safari-launcher',
1213
'karma-mocha',
1314
'karma-coveralls',
1415
'karma-coverage',
@@ -127,7 +128,7 @@ module.exports = function (karma) {
127128
return key.indexOf("bs_") !== -1;
128129
});
129130
} else {
130-
options.browsers = ['Chrome', 'Firefox'];
131+
options.browsers = ['Chrome', 'Firefox', 'Safari'];
131132
}
132133
if(process.env.COVERALLS_REPO_TOKEN) {
133134
options.reporters.push('coveralls');

0 commit comments

Comments
 (0)