Skip to content

Commit e33e002

Browse files
committed
Update week starting Monday code.
1 parent a1d51de commit e33e002

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

example/app.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const App = React.createClass({
7878
<Col sm={6}>
7979
<FormGroup>
8080
<ControlLabel>Week Starts on Monday</ControlLabel>
81-
<DatePicker onChange={this.handleChange} weekStartsOnMonday placeholder="Placeholder" value={this.state.date} onFocus={() => {this.setState({focused: true})}} onBlur={() => {this.setState({focused: false})}} />
81+
<DatePicker onChange={this.handleChange} weekStartsOnMonday placeholder="Placeholder" value={this.state.date} />
8282
<HelpBlock>Help</HelpBlock>
8383
</FormGroup>
8484
</Col>

lib/index.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,18 @@ exports.default = _react2.default.createClass({
245245
this.props.onChange(null);
246246
}
247247
},
248-
handleHide: function handleHide(e) {
248+
handleHide: function handleHide() {
249249
if (this.state.inputFocused) {
250250
return;
251251
}
252252
this.setState({
253253
focused: false
254254
});
255255
if (this.props.onBlur) {
256-
this.props.onBlur(e);
256+
var event = document.createEvent('CustomEvent');
257+
event.initEvent("Change Date", true, false);
258+
_reactDom2.default.findDOMNode(this.refs.hiddenInput).dispatchEvent(event);
259+
this.props.onBlur(event);
257260
}
258261
},
259262
handleKeyDown: function handleKeyDown(e) {
@@ -262,7 +265,10 @@ exports.default = _react2.default.createClass({
262265
focused: false
263266
});
264267
if (this.props.onBlur) {
265-
this.props.onBlur(e);
268+
var event = document.createEvent('CustomEvent');
269+
event.initEvent("Change Date", true, false);
270+
_reactDom2.default.findDOMNode(this.refs.hiddenInput).dispatchEvent(event);
271+
this.props.onBlur(event);
266272
}
267273
}
268274
},
@@ -275,7 +281,10 @@ exports.default = _react2.default.createClass({
275281
focused: true
276282
});
277283
if (this.props.onFocus) {
278-
this.props.onFocus(e);
284+
var event = document.createEvent('CustomEvent');
285+
event.initEvent("Change Date", true, false);
286+
_reactDom2.default.findDOMNode(this.refs.hiddenInput).dispatchEvent(event);
287+
this.props.onFocus(event);
279288
}
280289
},
281290
handleBlur: function handleBlur(e) {
@@ -400,6 +409,7 @@ exports.default = _react2.default.createClass({
400409
if (this.props.onBlur) {
401410
var event = document.createEvent('CustomEvent');
402411
event.initEvent("Change Date", true, false);
412+
_reactDom2.default.findDOMNode(this.refs.hiddenInput).dispatchEvent(event);
403413
this.props.onBlur(event);
404414
}
405415
if (this.props.onChange) {
@@ -439,7 +449,7 @@ exports.default = _react2.default.createClass({
439449
)
440450
),
441451
_react2.default.createElement('div', { ref: 'overlayContainer' }),
442-
_react2.default.createElement('input', { type: 'hidden', id: this.props.id, name: this.props.name, value: this.state.value || '' }),
452+
_react2.default.createElement('input', { ref: 'hiddenInput', type: 'hidden', id: this.props.id, name: this.props.name, value: this.state.value || '' }),
443453
_react2.default.createElement(_FormControl2.default, {
444454
onKeyDown: this.handleKeyDown,
445455
value: this.state.inputValue || '',

src/index.jsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const Calendar = React.createClass({
7070
const year = this.props.displayDate.getFullYear();
7171
const month = this.props.displayDate.getMonth();
7272
const firstDay = new Date(year, month, 1);
73-
const startingDay = this.props.weekStartsOnMonday ? (firstDay.getDay() + 1) : firstDay.getDay();
73+
const startingDay = this.props.weekStartsOnMonday ? (firstDay.getDay() - 1) : firstDay.getDay();
7474
let monthLength = daysInMonth[month];
7575
if (month == 1) {
7676
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
@@ -142,10 +142,9 @@ export default React.createClass({
142142
getDefaultProps() {
143143
const language = typeof window !== "undefined" && window.navigator ? (window.navigator.userLanguage || window.navigator.language || '').toLowerCase() : '';
144144
const dateFormat = !language || language === "en-us" ? 'MM/DD/YYYY' : 'DD/MM/YYYY';
145-
const dayLabels = this.props.weekStartsOnMonday ? ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] : ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
146145
return {
147146
cellPadding: "5px",
148-
dayLabels,
147+
dayLabels: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
149148
monthLabels: ['January', 'February', 'March', 'April',
150149
'May', 'June', 'July', 'August', 'September',
151150
'October', 'November', 'December'],
@@ -158,6 +157,11 @@ export default React.createClass({
158157
},
159158
getInitialState() {
160159
var state = this.makeDateValues(this.props.value);
160+
if(this.props.weekStartsOnMonday) {
161+
state.dayLabels = this.props.dayLabels.slice(1).concat(this.props.dayLabels.slice(0,1))
162+
} else {
163+
state.dayLabels = this.props.dayLabels;
164+
}
161165
state.focused = false;
162166
state.inputFocused = false;
163167
state.placeholder = this.props.placeholder || this.props.dateFormat;
@@ -382,7 +386,7 @@ export default React.createClass({
382386
return <InputGroup ref="inputGroup" bsClass={this.props.bsClass} bsSize={this.props.bsSize} id={this.props.id ? this.props.id + "_group" : null}>
383387
<Overlay rootClose={true} onHide={this.handleHide} show={this.state.focused} container={() => ReactDOM.findDOMNode(this.refs.overlayContainer)} target={() => ReactDOM.findDOMNode(this.refs.input)} placement={this.props.calendarPlacement} delayHide={200}>
384388
<Popover id="calendar" title={calendarHeader}>
385-
<Calendar cellPadding={this.props.cellPadding} selectedDate={this.state.selectedDate} displayDate={this.state.displayDate} onChange={this.onChangeDate} dayLabels={this.props.dayLabels} weekStartsOnMonday={this.props.weekStartsOnMonday} />
389+
<Calendar cellPadding={this.props.cellPadding} selectedDate={this.state.selectedDate} displayDate={this.state.displayDate} onChange={this.onChangeDate} dayLabels={this.state.dayLabels} weekStartsOnMonday={this.props.weekStartsOnMonday} />
386390
</Popover>
387391
</Overlay>
388392
<div ref="overlayContainer" />

test/core.test.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ describe("Date Picker", function() {
401401
yield new Promise(function(resolve, reject){
402402
ReactDOM.render(<App />, container, resolve);
403403
});
404+
const inputElement = document.querySelector("input.form-control");
405+
TestUtils.Simulate.focus(inputElement);
404406
assert.equal(document.querySelector("table thead tr:first-child td small").innerHTML, "Mon");
405407
ReactDOM.unmountComponentAtNode(container);
406408
}));

0 commit comments

Comments
 (0)