|
3 | 3 | import React, { Component, PropTypes } from 'react';
|
4 | 4 | import Const from '../Const';
|
5 | 5 |
|
| 6 | +const legalComparators = [ '=', '>', '>=', '<', '<=', '!=' ]; |
| 7 | + |
| 8 | +function dateParser(d) { |
| 9 | + return `${d.getFullYear()}-${("0" + (d.getMonth() + 1)).slice(-2)}-${("0" + d.getDate()).slice(-2)}`; |
| 10 | +} |
| 11 | + |
6 | 12 | class DateFilter extends Component {
|
7 | 13 | constructor(props) {
|
8 | 14 | super(props);
|
| 15 | + this.dateComparators = this.props.dateComparators || legalComparators; |
9 | 16 | this.filter = this.filter.bind(this);
|
| 17 | + this.onChangeComparator = this.onChangeComparator.bind(this); |
10 | 18 | }
|
11 | 19 |
|
12 | 20 | setDefaultDate() {
|
13 | 21 | let defaultDate = '';
|
14 |
| - if (this.props.defaultValue) { |
15 |
| - // Set the appropriate format for the input type=date, i.e. "YYYY-MM-DD" |
16 |
| - const defaultValue = new Date(this.props.defaultValue); |
17 |
| - defaultDate = `${defaultValue.getFullYear()}-${("0" + (defaultValue.getMonth() + 1)).slice(-2)}-${("0" + defaultValue.getDate()).slice(-2)}`; |
| 22 | + const { defaultValue } = this.props; |
| 23 | + if (defaultValue && defaultValue.date) { |
| 24 | + // Set the appropriate format for the input type=date, i.e. "YYYY-MM-DD" |
| 25 | + defaultDate = dateParser(new Date(defaultValue.date)); |
18 | 26 | }
|
19 | 27 | return defaultDate;
|
20 | 28 | }
|
21 | 29 |
|
| 30 | + onChangeComparator(event) { |
| 31 | + let date = this.refs.inputDate.value; |
| 32 | + const comparator = event.target.value; |
| 33 | + if (date === '') { |
| 34 | + return; |
| 35 | + } |
| 36 | + date = new Date(date); |
| 37 | + this.props.filterHandler({ date, comparator }, Const.FILTER_TYPE.DATE); |
| 38 | + } |
| 39 | + |
| 40 | + getComparatorOptions() { |
| 41 | + const optionTags = []; |
| 42 | + optionTags.push(<option key='-1'></option>); |
| 43 | + for (let i = 0; i < this.dateComparators.length; i++) { |
| 44 | + optionTags.push( |
| 45 | + <option key={ i } value={ this.dateComparators[i] }> |
| 46 | + { this.dateComparators[i] } |
| 47 | + </option> |
| 48 | + ); |
| 49 | + } |
| 50 | + return optionTags; |
| 51 | + } |
| 52 | + |
22 | 53 | filter(event) {
|
| 54 | + const comparator = this.refs.dateFilterComparator.value; |
23 | 55 | const dateValue = event.target.value;
|
24 | 56 | if (dateValue) {
|
25 |
| - this.props.filterHandler(new Date(dateValue), Const.FILTER_TYPE.DATE); |
| 57 | + this.props.filterHandler({ date: new Date(dateValue), comparator }, Const.FILTER_TYPE.DATE); |
26 | 58 | } else {
|
27 | 59 | this.props.filterHandler(null, Const.FILTER_TYPE.DATE);
|
28 | 60 | }
|
29 | 61 | }
|
30 | 62 |
|
31 | 63 | componentDidMount() {
|
| 64 | + const comparator = this.refs.dateFilterComparator.value; |
32 | 65 | const dateValue = this.refs.inputDate.defaultValue;
|
33 |
| - if (dateValue) { |
34 |
| - this.props.filterHandler(new Date(dateValue), Const.FILTER_TYPE.DATE); |
| 66 | + if (comparator && dateValue) { |
| 67 | + this.props.filterHandler({ date: new Date(dateValue), comparator }, Const.FILTER_TYPE.DATE); |
35 | 68 | }
|
36 | 69 | }
|
37 | 70 |
|
38 | 71 | render() {
|
| 72 | + const { defaultValue } = this.props; |
39 | 73 | return (
|
| 74 | + <div className='filter date-filter'> |
| 75 | + <select ref='dateFilterComparator' |
| 76 | + className='date-filter-comparator form-control' |
| 77 | + onChange={ this.onChangeComparator } |
| 78 | + defaultValue={ (defaultValue) ? defaultValue.comparator : '' }> |
| 79 | + { this.getComparatorOptions() } |
| 80 | + </select> |
40 | 81 | <input ref='inputDate'
|
41 |
| - className='filter date-filter form-control' |
| 82 | + className='filter date-filter-input form-control' |
42 | 83 | type='date'
|
43 | 84 | onChange={ this.filter }
|
44 | 85 | defaultValue={ this.setDefaultDate() } />
|
| 86 | + </div> |
45 | 87 | );
|
46 | 88 | }
|
47 | 89 | }
|
48 | 90 |
|
49 | 91 | DateFilter.propTypes = {
|
50 | 92 | filterHandler: PropTypes.func.isRequired,
|
51 |
| - defaultValue: PropTypes.object, |
| 93 | + defaultValue: PropTypes.shape({ |
| 94 | + date: PropTypes.object, |
| 95 | + comparator: PropTypes.oneOf(legalComparators) |
| 96 | + }), |
| 97 | + /* eslint consistent-return: 0 */ |
| 98 | + dateComparators: function(props, propName) { |
| 99 | + if (!props[propName]) { |
| 100 | + return; |
| 101 | + } |
| 102 | + for (let i = 0; i < props[propName].length; i++) { |
| 103 | + let comparatorIsValid = false; |
| 104 | + for (let j = 0; j < legalComparators.length; j++) { |
| 105 | + if (legalComparators[j] === props[propName][i]) { |
| 106 | + comparatorIsValid = true; |
| 107 | + break; |
| 108 | + } |
| 109 | + } |
| 110 | + if (!comparatorIsValid) { |
| 111 | + return new Error(`Date comparator provided is not supported. |
| 112 | + Use only ${legalComparators}`); |
| 113 | + } |
| 114 | + } |
| 115 | + }, |
52 | 116 | columnName: PropTypes.string
|
53 | 117 | };
|
54 | 118 |
|
|
0 commit comments