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

146 lines
3.8 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';
2016-07-09 10:52:58 +08:00
// TimePicker
export interface TimePickerProps {
2016-07-13 11:14:24 +08:00
/** 默认时间 */
value?: string | Date;
/** 初始默认时间 */
defaultValue?: string | Date;
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;
/** 时间发生变化的回调 */
onChange?: (Date: Date) => void;
/** 禁用全部操作 */
disabled?: boolean;
/** 没有值的时候显示的内容 */
placeholder?: string;
/** 国际化配置 */
locale?: Object;
/** 隐藏禁止选择的选项 */
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-07-09 10:52:58 +08:00
}
export default class TimePicker extends React.Component<TimePickerProps, any> {
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',
2016-07-13 11:14:24 +08:00
};
2015-11-16 23:11:41 +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
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}
2016-07-09 10:52:58 +08:00
formatter={this.getFormatter() }
2015-11-19 15:53:40 +08:00
onChange={this.handleChange}
2016-07-09 10:52:58 +08:00
/>
2015-11-16 23:11:41 +08:00
);
}
}