This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add calendar to date advanced filter
- Loading branch information
Showing
5 changed files
with
241 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
packages/components/src/filters/advanced/date-filter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
/** @format */ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
import interpolateComponents from 'interpolate-components'; | ||
import { SelectControl } from '@wordpress/components'; | ||
import { find, partial } from 'lodash'; | ||
import classnames from 'classnames'; | ||
import { __, _x } from '@wordpress/i18n'; | ||
import moment from 'moment'; | ||
|
||
/** | ||
* WooCommerce dependencies | ||
*/ | ||
import { isoDateFormat, toMoment } from '@woocommerce/date'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import DatePicker from '../../calendar/date-picker'; | ||
|
||
const dateStringFormat = __( 'MMM D, YYYY', 'wc-admin' ); | ||
const dateFormat = __( 'MM/DD/YYYY', 'wc-admin' ); | ||
|
||
class DateFilter extends Component { | ||
constructor( { filter } ) { | ||
super( ...arguments ); | ||
|
||
const [ isoAfter, isoBefore ] = ( filter.value || '' ).split( ',' ); | ||
const after = isoAfter ? toMoment( isoDateFormat, isoAfter ) : null; | ||
const before = isoBefore ? toMoment( isoDateFormat, isoBefore ) : null; | ||
|
||
this.state = { | ||
before, | ||
beforeText: before ? before.format( dateFormat ) : '', | ||
beforeError: null, | ||
after, | ||
afterText: after ? after.format( dateFormat ) : '', | ||
afterError: null, | ||
}; | ||
|
||
this.onSingleDateChange = this.onSingleDateChange.bind( this ); | ||
this.onRangeDateChange = this.onRangeDateChange.bind( this ); | ||
} | ||
|
||
getBetweenString() { | ||
return _x( | ||
'{{after /}}{{span}}and{{/span}}{{before /}}', | ||
'Date range inputs arranged on a single line', | ||
'wc-admin' | ||
); | ||
} | ||
|
||
getLegend( filter, config ) { | ||
const date = moment(); // For now... | ||
const rule = find( config.rules, { value: filter.rule } ) || {}; | ||
const filterStr = date && date.format( dateStringFormat ); | ||
|
||
return interpolateComponents( { | ||
mixedString: config.labels.title, | ||
components: { | ||
filter: <span>{ filterStr }</span>, | ||
rule: <span>{ rule.label }</span>, | ||
}, | ||
} ); | ||
} | ||
|
||
onSingleDateChange( { date, text, error } ) { | ||
const { filter, onFilterChange } = this.props; | ||
this.setState( { before: date, beforeText: text, beforeError: error } ); | ||
|
||
if ( date ) { | ||
onFilterChange( filter.key, 'value', date.format( isoDateFormat ) ); | ||
} | ||
} | ||
|
||
onRangeDateChange( input, { date, text, error } ) { | ||
const { filter, onFilterChange } = this.props; | ||
|
||
this.setState( { | ||
[ input ]: date, | ||
[ input + 'Text' ]: text, | ||
[ input + 'Error' ]: error, | ||
} ); | ||
|
||
if ( date ) { | ||
const { before, after } = this.state; | ||
let nextAfter = null; | ||
let nextBefore = null; | ||
|
||
if ( 'after' === input ) { | ||
nextAfter = date.format( isoDateFormat ); | ||
nextBefore = before ? before.format( isoDateFormat ) : null; | ||
} | ||
|
||
if ( 'before' === input ) { | ||
nextAfter = after ? after.format( isoDateFormat ) : null; | ||
nextBefore = date.format( isoDateFormat ); | ||
} | ||
|
||
if ( nextAfter && nextBefore ) { | ||
onFilterChange( filter.key, 'value', [ nextAfter, nextBefore ].join( ',' ) ); | ||
} | ||
} | ||
} | ||
|
||
getFilterInputs() { | ||
const { filter } = this.props; | ||
const { before, beforeText, beforeError, after, afterText, afterError } = this.state; | ||
|
||
if ( 'between' === filter.rule ) { | ||
return interpolateComponents( { | ||
mixedString: this.getBetweenString(), | ||
components: { | ||
after: ( | ||
<DatePicker | ||
date={ after } | ||
text={ afterText } | ||
error={ afterError } | ||
onUpdate={ partial( this.onRangeDateChange, 'after' ) } | ||
dateFormat={ dateFormat } | ||
invalidDays="none" | ||
/> | ||
), | ||
before: ( | ||
<DatePicker | ||
date={ before } | ||
text={ beforeText } | ||
error={ beforeError } | ||
onUpdate={ partial( this.onRangeDateChange, 'before' ) } | ||
dateFormat={ dateFormat } | ||
invalidDays="none" | ||
/> | ||
), | ||
span: <span className="separator" />, | ||
}, | ||
} ); | ||
} | ||
|
||
return ( | ||
<DatePicker | ||
date={ before } | ||
text={ beforeText } | ||
error={ beforeError } | ||
onUpdate={ this.onSingleDateChange } | ||
dateFormat={ dateFormat } | ||
invalidDays="none" | ||
/> | ||
); | ||
} | ||
|
||
render() { | ||
const { config, filter, onFilterChange, isEnglish } = this.props; | ||
const { key, rule } = filter; | ||
const { labels, rules } = config; | ||
const children = interpolateComponents( { | ||
mixedString: labels.title, | ||
components: { | ||
rule: ( | ||
<SelectControl | ||
className="woocommerce-filters-advanced__rule" | ||
options={ rules } | ||
value={ rule } | ||
onChange={ partial( onFilterChange, key, 'rule' ) } | ||
aria-label={ labels.rule } | ||
/> | ||
), | ||
filter: ( | ||
<div | ||
className={ classnames( 'woocommerce-filters-advanced__input-range', { | ||
'is-between': 'between' === rule, | ||
} ) } | ||
> | ||
{ this.getFilterInputs() } | ||
</div> | ||
), | ||
}, | ||
} ); | ||
/*eslint-disable jsx-a11y/no-noninteractive-tabindex*/ | ||
return ( | ||
<fieldset tabIndex="0"> | ||
<legend className="screen-reader-text"> | ||
{ this.getLegend( filter, config ) } | ||
</legend> | ||
<div | ||
className={ classnames( 'woocommerce-filters-advanced__fieldset', { | ||
'is-english': isEnglish, | ||
} ) } | ||
> | ||
{ children } | ||
</div> | ||
</fieldset> | ||
); | ||
/*eslint-enable jsx-a11y/no-noninteractive-tabindex*/ | ||
} | ||
} | ||
|
||
export default DateFilter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters