ant-design/components/timepicker/index.jsx

90 lines
2.1 KiB
React
Raw Normal View History

2015-11-16 23:11:41 +08:00
import React from 'react';
import DateTimeFormat from 'gregorian-calendar-format';
import TimePicker from 'rc-time-picker/lib/TimePicker';
2015-11-19 18:01:46 +08:00
import objectAssign from 'object-assign';
import defaultLocale from './locale/zh_CN';
2015-11-16 23:11:41 +08:00
const AntTimepicker = React.createClass({
2015-11-16 23:11:41 +08:00
getDefaultProps() {
return {
format: 'HH:mm:ss',
2015-11-19 14:28:54 +08:00
prefixCls: 'ant-timepicker',
2015-11-19 15:53:40 +08:00
onChange() {},
2015-11-16 23:11:41 +08:00
locale: {},
align: {
offset: [0, -2],
2015-11-16 23:11:41 +08:00
},
open: false,
disabled: false,
hourOptions: undefined,
minuteOptions: undefined,
secondOptions: undefined,
2015-11-18 12:14:26 +08:00
size: '',
placement: 'bottomLeft',
transitionName: 'slide-up',
2015-11-16 23:11:41 +08:00
};
},
2015-11-18 12:14:26 +08:00
/**
* 获得输入框的 className
*/
getSizeClass() {
let sizeClass = '';
if (this.props.size === 'large') {
sizeClass = ' ant-input-lg';
} else if (this.props.size === 'small') {
sizeClass = ' ant-input-sm';
}
return sizeClass;
},
2015-11-16 23:11:41 +08:00
2015-11-18 12:14:26 +08:00
/**
* 获得输入框的默认值
*/
getDefaultValue(formatter) {
const defaultValue = this.props.defaultValue;
2015-11-16 23:11:41 +08:00
if (defaultValue) {
2015-11-18 12:14:26 +08:00
return formatter.parse(defaultValue, {
2015-11-19 18:01:46 +08:00
locale: this.getLocale(),
2015-11-16 23:11:41 +08:00
obeyCount: true,
});
}
2015-11-18 12:14:26 +08:00
return undefined;
},
2015-11-19 15:53:40 +08:00
handleChange(value) {
let args = null;
if (value) {
args = new Date(value.getTime());
}
this.props.onChange(args);
2015-11-19 15:53:40 +08:00
},
2015-11-19 18:01:46 +08:00
getLocale() {
// 统一合并为完整的 Locale
let locale = objectAssign({}, defaultLocale, this.props.locale);
locale.lang = objectAssign({}, defaultLocale.lang, this.props.locale.lang);
return locale;
},
2015-11-18 12:14:26 +08:00
render() {
2015-11-19 14:28:54 +08:00
const { format } = this.props;
2015-11-18 12:14:26 +08:00
const formatter = new DateTimeFormat(format);
2015-11-19 19:28:36 +08:00
const placeholder = ('placeholder' in this.props)
? this.props.placeholder : this.getLocale().lang.placeholder;
2015-11-16 23:11:41 +08:00
return (
2015-11-18 12:14:26 +08:00
<TimePicker
2015-11-19 14:28:54 +08:00
{...this.props}
2015-11-18 12:14:26 +08:00
inputClassName={`ant-input ${this.getSizeClass()}`}
2015-11-19 14:28:54 +08:00
formatter={formatter}
2015-11-18 12:14:26 +08:00
defaultValue={this.getDefaultValue(formatter)}
2015-11-19 19:15:52 +08:00
placeholder={placeholder}
2015-11-19 15:53:40 +08:00
onChange={this.handleChange}
2015-11-18 12:14:26 +08:00
/>
2015-11-16 23:11:41 +08:00
);
}
});
export default AntTimepicker;