DateTimePicker.js 3.7 KB
import React,{PropTypes} from 'react'; 
import 'rc-calendar/assets/index.css';
import Calendar from 'rc-calendar';
import DatePicker  from 'rc-calendar/lib/Picker';  
import CalendarLocale from 'rc-calendar/lib/locale/zh_CN';
import 'rc-time-picker/assets/index.css';
import TimePicker from 'rc-time-picker'; 
import TimePickerLocale from 'rc-time-picker/lib/locale/zh_CN';
import zhCn from 'gregorian-calendar/lib/locale/zh_CN'; // spm error
import DateTimeFormat from 'gregorian-calendar-format';
import GregorianCalendar from 'gregorian-calendar';
import moment from 'moment';
import cx from 'classnames'; 


const timePickerElement = <TimePicker  locale={TimePickerLocale}/>;
const formatter = new DateTimeFormat('yyyy-MM-dd HH:mm:ss');
const dateFormatter = new DateTimeFormat('yyyy-MM-dd');
const SHOW_TIME = true;
const now = new GregorianCalendar(zhCn);
      now.setTime(Date.now());

function getFormatter(showTime) {
  return showTime ? formatter : dateFormatter;
}
 

const defaultConfig={
	zIndex:99999,//2000,
	dateInputPlaceholder:'',
	disabledTime:false,
	timePicker:true,
	showDateInput:true,
	disabledDate:false,
	disabled:false
}

 

export class DateTimePicker extends React.Component { 
	constructor(props) {
        super(props); 
        this.dateTimeChange=this.dateTimeChange.bind(this);
        this.initDefaultValue=this.initDefaultValue.bind(this);
        this.state={ 
        }  
	}
	static propTypes = {  
	    defaultValue:PropTypes.string,
	    value:PropTypes.object, 
	    onChange:PropTypes.func,
	    placeholder:PropTypes.string,
	    config:PropTypes.object
	}
	static defaultProps = {
		showTime: SHOW_TIME,
		disabled: false,
		formatString:'yyyy-MM-dd'
	}
	initDefaultValue(){
		if(this.props.defaultValue&&moment(this.props.defaultValue).isValid()){
			this.props.onChange(this.props.defaultValue);
		}else{
			this.dateTimeChange(now);
		}
	}
	dateTimeChange(value){ 
		const config=Object.assign({},defaultConfig,this.props.config); 
		if(value){ 
			const dateTime=getFormatter(config.timePicker).format(value);
			if(config.format){
				if(config.format=='utc')
					this.props.onChange(moment(dateTime).format());
				else
					this.props.onChange(moment(dateTime).format(config.format));
			}else{
				this.props.onChange(moment(dateTime).format());
			} 
		}else{
			this.props.onChange(value);
		} 
	}
	componentDidMount(){
		this.initDefaultValue();
	}
	render(){ 
		const config=Object.assign({},defaultConfig,this.props.config);
		if(this.props.defaultValue&&moment(this.props.defaultValue).isValid()){
			const defaultDate=moment(this.props.defaultValue).toDate(); 
			now.setTime(defaultDate);
		}
		const calendar = (<Calendar locale={CalendarLocale}
                             style={{zIndex: config.zIndex}} 
                             dateInputPlaceholder={config.dateInputPlaceholder} 
                             timePicker={config.timePicker ? timePickerElement : null} 
                             showOk
                             showDateInput={config.showDateInput}
                             disabledDate={()=>{config.disabledDate}} />);
		return ( 
			<DatePicker
	    	  style={{zIndex: config.zIndex}}
	          animation="slide-up"
	          disabled={config.disabled}
	          calendar={calendar} 
	          defaultValue={now}  
	          onChange={this.dateTimeChange}>
	          {		
	            ({value})=> {	 
	              	return ( 
		                <input placeholder={this.props.placeholder} 
	                       disabled={false}
	                       readOnly
	                       className={cx('form-control')} 
	                       value={value && getFormatter(config.timePicker).format(value)}/>  
	              	);
	            }
	          }
	        </DatePicker>  
		)
	}
}


export default DateTimePicker