Skip to content

Commit 058a80b

Browse files
committed
Extract ViewNavigation component
Three files use same navigation structure, so I extracted it to a component for DRY.
1 parent e808b1e commit 058a80b

File tree

4 files changed

+45
-37
lines changed

4 files changed

+45
-37
lines changed

src/datetime/DaysView.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import ViewNavigation from './ViewNavigation';
23

34
export default class DaysView extends React.Component {
45
static defaultProps = {
@@ -30,17 +31,14 @@ export default class DaysView extends React.Component {
3031

3132
renderNavigation( date, locale ) {
3233
return (
33-
<tr>
34-
<th className="rdtPrev" onClick={ () => this.props.navigate( -1, 'months' ) }>
35-
<span></span>
36-
</th>
37-
<th className="rdtSwitch" onClick={ () => this.props.showView( 'months' ) } colSpan={5} data-value={ this.props.viewDate.month() }>
38-
{ locale.months( date ) + ' ' + date.year() }
39-
</th>
40-
<th className="rdtNext" onClick={ () => this.props.navigate( 1, 'months' ) }>
41-
<span></span>
42-
</th>
43-
</tr>
34+
<ViewNavigation
35+
onClickPrev={ () => this.props.navigate( -1, 'months' ) }
36+
onClickSwitch={ () => this.props.showView( 'months' ) }
37+
onClickNext={ () => this.props.navigate( 1, 'months' ) }
38+
switchContent={ locale.months( date ) + ' ' + date.year() }
39+
switchColSpan={5}
40+
switchProps={ { 'data-value': this.props.viewDate.month() } }
41+
/>
4442
);
4543
}
4644

src/datetime/MonthsView.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import React from 'react';
2+
import ViewNavigation from './ViewNavigation';
23

34
export default class MonthsView extends React.Component {
45
render() {
56
return (
67
<div className="rdtMonths">
78
<table>
89
<thead>
9-
{ this.renderHeader() }
10+
{ this.renderNavigation() }
1011
</thead>
1112
</table>
1213
<table>
@@ -18,21 +19,17 @@ export default class MonthsView extends React.Component {
1819
);
1920
}
2021

21-
renderHeader() {
22+
renderNavigation() {
2223
let year = this.props.viewDate.year();
2324

2425
return (
25-
<tr>
26-
<th className="rdtPrev" onClick={ () => this.props.navigate( -1, 'years' ) }>
27-
<span></span>
28-
</th>
29-
<th className="rdtSwitch" onClick={ () => this.props.showView( 'years' ) } colSpan="2" data-value={ year } >
30-
{ year }
31-
</th>
32-
<th className="rdtNext" onClick={ () => this.props.navigate( 1, 'years' ) }>
33-
<span></span>
34-
</th>
35-
</tr>
26+
<ViewNavigation
27+
onClickPrev={ () => this.props.navigate( -1, 'years' ) }
28+
onClickSwitch={ () => this.props.showView( 'years' ) }
29+
onClickNext={ () => this.props.navigate( 1, 'years' ) }
30+
switchContent={ year }
31+
switchColSpan="2"
32+
/>
3633
);
3734
}
3835

src/datetime/ViewNavigation.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
3+
export default function ViewNavigation( { onClickPrev, onClickSwitch, onClickNext, switchContent, switchColSpan, switchProps } ) {
4+
return (
5+
<tr>
6+
<th className="rdtPrev" onClick={ onClickPrev }>
7+
<span></span>
8+
</th>
9+
<th className="rdtSwitch" colSpan={ switchColSpan } onClick={ onClickSwitch } {...switchProps}>
10+
{ switchContent }
11+
</th>
12+
<th className="rdtNext" onClick={ onClickNext }>
13+
<span></span>
14+
</th>
15+
</tr>
16+
);
17+
}

src/datetime/YearsView.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import ViewNavigation from './ViewNavigation';
23

34
export default class YearsView extends React.Component {
45
render() {
@@ -8,7 +9,7 @@ export default class YearsView extends React.Component {
89
<div className="rdtYears">
910
<table>
1011
<thead>
11-
{ this.renderHeader( viewYear ) }
12+
{ this.renderNavigation( viewYear ) }
1213
</thead>
1314
</table>
1415
<table>
@@ -20,19 +21,14 @@ export default class YearsView extends React.Component {
2021
);
2122
}
2223

23-
renderHeader( viewYear ) {
24+
renderNavigation( viewYear ) {
2425
return (
25-
<tr>
26-
<th className="rdtPrev" onClick={ () => this.props.navigate( -10, 'years' ) }>
27-
<span></span>
28-
</th>
29-
<th className="rdtSwitch" onClick={ () => this.props.showView( 'years' ) }>
30-
{ `${viewYear}-${viewYear + 9}` }
31-
</th>
32-
<th className="rdtNext" onClick={ () => this.props.navigate( 10, 'years' ) }>
33-
<span></span>
34-
</th>
35-
</tr>
26+
<ViewNavigation
27+
onClickPrev={ () => this.props.navigate( -10, 'years' ) }
28+
onClickSwitch={ () => this.props.showView( 'years' ) }
29+
onClickNext={ () => this.props.navigate( 10, 'years' ) }
30+
switchContent={ `${viewYear}-${viewYear + 9}` }
31+
/>
3632
);
3733
}
3834

0 commit comments

Comments
 (0)