2016-09-09 13:55:21 +08:00
|
|
|
import React from 'react';
|
|
|
|
import moment from 'moment';
|
2016-03-21 21:16:38 +08:00
|
|
|
import RcTimePicker from 'rc-time-picker/lib/TimePicker';
|
2015-12-08 14:55:28 +08:00
|
|
|
import classNames from 'classnames';
|
2016-06-22 13:18:43 +08:00
|
|
|
import assign from 'object-assign';
|
2016-09-09 13:55:21 +08:00
|
|
|
import defaultLocale from './locale/zh_CN';
|
2016-07-09 10:52:58 +08:00
|
|
|
|
|
|
|
// TimePicker
|
|
|
|
export interface TimePickerProps {
|
2016-09-09 13:55:21 +08:00
|
|
|
className?: string;
|
|
|
|
size?: 'large' | 'default' | 'small';
|
2016-07-13 11:14:24 +08:00
|
|
|
/** 默认时间 */
|
2016-09-09 13:55:21 +08:00
|
|
|
value?: moment.Moment;
|
2016-07-13 11:14:24 +08:00
|
|
|
/** 初始默认时间 */
|
2016-09-09 13:55:21 +08:00
|
|
|
defaultValue?: moment.Moment;
|
2016-07-09 10:52:58 +08:00
|
|
|
/** 展示的时间格式 : "HH:mm:ss"、"HH:mm"、"mm:ss" */
|
2016-07-13 11:14:24 +08:00
|
|
|
format?: string;
|
|
|
|
/** 时间发生变化的回调 */
|
2016-09-09 13:55:21 +08:00
|
|
|
onChange?: (time: moment.Moment, timeString: string) => void;
|
2016-07-13 11:14:24 +08:00
|
|
|
/** 禁用全部操作 */
|
|
|
|
disabled?: boolean;
|
|
|
|
/** 没有值的时候显示的内容 */
|
|
|
|
placeholder?: string;
|
|
|
|
/** 隐藏禁止选择的选项 */
|
|
|
|
hideDisabledOptions?: boolean;
|
|
|
|
/** 禁止选择部分小时选项 */
|
|
|
|
disabledHours?: Function;
|
|
|
|
/** 禁止选择部分分钟选项 */
|
|
|
|
disabledMinutes?: Function;
|
|
|
|
/** 禁止选择部分秒选项 */
|
|
|
|
disabledSeconds?: Function;
|
2016-07-09 10:52:58 +08:00
|
|
|
|
2016-07-13 11:14:24 +08:00
|
|
|
style?: React.CSSProperties;
|
2016-09-19 10:17:07 +08:00
|
|
|
getPopupContainer?: (trigger: any) => any;
|
2016-07-09 10:52:58 +08:00
|
|
|
}
|
2016-08-22 17:26:14 +08:00
|
|
|
|
|
|
|
export interface TimePickerContext {
|
|
|
|
antLocale?: {
|
|
|
|
TimePicker?: any,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-07-09 10:52:58 +08:00
|
|
|
export default class TimePicker extends React.Component<TimePickerProps, any> {
|
2016-03-29 17:33:37 +08:00
|
|
|
static defaultProps = {
|
|
|
|
format: 'HH:mm:ss',
|
|
|
|
prefixCls: 'ant-time-picker',
|
|
|
|
onChange() {
|
|
|
|
},
|
|
|
|
align: {
|
|
|
|
offset: [0, -2],
|
|
|
|
},
|
|
|
|
disabled: false,
|
|
|
|
disabledHours: undefined,
|
|
|
|
disabledMinutes: undefined,
|
|
|
|
disabledSeconds: undefined,
|
|
|
|
hideDisabledOptions: false,
|
|
|
|
placement: 'bottomLeft',
|
|
|
|
transitionName: 'slide-up',
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2015-11-16 23:11:41 +08:00
|
|
|
|
2016-03-29 17:33:37 +08:00
|
|
|
static contextTypes = {
|
2016-03-05 16:39:27 +08:00
|
|
|
antLocale: React.PropTypes.object,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-03 17:43:38 +08:00
|
|
|
|
2016-08-22 17:26:14 +08:00
|
|
|
context: TimePickerContext;
|
|
|
|
|
2016-09-09 13:55:21 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2015-12-08 14:55:28 +08:00
|
|
|
|
2016-09-09 13:55:21 +08:00
|
|
|
this.state = {
|
|
|
|
value: props.value || props.defaultValue,
|
|
|
|
};
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
2015-11-16 23:11:41 +08:00
|
|
|
|
2016-09-09 13:55:21 +08:00
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if ('value' in nextProps) {
|
|
|
|
this.setState({ value: nextProps.value });
|
2015-11-16 23:11:41 +08:00
|
|
|
}
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
2015-11-18 12:14:26 +08:00
|
|
|
|
2016-09-09 13:55:21 +08:00
|
|
|
handleChange = (value: moment.Moment) => {
|
|
|
|
if (!('value' in this.props)) {
|
|
|
|
this.setState({ value });
|
|
|
|
}
|
|
|
|
this.props.onChange(value, (value && value.format(this.props.format)) || '');
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
2015-11-19 15:53:40 +08:00
|
|
|
|
2015-11-19 18:01:46 +08:00
|
|
|
getLocale() {
|
2016-09-09 13:55:21 +08:00
|
|
|
const antLocale = this.context.antLocale;
|
|
|
|
const timePickerLocale = (antLocale && antLocale.TimePicker) || defaultLocale;
|
|
|
|
return timePickerLocale;
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
2015-11-19 18:01:46 +08:00
|
|
|
|
2015-11-18 12:14:26 +08:00
|
|
|
render() {
|
2016-06-22 13:18:43 +08:00
|
|
|
const props = assign({}, this.props);
|
2016-09-09 13:55:21 +08:00
|
|
|
delete props.defaultValue;
|
|
|
|
|
|
|
|
const className = classNames({
|
2015-12-08 14:55:28 +08:00
|
|
|
[props.className]: !!props.className,
|
2016-03-23 16:25:06 +08:00
|
|
|
[`${props.prefixCls}-${props.size}`]: !!props.size,
|
2015-12-08 14:55:28 +08:00
|
|
|
});
|
2015-12-16 17:29:28 +08:00
|
|
|
|
2015-11-16 23:11:41 +08:00
|
|
|
return (
|
2016-03-21 21:16:38 +08:00
|
|
|
<RcTimePicker
|
2015-12-08 14:55:28 +08:00
|
|
|
{...props}
|
|
|
|
className={className}
|
2016-09-09 13:55:21 +08:00
|
|
|
value={this.state.value}
|
|
|
|
placeholder={props.placeholder || this.getLocale().placeholder}
|
|
|
|
showHour={props.format.indexOf('HH') > -1}
|
|
|
|
showSecond={props.format.indexOf('ss') > -1}
|
2015-11-19 15:53:40 +08:00
|
|
|
onChange={this.handleChange}
|
2016-09-09 13:55:21 +08:00
|
|
|
/>
|
2015-11-16 23:11:41 +08:00
|
|
|
);
|
|
|
|
}
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|