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

const now = new GregorianCalendar(zhCn);
now.setTime(Date.now());
const formatter = new DateTimeFormat('yyyy-MM');

const defaultCalendarValue = new GregorianCalendar(zhCn);
defaultCalendarValue.setTime(Date.now());
defaultCalendarValue.addMonth(-1);

 

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 DateMonthPicker extends React.Component { 
	constructor(props) {
        super(props); 
        this.dateTimeChange=this.dateTimeChange.bind(this);
        this.state={ 
        }  
	}
	static propTypes = {  
	    defaultValue:PropTypes.string,
	    value:PropTypes.object, 
	    onChange:PropTypes.func,
	    placeholder:PropTypes.string,
	    config:PropTypes.object
	}
	static defaultProps = { 
		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(dateTime);
			} 
		}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 = (<MonthCalendar locale={CalendarLocale}
                             style={{zIndex: config.zIndex}} 
                             dateInputPlaceholder={config.dateInputPlaceholder}  
                             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 DateMonthPicker