2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import * as moment from 'moment';
|
2016-03-21 21:16:38 +08:00
|
|
|
import RcTimePicker from 'rc-time-picker/lib/TimePicker';
|
2015-12-08 14:55:28 +08:00
|
|
|
import classNames from 'classnames';
|
2017-10-20 14:54:38 +08:00
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
2017-09-26 23:12:47 +08:00
|
|
|
import defaultLocale from './locale/en_US';
|
2018-03-13 22:53:02 +08:00
|
|
|
import interopDefault from '../_util/interopDefault';
|
2016-07-09 10:52:58 +08:00
|
|
|
|
2017-06-29 11:22:12 +08:00
|
|
|
export function generateShowHourMinuteSecond(format: string) {
|
|
|
|
// Ref: http://momentjs.com/docs/#/parsing/string-format/
|
|
|
|
return {
|
|
|
|
showHour: (
|
|
|
|
format.indexOf('H') > -1 ||
|
|
|
|
format.indexOf('h') > -1 ||
|
|
|
|
format.indexOf('k') > -1
|
|
|
|
),
|
|
|
|
showMinute: format.indexOf('m') > -1,
|
|
|
|
showSecond: format.indexOf('s') > -1,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-07-09 10:52:58 +08:00
|
|
|
export interface TimePickerProps {
|
2016-09-09 13:55:21 +08:00
|
|
|
className?: string;
|
|
|
|
size?: 'large' | 'default' | 'small';
|
|
|
|
value?: moment.Moment;
|
2018-01-09 22:35:53 +08:00
|
|
|
defaultValue?: moment.Moment | moment.Moment[];
|
2017-04-28 09:40:20 +08:00
|
|
|
open?: boolean;
|
2016-07-13 11:14:24 +08:00
|
|
|
format?: string;
|
2016-09-09 13:55:21 +08:00
|
|
|
onChange?: (time: moment.Moment, timeString: string) => void;
|
2017-04-28 09:48:48 +08:00
|
|
|
onOpenChange?: (open: boolean) => void;
|
2016-07-13 11:14:24 +08:00
|
|
|
disabled?: boolean;
|
|
|
|
placeholder?: string;
|
2017-07-03 16:57:11 +08:00
|
|
|
prefixCls?: string;
|
2016-07-13 11:14:24 +08:00
|
|
|
hideDisabledOptions?: boolean;
|
2017-03-17 15:23:25 +08:00
|
|
|
disabledHours?: () => number[];
|
|
|
|
disabledMinutes?: (selectedHour: number) => number[];
|
|
|
|
disabledSeconds?: (selectedHour: number, selectedMinute: number) => number[];
|
2016-07-13 11:14:24 +08:00
|
|
|
style?: React.CSSProperties;
|
2017-03-28 13:20:05 +08:00
|
|
|
getPopupContainer?: (triggerNode: Element) => HTMLElement;
|
2016-11-06 14:53:29 +08:00
|
|
|
addon?: Function;
|
2017-03-22 17:53:37 +08:00
|
|
|
use12Hours?: boolean;
|
2017-10-28 15:00:56 +08:00
|
|
|
focusOnOpen?: boolean;
|
|
|
|
hourStep?: number;
|
|
|
|
minuteStep?: number;
|
|
|
|
secondStep?: number;
|
2017-11-03 14:55:06 +08:00
|
|
|
allowEmpty?: boolean;
|
2018-02-11 10:22:18 +08:00
|
|
|
inputReadOnly?: boolean;
|
2017-11-03 14:55:06 +08:00
|
|
|
clearText?: string;
|
|
|
|
defaultOpenValue?: moment.Moment;
|
|
|
|
popupClassName?: string;
|
2016-07-09 10:52:58 +08:00
|
|
|
}
|
2016-08-22 17:26:14 +08:00
|
|
|
|
2017-11-20 18:27:36 +08:00
|
|
|
export interface TimePickerLocale {
|
|
|
|
placeholder: string;
|
|
|
|
}
|
|
|
|
|
2017-10-20 14:54:38 +08:00
|
|
|
export default class TimePicker extends React.Component<TimePickerProps, any> {
|
2016-03-29 17:33:37 +08:00
|
|
|
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',
|
2017-10-28 15:00:56 +08:00
|
|
|
focusOnOpen: true,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2015-11-16 23:11:41 +08:00
|
|
|
|
2017-11-20 18:27:36 +08:00
|
|
|
private timePickerRef: typeof RcTimePicker;
|
2016-08-22 17:26:14 +08:00
|
|
|
|
2017-03-17 15:23:25 +08:00
|
|
|
constructor(props: TimePickerProps) {
|
2016-09-09 13:55:21 +08:00
|
|
|
super(props);
|
2016-12-02 15:07:33 +08:00
|
|
|
const value = props.value || props.defaultValue;
|
2018-03-13 22:53:02 +08:00
|
|
|
if (value && !interopDefault(moment).isMoment(value)) {
|
2016-12-02 15:07:33 +08:00
|
|
|
throw new Error(
|
|
|
|
'The value/defaultValue of TimePicker must be a moment object after `antd@2.0`, ' +
|
2017-07-19 13:59:53 +08:00
|
|
|
'see: https://u.ant.design/time-picker-value',
|
2016-12-02 15:07:33 +08:00
|
|
|
);
|
|
|
|
}
|
2016-09-09 13:55:21 +08:00
|
|
|
this.state = {
|
2016-12-02 15:07:33 +08:00
|
|
|
value,
|
2016-09-09 13:55:21 +08:00
|
|
|
};
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
2015-11-16 23:11:41 +08:00
|
|
|
|
2017-03-17 15:23:25 +08:00
|
|
|
componentWillReceiveProps(nextProps: TimePickerProps) {
|
2016-09-09 13:55:21 +08:00
|
|
|
if ('value' in nextProps) {
|
|
|
|
this.setState({ value: nextProps.value });
|
2015-11-16 23:11:41 +08:00
|
|
|
}
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
2015-11-18 12:14:26 +08:00
|
|
|
|
2016-09-09 13:55:21 +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)) || '');
|
|
|
|
}
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
2015-11-19 15:53:40 +08:00
|
|
|
|
2017-11-20 18:27:36 +08:00
|
|
|
handleOpenClose = ({ open }: { open: boolean }) => {
|
2017-04-28 09:40:20 +08:00
|
|
|
const { onOpenChange } = this.props;
|
|
|
|
if (onOpenChange) {
|
|
|
|
onOpenChange(open);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 18:27:36 +08:00
|
|
|
saveTimePicker = (timePickerRef: typeof RcTimePicker) => {
|
2017-02-21 15:10:28 +08:00
|
|
|
this.timePickerRef = timePickerRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
focus() {
|
|
|
|
this.timePickerRef.focus();
|
|
|
|
}
|
|
|
|
|
2017-11-19 01:41:40 +08:00
|
|
|
blur() {
|
|
|
|
this.timePickerRef.blur();
|
|
|
|
}
|
|
|
|
|
2017-03-22 17:53:37 +08:00
|
|
|
getDefaultFormat() {
|
|
|
|
const { format, use12Hours } = this.props;
|
|
|
|
if (format) {
|
|
|
|
return format;
|
|
|
|
} else if (use12Hours) {
|
|
|
|
return 'h:mm:ss a';
|
|
|
|
}
|
|
|
|
return 'HH:mm:ss';
|
|
|
|
}
|
|
|
|
|
2017-11-20 18:27:36 +08:00
|
|
|
renderTimePicker = (locale: TimePickerLocale) => {
|
2017-07-03 16:57:11 +08:00
|
|
|
const props = {
|
|
|
|
...this.props,
|
|
|
|
};
|
2016-09-09 13:55:21 +08:00
|
|
|
delete props.defaultValue;
|
|
|
|
|
2017-03-22 17:53:37 +08:00
|
|
|
const format = this.getDefaultFormat();
|
2016-11-30 10:20:23 +08:00
|
|
|
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
|
|
|
|
2017-11-20 18:27:36 +08:00
|
|
|
const addon = (panel: React.ReactElement<any>) => (
|
2016-11-25 12:03:39 +08:00
|
|
|
props.addon ? (
|
|
|
|
<div className={`${props.prefixCls}-panel-addon`}>
|
|
|
|
{props.addon(panel)}
|
|
|
|
</div>
|
|
|
|
) : null
|
|
|
|
);
|
|
|
|
|
2015-11-16 23:11:41 +08:00
|
|
|
return (
|
2016-03-21 21:16:38 +08:00
|
|
|
<RcTimePicker
|
2017-06-29 11:22:12 +08:00
|
|
|
{...generateShowHourMinuteSecond(format)}
|
2015-12-08 14:55:28 +08:00
|
|
|
{...props}
|
2017-02-21 15:10:28 +08:00
|
|
|
ref={this.saveTimePicker}
|
2017-03-22 17:53:37 +08:00
|
|
|
format={format}
|
2015-12-08 14:55:28 +08:00
|
|
|
className={className}
|
2016-09-09 13:55:21 +08:00
|
|
|
value={this.state.value}
|
2017-10-20 14:54:38 +08:00
|
|
|
placeholder={props.placeholder === undefined ? locale.placeholder : props.placeholder}
|
2015-11-19 15:53:40 +08:00
|
|
|
onChange={this.handleChange}
|
2017-04-28 09:40:20 +08:00
|
|
|
onOpen={this.handleOpenClose}
|
|
|
|
onClose={this.handleOpenClose}
|
2016-11-25 12:03:39 +08:00
|
|
|
addon={addon}
|
2016-09-09 13:55:21 +08:00
|
|
|
/>
|
2015-11-16 23:11:41 +08:00
|
|
|
);
|
|
|
|
}
|
2017-03-17 15:23:25 +08:00
|
|
|
|
2017-10-20 14:54:38 +08:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<LocaleReceiver
|
|
|
|
componentName="TimePicker"
|
|
|
|
defaultLocale={defaultLocale}
|
|
|
|
>
|
|
|
|
{this.renderTimePicker}
|
|
|
|
</LocaleReceiver>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|