DateMonthPicker.js
3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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