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

119 lines
3.2 KiB
TypeScript
Raw Normal View History

import React from 'react';
import moment from 'moment';
import RcTimePicker from 'rc-time-picker/lib/TimePicker';
2015-12-08 14:55:28 +08:00
import classNames from 'classnames';
2016-06-22 13:18:43 +08:00
import assign from 'object-assign';
import injectLocale from '../locale-provider/injectLocale';
import defaultLocale from './locale/zh_CN';
2016-07-09 10:52:58 +08:00
export interface TimePickerProps {
className?: string;
size?: 'large' | 'default' | 'small';
value?: moment.Moment;
defaultValue?: moment.Moment;
2016-07-13 11:14:24 +08:00
format?: string;
onChange?: (time: moment.Moment, timeString: string) => void;
2016-07-13 11:14:24 +08:00
disabled?: boolean;
placeholder?: string;
hideDisabledOptions?: boolean;
disabledHours?: () => number[];
disabledMinutes?: (selectedHour: number) => number[];
disabledSeconds?: (selectedHour: number, selectedMinute: number) => number[];
2016-07-13 11:14:24 +08:00
style?: React.CSSProperties;
getPopupContainer?: (trigger: any) => any;
2016-11-06 14:53:29 +08:00
addon?: Function;
2016-07-09 10:52:58 +08:00
}
2016-08-22 17:26:14 +08:00
abstract class TimePicker extends React.Component<TimePickerProps, any> {
static defaultProps = {
prefixCls: 'ant-time-picker',
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
timePickerRef: any;
2016-08-22 17:26:14 +08:00
constructor(props: TimePickerProps) {
super(props);
const value = props.value || props.defaultValue;
if (value && !moment.isMoment(value)) {
throw new Error(
'The value/defaultValue of TimePicker must be a moment object after `antd@2.0`, ' +
'see: http://u.ant.design/time-picker-value'
);
}
this.state = {
value,
};
}
2015-11-16 23:11:41 +08:00
abstract getLocale()
componentWillReceiveProps(nextProps: TimePickerProps) {
if ('value' in nextProps) {
this.setState({ value: nextProps.value });
2015-11-16 23:11:41 +08:00
}
}
2015-11-18 12:14:26 +08:00
handleChange = (value: moment.Moment) => {
if (!('value' in this.props)) {
this.setState({ value });
}
2016-10-24 16:30:38 +08:00
const { onChange, format = 'HH:mm:ss' } = this.props;
if (onChange) {
onChange(value, (value && value.format(format)) || '');
}
}
2015-11-19 15:53:40 +08:00
saveTimePicker = (timePickerRef) => {
this.timePickerRef = timePickerRef;
}
focus() {
this.timePickerRef.focus();
}
2015-11-18 12:14:26 +08:00
render() {
const props = assign({ format: 'HH:mm:ss' }, this.props);
delete props.defaultValue;
const className = classNames(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-16 17:29:28 +08:00
const addon = (panel) => (
props.addon ? (
<div className={`${props.prefixCls}-panel-addon`}>
{props.addon(panel)}
</div>
) : null
);
2015-11-16 23:11:41 +08:00
return (
<RcTimePicker
2015-12-08 14:55:28 +08:00
{...props}
ref={this.saveTimePicker}
2015-12-08 14:55:28 +08:00
className={className}
value={this.state.value}
placeholder={props.placeholder === undefined ? this.getLocale().placeholder : props.placeholder}
showHour={props.format.indexOf('HH') > -1}
showMinute={props.format.indexOf('mm') > -1}
showSecond={props.format.indexOf('ss') > -1}
2015-11-19 15:53:40 +08:00
onChange={this.handleChange}
addon={addon}
/>
2015-11-16 23:11:41 +08:00
);
}
}
const injectTimePickerLocale = injectLocale('TimePicker', defaultLocale);
export default injectTimePickerLocale<TimePickerProps>(TimePicker as any);