Skip to content

Commit e1e4a02

Browse files
authored
Merge pull request #728 from onlined/remove-unneeded-parameters
Remove unneeded parameters
2 parents d6e72d2 + 3736650 commit e1e4a02

5 files changed

Lines changed: 167 additions & 154 deletions

File tree

src/datetime/DateTime.js

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ export default class Datetime extends React.Component {
8181

8282
constructor( props ) {
8383
super( props );
84-
this.state = this.getInitialState( props );
84+
this.state = this.getInitialState();
8585
}
8686

8787
render() {
8888
return (
8989
<ClickableWrapper className={ this.getClassName() } onClickOut={ this._handleClickOutside }>
9090
{ this.renderInput() }
9191
<div className="rdtPicker">
92-
{ this.renderView( this.state.currentView, this._renderCalendar ) }
92+
{ this.renderView() }
9393
</div>
9494
</ClickableWrapper>
9595
);
@@ -122,14 +122,14 @@ export default class Datetime extends React.Component {
122122
);
123123
}
124124

125-
renderView( currentView, renderer ) {
125+
renderView() {
126126
if ( this.props.renderView ) {
127-
return this.props.renderView( currentView, () => renderer(currentView) );
127+
return this.props.renderView( this.state.currentView, this._renderCalendar );
128128
}
129-
return renderer( this.state.currentView );
129+
return this._renderCalendar();
130130
}
131131

132-
_renderCalendar = currentView => {
132+
_renderCalendar = () => {
133133
const props = this.props;
134134
const state = this.state;
135135

@@ -145,7 +145,7 @@ export default class Datetime extends React.Component {
145145

146146
// Probably updateOn, updateSelectedDate and setDate can be merged in the same method
147147
// that would update viewDate or selectedDate depending on the view and the dateFormat
148-
switch ( currentView ) {
148+
switch ( state.currentView ) {
149149
case viewModes.YEARS:
150150
// Used viewProps
151151
// { viewDate, selectedDate, renderYear, isValidDate, navigate, showView, updateDate }
@@ -173,31 +173,32 @@ export default class Datetime extends React.Component {
173173
}
174174
}
175175

176-
getInitialState( p ) {
177-
let props = p || this.props;
176+
getInitialState() {
177+
let props = this.props;
178178
let inputFormat = this.getFormat('datetime');
179179
let selectedDate = this.parseDate( props.value || props.initialValue, inputFormat );
180180

181-
this.checkTZ( props );
181+
this.checkTZ();
182182

183183
return {
184184
open: !props.input,
185-
currentView: props.initialViewMode || this.getInitialView( this.getFormat('date') ),
186-
viewDate: this.getInitialViewDate( props.initialViewDate, selectedDate, inputFormat ),
185+
currentView: props.initialViewMode || this.getInitialView(),
186+
viewDate: this.getInitialViewDate( selectedDate ),
187187
selectedDate: selectedDate && selectedDate.isValid() ? selectedDate : undefined,
188-
inputValue: this.getInitialInputValue( props, selectedDate, inputFormat )
188+
inputValue: this.getInitialInputValue( selectedDate )
189189
};
190190
}
191191

192-
getInitialViewDate( propDate, selectedDate, format ) {
192+
getInitialViewDate( selectedDate ) {
193+
const propDate = this.props.initialViewDate;
193194
let viewDate;
194195
if ( propDate ) {
195-
viewDate = this.parseDate( propDate, format );
196+
viewDate = this.parseDate( propDate, this.getFormat('datetime') );
196197
if ( viewDate && viewDate.isValid() ) {
197198
return viewDate;
198199
}
199200
else {
200-
this.log('The initialViewDated given "' + propDate + '" is not valid. Using current date instead.');
201+
log('The initialViewDated given "' + propDate + '" is not valid. Using current date instead.');
201202
}
202203
}
203204
else if ( selectedDate && selectedDate.isValid() ) {
@@ -212,9 +213,9 @@ export default class Datetime extends React.Component {
212213
return m;
213214
}
214215

215-
getInitialView( dateFormat ) {
216-
if ( !dateFormat ) return viewModes.TIME;
217-
return this.getUpdateOn( dateFormat );
216+
getInitialView() {
217+
const dateFormat = this.getFormat( 'date' );
218+
return dateFormat ? this.getUpdateOn( dateFormat ) : viewModes.TIME;
218219
}
219220

220221
parseDate(date, dateFormat) {
@@ -277,19 +278,21 @@ export default class Datetime extends React.Component {
277278
return viewModes.DAYS;
278279
}
279280

280-
getLocaleData( props ) {
281-
let p = props || this.props;
281+
getLocaleData() {
282+
let p = this.props;
282283
return this.localMoment( p.value || p.defaultValue || new Date() ).localeData();
283284
}
284285

285-
getDateFormat( locale ) {
286+
getDateFormat() {
287+
const locale = this.getLocaleData();
286288
let format = this.props.dateFormat;
287289
if ( format === true ) return locale.longDateFormat('L');
288290
if ( format ) return format;
289291
return '';
290292
}
291293

292-
getTimeFormat( locale ) {
294+
getTimeFormat() {
295+
const locale = this.getLocaleData();
293296
let format = this.props.timeFormat;
294297
if ( format === true ) {
295298
return locale.longDateFormat('LT');
@@ -299,15 +302,14 @@ export default class Datetime extends React.Component {
299302

300303
getFormat( type ) {
301304
if ( type === 'date' ) {
302-
return this.getDateFormat( this.getLocaleData() );
305+
return this.getDateFormat();
303306
}
304307
else if ( type === 'time' ) {
305-
return this.getTimeFormat( this.getLocaleData() );
308+
return this.getTimeFormat();
306309
}
307310

308-
let locale = this.getLocaleData();
309-
let dateFormat = this.getDateFormat( locale );
310-
let timeFormat = this.getTimeFormat( locale );
311+
let dateFormat = this.getDateFormat();
312+
let timeFormat = this.getTimeFormat();
311313
return dateFormat && timeFormat ? dateFormat + ' ' + timeFormat : (dateFormat || timeFormat );
312314
}
313315

@@ -437,10 +439,11 @@ export default class Datetime extends React.Component {
437439
return m;
438440
}
439441

440-
checkTZ( props ) {
441-
if ( props.displayTimeZone && !this.tzWarning && !moment.tz ) {
442+
checkTZ() {
443+
const { displayTimeZone } = this.props;
444+
if ( displayTimeZone && !this.tzWarning && !moment.tz ) {
442445
this.tzWarning = true;
443-
this.log('displayTimeZone prop with value "' + props.displayTimeZone + '" is used but moment.js timezone is not loaded.', 'error');
446+
log('displayTimeZone prop with value "' + displayTimeZone + '" is used but moment.js timezone is not loaded.', 'error');
444447
}
445448
}
446449

@@ -455,17 +458,18 @@ export default class Datetime extends React.Component {
455458
});
456459

457460
if ( needsUpdate ) {
458-
this.regenerateDates( this.props );
461+
this.regenerateDates();
459462
}
460463

461464
if ( thisProps.value && thisProps.value !== prevProps.value ) {
462465
this.setViewDate( thisProps.value );
463466
}
464467

465-
this.checkTZ( this.props );
468+
this.checkTZ();
466469
}
467470

468-
regenerateDates(props) {
471+
regenerateDates() {
472+
const props = this.props;
469473
let viewDate = this.state.viewDate.clone();
470474
let selectedDate = this.state.selectedDate && this.state.selectedDate.clone();
471475

@@ -500,12 +504,13 @@ export default class Datetime extends React.Component {
500504
return selectedDate && selectedDate.isValid() ? selectedDate : false;
501505
}
502506

503-
getInitialInputValue( props, selectedDate, inputFormat ) {
507+
getInitialInputValue( selectedDate ) {
508+
const props = this.props;
504509
if ( props.inputProps.value )
505510
return props.inputProps.value;
506511

507512
if ( selectedDate && selectedDate.isValid() )
508-
return selectedDate.format( inputFormat );
513+
return selectedDate.format( this.getFormat('datetime') );
509514

510515
if ( props.value && typeof props.value === 'string' )
511516
return props.value;
@@ -555,16 +560,6 @@ export default class Datetime extends React.Component {
555560
this._showView( mode );
556561
}
557562

558-
log( message, method ) {
559-
let con = typeof window !== 'undefined' && window.console;
560-
if ( !con ) return;
561-
562-
if ( !method ) {
563-
method = 'warn';
564-
}
565-
con[ method ]( '***react-datetime:' + message );
566-
}
567-
568563
_onInputFocus = e => {
569564
if ( !this.callHandler( this.props.inputProps.onFocus, e ) ) return;
570565
this._openCalendar();
@@ -613,6 +608,16 @@ export default class Datetime extends React.Component {
613608
}
614609
}
615610

611+
function log( message, method ) {
612+
let con = typeof window !== 'undefined' && window.console;
613+
if ( !con ) return;
614+
615+
if ( !method ) {
616+
method = 'warn';
617+
}
618+
con[ method ]( '***react-datetime:' + message );
619+
}
620+
616621
class ClickOutBase extends React.Component {
617622
container = React.createRef();
618623

src/datetime/DaysView.js

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,25 @@ export default class DaysView extends React.Component {
77
}
88

99
render() {
10-
const date = this.props.viewDate;
11-
const locale = date.localeData();
12-
13-
let startOfMonth = date.clone().startOf('month');
14-
let endOfMonth = date.clone().endOf('month');
15-
1610
return (
1711
<div className="rdtDays">
1812
<table>
1913
<thead>
20-
{ this.renderNavigation( date, locale ) }
21-
{ this.renderDayHeaders( locale ) }
14+
{ this.renderNavigation() }
15+
{ this.renderDayHeaders() }
2216
</thead>
2317
<tbody>
24-
{ this.renderDays( date, startOfMonth, endOfMonth ) }
18+
{ this.renderDays() }
2519
</tbody>
26-
{ this.renderFooter( date ) }
20+
{ this.renderFooter() }
2721
</table>
2822
</div>
2923
);
3024
}
3125

32-
renderNavigation( date, locale ) {
26+
renderNavigation() {
27+
const date = this.props.viewDate;
28+
const locale = date.localeData();
3329
return (
3430
<ViewNavigation
3531
onClickPrev={ () => this.props.navigate( -1, 'months' ) }
@@ -42,8 +38,9 @@ export default class DaysView extends React.Component {
4238
);
4339
}
4440

45-
renderDayHeaders( locale ) {
46-
let dayItems = this.getDaysOfWeek( locale ).map( (day, index) => (
41+
renderDayHeaders() {
42+
const locale = this.props.viewDate.localeData();
43+
let dayItems = getDaysOfWeek( locale ).map( (day, index) => (
4744
<th key={ day + index } className="dow">{ day }</th>
4845
));
4946

@@ -54,7 +51,11 @@ export default class DaysView extends React.Component {
5451
);
5552
}
5653

57-
renderDays( date, startOfMonth, endOfMonth ) {
54+
renderDays() {
55+
const date = this.props.viewDate;
56+
const startOfMonth = date.clone().startOf('month');
57+
const endOfMonth = date.clone().endOf('month');
58+
5859
// We need 42 days in 6 rows
5960
// starting in the last week of the previous month
6061
let rows = [[], [], [], [], [], []];
@@ -66,7 +67,7 @@ export default class DaysView extends React.Component {
6667
let i = 0;
6768

6869
while ( startDate.isBefore( endDate ) ) {
69-
let row = this.getRow( rows, i++ );
70+
let row = getRow( rows, i++ );
7071
row.push( this.renderDay( startDate, startOfMonth, endOfMonth ) );
7172
startDate.add( 1, 'd' );
7273
}
@@ -120,9 +121,10 @@ export default class DaysView extends React.Component {
120121
);
121122
}
122123

123-
renderFooter( date ) {
124+
renderFooter() {
124125
if ( !this.props.timeFormat ) return;
125126

127+
const date = this.props.viewDate;
126128
return (
127129
<tfoot>
128130
<tr>
@@ -139,25 +141,25 @@ export default class DaysView extends React.Component {
139141
_setDate = e => {
140142
this.props.updateDate( e );
141143
}
144+
}
142145

143-
/**
144-
* Get a list of the days of the week
145-
* depending on the current locale
146-
* @return {array} A list with the shortname of the days
147-
*/
148-
getDaysOfWeek(locale) {
149-
const first = locale.firstDayOfWeek();
150-
let dow = [];
151-
let i = 0;
152-
153-
locale._weekdaysMin.forEach(function (day) {
154-
dow[(7 + (i++) - first) % 7] = day;
155-
});
156-
157-
return dow;
158-
}
146+
function getRow( rows, day ) {
147+
return rows[ Math.floor( day / 7 ) ];
148+
}
159149

160-
getRow( rows, day ) {
161-
return rows[ Math.floor( day / 7 ) ];
162-
}
150+
/**
151+
* Get a list of the days of the week
152+
* depending on the current locale
153+
* @return {array} A list with the shortname of the days
154+
*/
155+
function getDaysOfWeek( locale ) {
156+
const first = locale.firstDayOfWeek();
157+
let dow = [];
158+
let i = 0;
159+
160+
locale._weekdaysMin.forEach(function (day) {
161+
dow[(7 + (i++) - first) % 7] = day;
162+
});
163+
164+
return dow;
163165
}

0 commit comments

Comments
 (0)