FilterInput.js 3.2 KB
import React,{PropTypes} from 'react';   
import cx from 'classnames';
import s from './FilterInput.scss';
import DateTimePicker from './DateTimePicker';
import DateMonthPicker from './DateMonthPicker';
import 'react-month-picker/css/month-picker.css';
import {MonthPicker,DatetimePicker} from './custom';

class FilterInput extends React.Component {
	constructor (props) {
	    super(props);   
        this.handleChange=this.handleChange.bind(this);   
        this.handleValueChange=this.handleValueChange.bind(this);   
        this.handleDateChange=this.handleDateChange.bind(this);   
		this.handleAMonthChange=this.handleAMonthChange.bind(this);
		const iniValue = this.props.iniValue?this.props.iniValue:'';
 
		this.state={
			value:iniValue,
			hasFilter:false
		}
	}
	static propTypes = {  
    	floatR: PropTypes.bool, 
	}  
	static defaultProps={
		floatR:true 
	}

	componentWillReceiveProps(nextProps){
		const {iniValue}=nextProps;
		console.log(iniValue);
		if(iniValue&&iniValue.length>0&&iniValue!=this.state.value){
			this.setState({value:iniValue, hasFilter:true});
		}
	}


	handleChange(e) {
		var input = e.target.value;
		const key = this.props.placeholder.key;

		if(e.keyCode === 13){
			input = input.replace(/[\r\n]/g,"");
			if(input.length>0){
				this.setState({value: input,hasFilter:true});
				this.props.onChange(key, input);
			}
		}
		else if(input.length==0){
			const hasFilter = this.state.hasFilter;
			if(hasFilter){
				this.props.onChange(key, '');
				//this.setState({value: input,hasFilter:true});
			}
		}
	}

	handleValueChange(e){
		const value = e.target.value;
		this.setState({value,value});
	}

	static propTypes = {  
    	onChange: PropTypes.func, 
    	placeholder:PropTypes.object
	}  

	handleDateChange(e) {
		var input = e;
		const key = this.props.placeholder.key;
		this.setState({value: input});
		this.props.onChange(key, input);
	}

	handleAMonthChange(input) {
		console.log(input);
		const key = this.props.placeholder.key;
		this.setState({value: input});
		this.props.onChange(key, input);
	} 
	renderMonth(){
		return( 
			<div className={cx(s.filter_input_wrap)}>  
				<MonthPicker  className="form-control"  defaultValue={this.state.value} onChange={this.handleAMonthChange}></MonthPicker> 
			</div>
		);
	} 
	render(){ 
		const {placeholder,floatR,width}=this.props;
		const dataType = placeholder.dataType;
		if(dataType&&dataType=='date'){  
			const dateTimeConfig={'format':'utc'};
			return(	
				<div className={cx(s.filter_input_wrap,{'fr':floatR})} style={{width:'180px'}}> 
					<DatetimePicker config={dateTimeConfig} value={this.state.value}  onChange={this.handleDateChange}></DatetimePicker>
				</div>
				); 
		}
		else if(dataType&&dataType=='month'){ 
			return this.renderMonth();
		}
		else{
			console.log(this.state.value);
			return( 
				<div className={cx(s.filter_input_wrap,{'fr':floatR})}  style={{'width':width}}> 
					<i className={cx('kr_icon')}>&#xe65c;</i>
					<input type="text" className="form-control" style={{'width':'100%'}} value={this.state.value} placeholder={placeholder.displayName} onChange={this.handleValueChange} onKeyUp={this.handleChange}/> 			 
				</div>
			);
		}
	}
} 

export default FilterInput;