Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated proposal: Use moment defaults and preserve global settings. #257

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 16 additions & 19 deletions src/calendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var Calendar = React.createClass({
mixins: [require("react-onclickoutside")],

propTypes: {
weekdays: React.PropTypes.array.isRequired,
weekdays: React.PropTypes.array,
locale: React.PropTypes.string,
moment: React.PropTypes.func.isRequired,
dateFormat: React.PropTypes.string.isRequired,
Expand All @@ -22,7 +22,7 @@ var Calendar = React.createClass({
endDate: React.PropTypes.object,
excludeDates: React.PropTypes.array,
includeDates: React.PropTypes.array,
weekStart: React.PropTypes.string.isRequired
weekStart: React.PropTypes.string
},

handleClickOutside() {
Expand All @@ -31,7 +31,7 @@ var Calendar = React.createClass({

getInitialState() {
return {
date: new DateUtil(this.props.selected).safeClone(this.props.moment())
date: this.localizeDateUtil(new DateUtil(this.props.selected).safeClone(this.props.moment()))
};
},

Expand All @@ -41,31 +41,24 @@ var Calendar = React.createClass({
};
},

componentWillMount() {
this.initializeMomentLocale();
},

componentWillReceiveProps(nextProps) {
if (nextProps.selected === null) { return; }

// When the selected date changed
if (nextProps.selected !== this.props.selected) {
this.setState({
date: new DateUtil(nextProps.selected).clone()
date: this.localizeDateUtil(new DateUtil(nextProps.selected).clone())
});
}
},

initializeMomentLocale() {
var weekdays = this.props.weekdays.slice(0);
weekdays = weekdays.concat(weekdays.splice(0, this.props.weekStart));
localizeDateUtil(dateUtil) {
var thisMoment = dateUtil.moment();

this.props.moment.locale(this.props.locale, {
week: {
dow: this.props.weekStart
},
weekdaysMin: weekdays
});
if (this.props.locale) { thisMoment.locale(this.props.locale); }
if (this.props.weekStart) { thisMoment._locale._week.dow = this.props.weekStart; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this line is problematic. Modifying _locale touches the global locale, which we want to avoid. I found this out by trying to change _locale._weekdaysMin in one picker and seeing that it affected all pickers.


return dateUtil;
},

increaseMonth() {
Expand Down Expand Up @@ -151,7 +144,11 @@ var Calendar = React.createClass({
},

header() {
return this.props.moment.weekdaysMin().map(function(day, key) {
var localeData = this.state.date.moment().localeData();
var weekdays = localeData._weekdaysMin.slice(0);
weekdays = weekdays.concat(weekdays.splice(0, localeData._week.dow));

return weekdays.map(function(day, key) {
return <div className="datepicker__day" key={key}>{day}</div>;
});
},
Expand All @@ -165,7 +162,7 @@ var Calendar = React.createClass({
onClick={this.decreaseMonth}>
</a>
<h2 className="datepicker__current-month">
{this.state.date.localeFormat(this.props.locale, this.props.dateFormat)}
{this.state.date.localeFormat(locale, this.props.dateFormat)}
</h2>
<YearDropdown
onChange={this.changeYear}
Expand Down
2 changes: 0 additions & 2 deletions src/datepicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ var DatePicker = React.createClass({

getDefaultProps() {
return {
weekdays: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
locale: "en",
dateFormatCalendar: "MMMM",
moment: moment,
onChange() {},
Expand Down