Skip to content

Commit a1d51de

Browse files
committed
2 parents 51e7393 + da806bf commit a1d51de

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ DatePicker component. Renders as a [React-Bootstrap InputGroup](https://react-bo
132132
* **Optional**
133133
* **Type:** `string`
134134
* **Example:** `"top"`
135+
* `weekStartsOnMonday` - Makes the calendar's weel to start on Monday.
136+
* **Optional**
137+
* **Type:** `boolean`
138+
* **Example:** `true`
135139

136140
## Tests
137141

example/app.jsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,28 @@ const App = React.createClass({
6060
</Col>
6161
</Row>
6262
<Row>
63-
<Col xs={12}>
63+
<Col xs={6}>
6464
<h2>Blur and Focus Events</h2>
6565
</Col>
66+
<Col xs={6}>
67+
<h2>Week Starts on Monday</h2>
68+
</Col>
6669
</Row>
6770
<Row>
6871
<Col sm={6}>
6972
<FormGroup>
7073
<ControlLabel>{this.state.focused ? "Focused" : "Blurred"}</ControlLabel>
71-
<DatePicker onChange={this.handleChange} placeholder="Placeholder" value={this.state.date} onFocus={() => {this.setState({focused: true})}} onBlur={() => {this.setState({focused: false})}} />
74+
<DatePicker onChange={this.handleChange} placeholder="Placeholder" value={this.state.date} onFocus={() => {this.setState({focused: true})}} onBlur={() => {this.setState({focused: false})}} />
7275
<HelpBlock>This is {this.state.focused ? "focused" : "blurred"}.</HelpBlock>
7376
</FormGroup>
7477
</Col>
78+
<Col sm={6}>
79+
<FormGroup>
80+
<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})}} />
82+
<HelpBlock>Help</HelpBlock>
83+
</FormGroup>
84+
</Col>
7585
</Row>
7686
<Row>
7787
<Col xs={12}>

src/index.jsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
import React from 'react';
44
import ReactDOM from 'react-dom';
5-
import FormControl from 'react-bootstrap/lib/FormControl';
6-
import InputGroup from 'react-bootstrap/lib/InputGroup';
7-
import Popover from 'react-bootstrap/lib/Popover';
8-
import Button from 'react-bootstrap/lib/Button';
9-
import Overlay from 'react-bootstrap/lib/Overlay';
5+
import { Button, FormControl, InputGroup, Overlay, Popover } from 'react-bootstrap';
106

117
const CalendarHeader = React.createClass({
128
displayName: "DatePickerHeader",
@@ -51,7 +47,8 @@ const Calendar = React.createClass({
5147
displayDate: React.PropTypes.object.isRequired,
5248
onChange: React.PropTypes.func.isRequired,
5349
dayLabels: React.PropTypes.array.isRequired,
54-
cellPadding: React.PropTypes.string.isRequired
50+
cellPadding: React.PropTypes.string.isRequired,
51+
weekStartsOnMonday: React.PropTypes.bool
5552
},
5653
handleClick(day) {
5754
const newSelectedDate = new Date(this.props.displayDate);
@@ -73,7 +70,7 @@ const Calendar = React.createClass({
7370
const year = this.props.displayDate.getFullYear();
7471
const month = this.props.displayDate.getMonth();
7572
const firstDay = new Date(year, month, 1);
76-
const startingDay = firstDay.getDay();
73+
const startingDay = this.props.weekStartsOnMonday ? (firstDay.getDay() + 1) : firstDay.getDay();
7774
let monthLength = daysInMonth[month];
7875
if (month == 1) {
7976
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
@@ -126,6 +123,7 @@ export default React.createClass({
126123
monthLabels: React.PropTypes.array,
127124
onChange: React.PropTypes.func,
128125
onClear: React.PropTypes.func,
126+
weekStartsOnMonday: React.PropTypes.bool,
129127
clearButtonElement: React.PropTypes.oneOfType([
130128
React.PropTypes.string,
131129
React.PropTypes.object
@@ -144,9 +142,10 @@ export default React.createClass({
144142
getDefaultProps() {
145143
const language = typeof window !== "undefined" && window.navigator ? (window.navigator.userLanguage || window.navigator.language || '').toLowerCase() : '';
146144
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'];
147146
return {
148147
cellPadding: "5px",
149-
dayLabels: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
148+
dayLabels,
150149
monthLabels: ['January', 'February', 'March', 'April',
151150
'May', 'June', 'July', 'August', 'September',
152151
'October', 'November', 'December'],
@@ -383,7 +382,7 @@ export default React.createClass({
383382
return <InputGroup ref="inputGroup" bsClass={this.props.bsClass} bsSize={this.props.bsSize} id={this.props.id ? this.props.id + "_group" : null}>
384383
<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}>
385384
<Popover id="calendar" title={calendarHeader}>
386-
<Calendar cellPadding={this.props.cellPadding} selectedDate={this.state.selectedDate} displayDate={this.state.displayDate} onChange={this.onChangeDate} dayLabels={this.props.dayLabels} />
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} />
387386
</Popover>
388387
</Overlay>
389388
<div ref="overlayContainer" />

test/core.test.jsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,4 +389,19 @@ describe("Date Picker", function() {
389389
assert.equal(yyyy_mm_dd_inputElement.value, "1999/12/31");
390390
ReactDOM.unmountComponentAtNode(container);
391391
}));
392+
it("week should start on Monday.", co.wrap(function *(){
393+
const id = UUID.v4();
394+
const App = React.createClass({
395+
render: function(){
396+
return <div>
397+
<DatePicker id={id} weekStartsOnMonday />
398+
</div>;
399+
}
400+
});
401+
yield new Promise(function(resolve, reject){
402+
ReactDOM.render(<App />, container, resolve);
403+
});
404+
assert.equal(document.querySelector("table thead tr:first-child td small").innerHTML, "Mon");
405+
ReactDOM.unmountComponentAtNode(container);
406+
}));
392407
});

0 commit comments

Comments
 (0)