-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Use moment locales for customization #277
Merged
rafeememon
merged 6 commits into
Hacker0x01:master
from
rafeememon:feature/moment-locales
Feb 14, 2016
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7ceaa23
Localize moments locally without modifying global state
rafeememon a3dd2a9
Update readme for localization
rafeememon 22fd8e1
Fix weekend day check
rafeememon daa0fc3
Update examples for locales
rafeememon 306f56b
Add tests for localization
rafeememon 15cefbf
Add localization to date input
rafeememon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,8 +20,7 @@ var Calendar = React.createClass({ | |
mixins: [require("react-onclickoutside")], | ||
|
||
propTypes: { | ||
weekdays: React.PropTypes.array.isRequired, | ||
locale: React.PropTypes.string.isRequired, | ||
locale: React.PropTypes.string, | ||
moment: React.PropTypes.func.isRequired, | ||
dateFormat: React.PropTypes.string.isRequired, | ||
onSelect: React.PropTypes.func.isRequired, | ||
|
@@ -33,7 +32,6 @@ var Calendar = React.createClass({ | |
excludeDates: React.PropTypes.array, | ||
includeDates: React.PropTypes.array, | ||
filterDate: React.PropTypes.func, | ||
weekStart: React.PropTypes.string.isRequired, | ||
showYearDropdown: React.PropTypes.bool | ||
}, | ||
|
||
|
@@ -43,38 +41,20 @@ var Calendar = React.createClass({ | |
|
||
getInitialState() { | ||
return { | ||
date: getDateInView(this.props) | ||
date: this.localizeMoment(getDateInView(this.props)) | ||
}; | ||
}, | ||
|
||
getDefaultProps() { | ||
return { | ||
weekStart: "1" | ||
}; | ||
}, | ||
|
||
componentWillMount() { | ||
this.initializeMomentLocale(); | ||
}, | ||
|
||
componentWillReceiveProps(nextProps) { | ||
if (nextProps.selected && !isSameDay(nextProps.selected, this.props.selected)) { | ||
this.setState({ | ||
date: nextProps.selected | ||
date: this.localizeMoment(nextProps.selected) | ||
}); | ||
} | ||
}, | ||
|
||
initializeMomentLocale() { | ||
var weekdays = this.props.weekdays.slice(0); | ||
weekdays = weekdays.concat(weekdays.splice(0, this.props.weekStart)); | ||
|
||
this.props.moment.locale(this.props.locale, { | ||
week: { | ||
dow: this.props.weekStart | ||
}, | ||
weekdaysMin: weekdays | ||
}); | ||
localizeMoment(date) { | ||
return date.clone().locale(this.props.locale || this.props.moment.locale()); | ||
}, | ||
|
||
increaseMonth() { | ||
|
@@ -100,8 +80,14 @@ var Calendar = React.createClass({ | |
}, | ||
|
||
header() { | ||
return this.props.moment.weekdaysMin().map(function(day, key) { | ||
return <div className="datepicker__day" key={key}>{day}</div>; | ||
const startOfWeek = this.state.date.clone().startOf("week"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the best I could come up with without digging into moment internal fields =( |
||
return [0, 1, 2, 3, 4, 5, 6].map(offset => { | ||
const day = startOfWeek.clone().add(offset, "days"); | ||
return ( | ||
<div key={offset} className="datepicker__day"> | ||
{day.localeData().weekdaysMin(day)} | ||
</div> | ||
); | ||
}); | ||
}, | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,8 +44,8 @@ var Day = React.createClass({ | |
}, | ||
|
||
isWeekend() { | ||
const weekday = this.props.day.weekday(); | ||
return weekday === 5 || weekday === 6; | ||
const weekday = this.props.day.day(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hooray for tests! |
||
return weekday === 0 || weekday === 6; | ||
}, | ||
|
||
isOutsideMonth() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Localizing the moment as soon as it's passed in allows the localization to be passed to weekdays and week start automatically.