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

108 lines
2.5 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';
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-12-08 14:55:28 +08:00
size: 'default',
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) {
return this.getFormatter().parse(value, {
locale: this.getLocale().calendar,
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) {
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);
} else {
delete props.value;
}
let className = classNames({
[props.className]: !!props.className,
[props.prefixCls + '-' + props.size]: true,
});
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;