Skip to content

Commit cce0aac

Browse files
marioramvamartijnrusschen
authored andcommitted
ShortMonth on MonthDropdown (Hacker0x01#1186)
* ShortMonth on MonthDropdown For this to work I added prop useShortMonthInDropdown in DatePicker & MonthDropdown components Also added the function getMonthShortInLocale in date_utils and the conditional in Calendar component Finally added a className for selected option on both Month & Year Dropdown selected option. This only for css customization, because can't format parent based on child options. * Added ShortMonth for MonthDropdown * Added missing prop useShortMonthInDropdown in Calendar * Added Example Shor Month * Added test for useShortMonthInDropdown prop
1 parent 29bfed5 commit cce0aac

File tree

9 files changed

+76
-16
lines changed

9 files changed

+76
-16
lines changed

docs-site/src/example_components.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import DateRange from './examples/date_range'
2424
import TabIndex from './examples/tab_index'
2525
import YearDropdown from './examples/year_dropdown'
2626
import MonthDropdown from './examples/month_dropdown'
27+
import MonthDropdownShort from './examples/month_dropdown_short'
2728
import YearSelectDropdown from './examples/year_select_dropdown'
2829
import Today from './examples/today'
2930
import TimeZoneDate from './examples/timezone_date'
@@ -168,6 +169,10 @@ export default class exampleComponents extends React.Component {
168169
title: 'Month dropdown',
169170
component: <MonthDropdown />
170171
},
172+
{
173+
title: 'Month dropdown short month',
174+
component: <MonthDropdownShort />
175+
},
171176
{
172177
title: 'Year select dropdown',
173178
component: <YearSelectDropdown />
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from 'react'
2+
import DatePicker from 'react-datepicker'
3+
import moment from 'moment'
4+
5+
export default class MonthDropdownShort extends React.Component {
6+
constructor (props) {
7+
super(props)
8+
this.state = {
9+
startDate: moment()
10+
}
11+
}
12+
13+
handleChange = (date) => {
14+
this.setState({
15+
startDate: date
16+
})
17+
}
18+
19+
render () {
20+
return <div className="row">
21+
<pre className="column example__code">
22+
<code className="jsx">{`
23+
<DatePicker
24+
selected={this.state.startDate}
25+
onChange={this.handleChange}
26+
showMonthDropdown
27+
useShortMonthInDropdown
28+
/>
29+
`}
30+
</code>
31+
</pre>
32+
<div className="column">
33+
<DatePicker
34+
selected={this.state.startDate}
35+
onChange={this.handleChange}
36+
showMonthDropdown
37+
useShortMonthInDropdown />
38+
</div>
39+
</div>
40+
}
41+
}

src/calendar.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ export default class Calendar extends React.Component {
9595
utcOffset: PropTypes.number,
9696
weekLabel: PropTypes.string,
9797
yearDropdownItemNumber: PropTypes.number,
98-
setOpen: PropTypes.func
98+
setOpen: PropTypes.func,
99+
useShortMonthInDropdown: PropTypes.bool
99100
};
100101

101102
static get defaultProps() {
@@ -345,7 +346,8 @@ export default class Calendar extends React.Component {
345346
locale={this.props.locale}
346347
dateFormat={this.props.dateFormat}
347348
onChange={this.changeMonth}
348-
month={getMonth(this.state.date)}/>
349+
month={getMonth(this.state.date)}
350+
useShortMonthInDropdown={this.props.useShortMonthInDropdown} />
349351
);
350352
};
351353

src/date_utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ export function getMonthInLocale(locale, date, format) {
324324
return locale.months(date, format);
325325
}
326326

327+
export function getMonthShortInLocale (locale, date) {
328+
return locale.monthsShort(date)
329+
}
330+
327331
// ** Utils for some components **
328332

329333
export function isDayDisabled(

src/index.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ export default class DatePicker extends React.Component {
122122
timeIntervals: PropTypes.number,
123123
minTime: PropTypes.object,
124124
maxTime: PropTypes.object,
125-
excludeTimes: PropTypes.array
125+
excludeTimes: PropTypes.array,
126+
useShortMonthInDropdown: PropTypes.bool
126127
};
127128

128129
static get defaultProps() {
@@ -483,6 +484,7 @@ export default class DatePicker extends React.Component {
483484
inline={this.props.inline}
484485
peekNextMonth={this.props.peekNextMonth}
485486
showMonthDropdown={this.props.showMonthDropdown}
487+
useShortMonthInDropdown={this.props.useShortMonthInDropdown}
486488
showWeekNumbers={this.props.showWeekNumbers}
487489
showYearDropdown={this.props.showYearDropdown}
488490
withPortal={this.props.withPortal}

src/month_dropdown.jsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export default class MonthDropdown extends React.Component {
1212
locale: PropTypes.string,
1313
dateFormat: PropTypes.string.isRequired,
1414
month: PropTypes.number.isRequired,
15-
onChange: PropTypes.func.isRequired
15+
onChange: PropTypes.func.isRequired,
16+
useShortMonthInDropdown: PropTypes.bool
1617
};
1718

1819
state = {
@@ -79,14 +80,12 @@ export default class MonthDropdown extends React.Component {
7980
dropdownVisible: !this.state.dropdownVisible
8081
});
8182

82-
render() {
83-
const localeData = utils.getLocaleDataForLocale(this.props.locale);
84-
const monthNames = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map(M =>
85-
utils.getMonthInLocale(
86-
localeData,
87-
utils.newDate({ M }),
88-
this.props.dateFormat
89-
)
83+
render () {
84+
const localeData = utils.getLocaleDataForLocale(this.props.locale)
85+
const monthNames = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map(
86+
this.props.useShortMonthInDropdown
87+
? (M) => utils.getMonthShortInLocale(localeData, utils.newDate({M}))
88+
: (M) => utils.getMonthInLocale(localeData, utils.newDate({M}), this.props.dateFormat)
9089
);
9190

9291
let renderedDropdown;

src/month_dropdown_options.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ export default class MonthDropdownOptions extends React.Component {
1111

1212
renderOptions = () => {
1313
return this.props.monthNames.map((month, i) => (
14-
<div
15-
className="react-datepicker__month-option"
14+
<div className={this.props.month === i ? 'react-datepicker__month-option --selected_month' : 'react-datepicker__month-option'}
1615
key={month}
1716
ref={month}
1817
onClick={this.onChange.bind(this, i)}>

src/year_dropdown_options.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ export default class YearDropdownOptions extends React.Component {
5454
renderOptions = () => {
5555
var selectedYear = this.props.year;
5656
var options = this.state.yearsList.map(year => (
57-
<div
58-
className="react-datepicker__year-option"
57+
<div className={selectedYear === year ? 'react-datepicker__year-option --selected_year' : 'react-datepicker__year-option'}
5958
key={year}
6059
ref={year}
6160
onClick={this.onChange.bind(this, year)}>

test/month_dropdown_test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ describe("MonthDropdown", () => {
160160
"December"
161161
]);
162162
});
163+
// Short Month Names
164+
it("renders month options with short name and default locale", () => {
165+
monthDropdown = getMonthDropdown({dropdownMode: "select", useShortMonthInDropdown: true})
166+
var options = monthDropdown.find("option")
167+
expect(options.map(o => o.text())).to.eql([
168+
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
169+
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
170+
]);
171+
});
163172

164173
// Failing on Travis CI.
165174
xit("renders month options with specified locale", () => {

0 commit comments

Comments
 (0)