ant-design/components/time-picker/index.jsx

112 lines
2.7 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-12-08 14:55:28 +08:00
import classNames from 'classnames';
2016-01-14 19:15:21 +08:00
import GregorianCalendar from 'gregorian-calendar';
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-12-11 19:58:41 +08:00
prefixCls: 'ant-time-picker',
onChange() {
},
2015-11-16 23:11:41 +08:00
locale: {},
align: {
offset: [0, -2],
2015-11-16 23:11:41 +08:00
},
disabled: false,
2015-12-16 17:29:28 +08:00
disabledHours: undefined,
disabledMinutes: undefined,
disabledSeconds: undefined,
hideDisabledOptions: false,
2015-11-18 12:14:26 +08:00
placement: 'bottomLeft',
transitionName: 'slide-up',
2015-11-16 23:11:41 +08:00
};
},
2015-12-08 14:55:28 +08:00
getFormatter() {
return new DateTimeFormat(this.props.format);
},
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
/**
* 获得输入框的默认值
*/
2015-12-08 14:55:28 +08:00
parseTimeFromValue(value) {
if (value) {
2016-01-14 19:15:21 +08:00
if (typeof value === 'string') {
return this.getFormatter().parse(value, {
locale: this.getLocale().calendar,
obeyCount: true,
});
} else if (value instanceof Date) {
let date = new GregorianCalendar(this.getLocale().calendar);
date.setTime(+value);
return date;
}
2015-11-16 23:11:41 +08:00
}
2016-01-14 19:15:21 +08:00
return value;
2015-11-18 12:14:26 +08:00
},
2015-11-19 15:53:40 +08:00
handleChange(value) {
2015-12-08 14:55:28 +08:00
this.props.onChange(value ? new Date(value.getTime()) : null);
2015-11-19 15:53:40 +08:00
},
2015-11-19 18:01:46 +08:00
getLocale() {
// 统一合并为完整的 Locale
return objectAssign({}, defaultLocale, this.props.locale);
2015-11-19 18:01:46 +08:00
},
2015-11-18 12:14:26 +08:00
render() {
2015-12-08 14:55:28 +08:00
const props = objectAssign({}, this.props);
props.placeholder = ('placeholder' in this.props)
? props.placeholder : this.getLocale().placeholder;
2015-12-08 14:55:28 +08:00
if (props.defaultValue) {
props.defaultValue = this.parseTimeFromValue(props.defaultValue);
} else {
delete props.defaultValue;
}
if (props.value) {
props.value = this.parseTimeFromValue(props.value);
}
let className = classNames({
[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-08 21:23:07 +08:00
if (props.format.indexOf('ss') < 0) {
props.showSecond = false;
}
if (props.format.indexOf('HH') < 0) {
props.showHour = false;
}
2015-12-16 17:29:28 +08:00
2015-11-16 23:11:41 +08:00
return (
2015-11-18 12:14:26 +08:00
<TimePicker
2015-12-08 14:55:28 +08:00
{...props}
className={className}
locale={this.getLocale()}
2015-12-08 14:55:28 +08:00
formatter={this.getFormatter()}
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;