|
| 1 | +/* eslint react/require-default-props: 0 */ |
| 2 | +/* eslint react/no-unused-prop-types: 0 */ |
| 3 | +/* eslint no-return-assign: 0 */ |
| 4 | +import React, { Component } from 'react'; |
| 5 | +import { PropTypes } from 'prop-types'; |
| 6 | + |
| 7 | +import { LIKE, EQ } from '../comparison'; |
| 8 | +import { FILTER_TYPE, FILTER_DELAY } from '../const'; |
| 9 | + |
| 10 | +class TextFilter extends Component { |
| 11 | + constructor(props) { |
| 12 | + super(props); |
| 13 | + this.filter = this.filter.bind(this); |
| 14 | + this.timeout = null; |
| 15 | + this.state = { |
| 16 | + value: props.defaultValue |
| 17 | + }; |
| 18 | + } |
| 19 | + componentDidMount() { |
| 20 | + const defaultValue = this.input.value; |
| 21 | + if (defaultValue) { |
| 22 | + this.props.onFilter(this.props.column, defaultValue, FILTER_TYPE.TEXT); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + componentWillReceiveProps(nextProps) { |
| 27 | + if (nextProps.defaultValue !== this.props.defaultValue) { |
| 28 | + this.applyFilter(nextProps.defaultValue); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + componentWillUnmount() { |
| 33 | + clearTimeout(this.timeout); |
| 34 | + } |
| 35 | + |
| 36 | + filter(e) { |
| 37 | + e.stopPropagation(); |
| 38 | + if (this.timeout) { |
| 39 | + clearTimeout(this.timeout); |
| 40 | + } |
| 41 | + const filterValue = e.target.value; |
| 42 | + this.setState(() => ({ value: filterValue })); |
| 43 | + this.timeout = setTimeout(() => { |
| 44 | + this.props.onFilter(this.props.column, filterValue, FILTER_TYPE.TEXT); |
| 45 | + }, this.props.delay); |
| 46 | + } |
| 47 | + |
| 48 | + cleanFiltered() { |
| 49 | + const value = this.props.defaultValue; |
| 50 | + this.setState(() => ({ value })); |
| 51 | + this.props.onFilter(this.props.column, value, FILTER_TYPE.TEXT); |
| 52 | + } |
| 53 | + |
| 54 | + applyFilter(filterText) { |
| 55 | + this.setState(() => ({ value: filterText })); |
| 56 | + this.props.onFilter(this.props.column, filterText, FILTER_TYPE.TEXT); |
| 57 | + } |
| 58 | + |
| 59 | + render() { |
| 60 | + const { placeholder, column: { text }, style } = this.props; |
| 61 | + // stopPropagation for onClick event is try to prevent sort was triggered. |
| 62 | + return ( |
| 63 | + <input |
| 64 | + ref={ n => this.input = n } |
| 65 | + type="text" |
| 66 | + className="filter text-filter form-control" |
| 67 | + style={ style } |
| 68 | + onChange={ this.filter } |
| 69 | + onClick={ e => e.stopPropagation() } |
| 70 | + placeholder={ placeholder || `Enter ${text}...` } |
| 71 | + value={ this.state.value } |
| 72 | + /> |
| 73 | + ); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +TextFilter.propTypes = { |
| 78 | + onFilter: PropTypes.func.isRequired, |
| 79 | + comparator: PropTypes.oneOf([LIKE, EQ]), |
| 80 | + defaultValue: PropTypes.string, |
| 81 | + delay: PropTypes.number, |
| 82 | + placeholder: PropTypes.string, |
| 83 | + column: PropTypes.object, |
| 84 | + style: PropTypes.object |
| 85 | +}; |
| 86 | + |
| 87 | +TextFilter.defaultProps = { |
| 88 | + delay: FILTER_DELAY, |
| 89 | + defaultValue: '' |
| 90 | +}; |
| 91 | + |
| 92 | + |
| 93 | +export default TextFilter; |
0 commit comments