TimePicker.js
2.2 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
import React,{PropTypes} from 'react';
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 formatter = new DateTimeFormat('HH:mm:ss');
const formatterNoSecond = new DateTimeFormat('HH:mm');
const now = new GregorianCalendar(TimePickerLocale.calendar);
function onChange(value) {
console.log(value && formatter.format(value));
}
export class MyTimePicker extends React.Component {
constructor(props) {
super(props);
this.timeChange=this.timeChange.bind(this);
this.state={
timeValue:now
}
}
static propTypes = {
defaultValue:PropTypes.string,
value:PropTypes.string,
onChange:PropTypes.func,
placeholder:PropTypes.string,
config:PropTypes.object,
showSecond:PropTypes.bool
}
static defaultProps = {
disabled: false,
showSecond:false,
formatString:'HH:mm:ss'
}
componentDidMount(){
const {value}=this.props;
const my = new GregorianCalendar(TimePickerLocale.calendar);
if(value){
my.setTime(new Date('2016/03/02 '+value+':00'));
}else
my.setTime(new Date('2016/03/02 00:00:00'));
this.setState({
timeValue:my
})
}
timeChange(value){
console.log(value && formatter.format(value));
const {showSecond}=this.props;
let time='';
if(showSecond){
time=formatter.format(value);
}else{
time=formatterNoSecond.format(value);
}
this.props.onChange(time);
this.setState({ timeValue:value });
}
render(){
const {showSecond,value}=this.props;
const {timeValue}=this.state;
return (
<TimePicker formatter={showSecond?formatter:formatterNoSecond} locale={TimePickerLocale}
showSecond={true}
value={timeValue}
showSecond={showSecond}
animation="slide-up"
className="form-control"
onChange={this.timeChange} />
)
}
}
export default MyTimePicker