Skip to content

Commit

Permalink
Extract ViewNavigation component
Browse files Browse the repository at this point in the history
Three files use same navigation structure, so I extracted it to a
component for DRY.
  • Loading branch information
onlined committed Sep 21, 2020
1 parent d53beb6 commit b29e9c2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 37 deletions.
20 changes: 9 additions & 11 deletions src/datetime/DaysView.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import ViewNavigation from './ViewNavigation';

export default class DaysView extends React.Component {
static defaultProps = {
Expand Down Expand Up @@ -30,17 +31,14 @@ export default class DaysView extends React.Component {

renderNavigation( date, locale ) {
return (
<tr>
<th className="rdtPrev" onClick={ () => this.props.navigate( -1, 'months' ) }>
<span></span>
</th>
<th className="rdtSwitch" onClick={ () => this.props.showView( 'months' ) } colSpan={5} data-value={ this.props.viewDate.month() }>
{ locale.months( date ) + ' ' + date.year() }
</th>
<th className="rdtNext" onClick={ () => this.props.navigate( 1, 'months' ) }>
<span></span>
</th>
</tr>
<ViewNavigation
onClickPrev={ () => this.props.navigate( -1, 'months' ) }
onClickSwitch={ () => this.props.showView( 'months' ) }
onClickNext={ () => this.props.navigate( 1, 'months' ) }
switchContent={ locale.months( date ) + ' ' + date.year() }
switchColSpan={5}
switchProps={ { 'data-value': this.props.viewDate.month() } }
/>
);
}

Expand Down
23 changes: 10 additions & 13 deletions src/datetime/MonthsView.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React from 'react';
import ViewNavigation from './ViewNavigation';

export default class MonthsView extends React.Component {
render() {
return (
<div className="rdtMonths">
<table>
<thead>
{ this.renderHeader() }
{ this.renderNavigation() }
</thead>
</table>
<table>
Expand All @@ -18,21 +19,17 @@ export default class MonthsView extends React.Component {
);
}

renderHeader() {
renderNavigation() {
let year = this.props.viewDate.year();

return (
<tr>
<th className="rdtPrev" onClick={ () => this.props.navigate( -1, 'years' ) }>
<span></span>
</th>
<th className="rdtSwitch" onClick={ () => this.props.showView( 'years' ) } colSpan="2" data-value={ year } >
{ year }
</th>
<th className="rdtNext" onClick={ () => this.props.navigate( 1, 'years' ) }>
<span></span>
</th>
</tr>
<ViewNavigation
onClickPrev={ () => this.props.navigate( -1, 'years' ) }
onClickSwitch={ () => this.props.showView( 'years' ) }
onClickNext={ () => this.props.navigate( 1, 'years' ) }
switchContent={ year }
switchColSpan="2"
/>
);
}

Expand Down
17 changes: 17 additions & 0 deletions src/datetime/ViewNavigation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';

export default function ViewNavigation( { onClickPrev, onClickSwitch, onClickNext, switchContent, switchColSpan, switchProps } ) {
return (
<tr>
<th className="rdtPrev" onClick={ onClickPrev }>
<span></span>
</th>
<th className="rdtSwitch" colSpan={ switchColSpan } onClick={ onClickSwitch } {...switchProps}>
{ switchContent }
</th>
<th className="rdtNext" onClick={ onClickNext }>
<span></span>
</th>
</tr>
);
}
22 changes: 9 additions & 13 deletions src/datetime/YearsView.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import ViewNavigation from './ViewNavigation';

export default class YearsView extends React.Component {
render() {
Expand All @@ -8,7 +9,7 @@ export default class YearsView extends React.Component {
<div className="rdtYears">
<table>
<thead>
{ this.renderHeader( viewYear ) }
{ this.renderNavigation( viewYear ) }
</thead>
</table>
<table>
Expand All @@ -20,19 +21,14 @@ export default class YearsView extends React.Component {
);
}

renderHeader( viewYear ) {
renderNavigation( viewYear ) {
return (
<tr>
<th className="rdtPrev" onClick={ () => this.props.navigate( -10, 'years' ) }>
<span></span>
</th>
<th className="rdtSwitch" onClick={ () => this.props.showView( 'years' ) }>
{ `${viewYear}-${viewYear + 9}` }
</th>
<th className="rdtNext" onClick={ () => this.props.navigate( 10, 'years' ) }>
<span></span>
</th>
</tr>
<ViewNavigation
onClickPrev={ () => this.props.navigate( -10, 'years' ) }
onClickSwitch={ () => this.props.showView( 'years' ) }
onClickNext={ () => this.props.navigate( 10, 'years' ) }
switchContent={ `${viewYear}-${viewYear + 9}` }
/>
);
}

Expand Down

0 comments on commit b29e9c2

Please sign in to comment.