DateTimePicker.js
3.7 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
121
122
123
124
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