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

118 lines
3.0 KiB
TypeScript
Raw Normal View History

2016-07-07 20:25:03 +08:00
import * as React from 'react';
2015-11-16 23:11:41 +08:00
import DateTimeFormat from 'gregorian-calendar-format';
import RcTimePicker from 'rc-time-picker/lib/TimePicker';
2015-11-19 18:01:46 +08:00
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';
2016-06-22 13:18:43 +08:00
import assign from 'object-assign';
export default class TimePicker extends React.Component {
static defaultProps = {
format: 'HH:mm:ss',
prefixCls: 'ant-time-picker',
onChange() {
},
locale: {},
align: {
offset: [0, -2],
},
disabled: false,
disabledHours: undefined,
disabledMinutes: undefined,
disabledSeconds: undefined,
hideDisabledOptions: false,
placement: 'bottomLeft',
transitionName: 'slide-up',
}
2015-11-16 23:11:41 +08:00
static contextTypes = {
2016-03-05 16:39:27 +08:00
antLocale: React.PropTypes.object,
}
2016-03-03 17:43:38 +08:00
2015-12-08 14:55:28 +08:00
getFormatter() {
return new DateTimeFormat(this.props.format, this.getLocale().format);
}
2015-12-08 14:55:28 +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
/**
*
*/
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
handleChange = (value) => {
this.props.onChange(
value ? new Date(value.getTime()) : null,
value ? this.getFormatter().format(value) : '',
);
}
2015-11-19 15:53:40 +08:00
2015-11-19 18:01:46 +08:00
getLocale() {
2016-03-03 17:43:38 +08:00
let locale = defaultLocale;
2016-03-05 16:39:27 +08:00
if (this.context.antLocale && this.context.antLocale.TimePicker) {
locale = this.context.antLocale.TimePicker;
2016-03-03 17:43:38 +08:00
}
2015-11-19 18:01:46 +08:00
// 统一合并为完整的 Locale
2016-06-22 13:18:43 +08:00
return assign({}, locale, this.props.locale);
}
2015-11-19 18:01:46 +08:00
2015-11-18 12:14:26 +08:00
render() {
2016-03-03 17:43:38 +08:00
const locale = this.getLocale();
2016-06-22 13:18:43 +08:00
const props = assign({}, this.props);
2015-12-08 14:55:28 +08:00
props.placeholder = ('placeholder' in this.props)
2016-03-03 17:43:38 +08:00
? props.placeholder : locale.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 (
<RcTimePicker
2015-12-08 14:55:28 +08:00
{...props}
className={className}
2016-03-03 17:43:38 +08:00
locale={locale}
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
);
}
}