Skip to content

Commit

Permalink
Extract Header component
Browse files Browse the repository at this point in the history
Three files use same header structure, so I extracted it to a
component for DRY.
  • Loading branch information
onlined committed Sep 21, 2020
1 parent d53beb6 commit 07866bc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 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 Header from './Header';

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>
<Header
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
17 changes: 17 additions & 0 deletions src/datetime/Header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';

export default function Header( { 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>
);
}
19 changes: 8 additions & 11 deletions src/datetime/MonthsView.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import Header from './Header';

export default class MonthsView extends React.Component {
render() {
Expand All @@ -22,17 +23,13 @@ export default class MonthsView extends React.Component {
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>
<Header
onClickPrev={ () => this.props.navigate( -1, 'years' ) }
onClickSwitch={ () => this.props.showView( 'years' ) }
onClickNext={ () => this.props.navigate( 1, 'years' ) }
switchContent={ year }
switchColSpan="2"
/>
);
}

Expand Down
18 changes: 7 additions & 11 deletions src/datetime/YearsView.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import Header from './Header';

export default class YearsView extends React.Component {
render() {
Expand All @@ -22,17 +23,12 @@ export default class YearsView extends React.Component {

renderHeader( 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>
<Header
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 07866bc

Please sign in to comment.