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

206 lines
5.9 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import * as moment from 'moment';
2018-08-08 14:47:13 +08:00
import { polyfill } from 'react-lifecycles-compat';
import RcTimePicker from 'rc-time-picker/lib/TimePicker';
2015-12-08 14:55:28 +08:00
import classNames from 'classnames';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
2018-11-26 12:06:42 +08:00
import { ConfigConsumer, ConfigProviderProps } from '../config-provider';
import defaultLocale from './locale/en_US';
2018-03-13 22:53:02 +08:00
import interopDefault from '../_util/interopDefault';
2018-08-14 14:45:04 +08:00
import Icon from '../icon';
2016-07-09 10:52:58 +08:00
export function generateShowHourMinuteSecond(format: string) {
// Ref: http://momentjs.com/docs/#/parsing/string-format/
return {
2018-12-07 16:17:45 +08:00
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 {
className?: string;
size?: 'large' | 'default' | 'small';
value?: moment.Moment;
2018-01-09 22:35:53 +08:00
defaultValue?: moment.Moment | moment.Moment[];
open?: boolean;
2016-07-13 11:14:24 +08:00
format?: string;
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;
prefixCls?: string;
2016-07-13 11:14:24 +08:00
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?: (triggerNode: Element) => HTMLElement;
2016-11-06 14:53:29 +08:00
addon?: Function;
use12Hours?: boolean;
focusOnOpen?: boolean;
hourStep?: number;
minuteStep?: number;
secondStep?: number;
allowEmpty?: boolean;
inputReadOnly?: boolean;
clearText?: string;
defaultOpenValue?: moment.Moment;
popupClassName?: string;
2018-09-16 19:45:27 +08:00
suffixIcon?: React.ReactNode;
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;
}
2018-08-08 14:47:13 +08:00
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',
focusOnOpen: true,
2016-07-13 11:14:24 +08:00
};
2015-11-16 23:11:41 +08:00
2018-08-08 14:47:13 +08:00
static getDerivedStateFromProps(nextProps: TimePickerProps) {
if ('value' in nextProps) {
return { value: nextProps.value };
}
return null;
}
2017-11-20 18:27:36 +08:00
private timePickerRef: typeof RcTimePicker;
2016-08-22 17:26:14 +08:00
constructor(props: TimePickerProps) {
super(props);
const value = props.value || props.defaultValue;
2018-03-13 22:53:02 +08:00
if (value && !interopDefault(moment).isMoment(value)) {
throw new Error(
'The value/defaultValue of TimePicker must be a moment object after `antd@2.0`, ' +
2018-12-07 16:17:45 +08:00
'see: https://u.ant.design/time-picker-value',
);
}
this.state = {
value,
};
}
2015-11-16 23:11:41 +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)) || '');
}
2018-12-07 16:17:45 +08:00
};
2015-11-19 15:53:40 +08:00
2017-11-20 18:27:36 +08:00
handleOpenClose = ({ open }: { open: boolean }) => {
const { onOpenChange } = this.props;
if (onOpenChange) {
onOpenChange(open);
}
2018-12-07 16:17:45 +08:00
};
2017-11-20 18:27:36 +08:00
saveTimePicker = (timePickerRef: typeof RcTimePicker) => {
this.timePickerRef = timePickerRef;
2018-12-07 16:17:45 +08:00
};
focus() {
this.timePickerRef.focus();
}
blur() {
this.timePickerRef.blur();
}
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) => {
2018-11-26 12:06:42 +08:00
const { getPopupContainer, ...props } = this.props;
delete props.defaultValue;
const format = this.getDefaultFormat();
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
2018-12-07 16:17:45 +08:00
const addon = (panel: React.ReactElement<any>) =>
props.addon ? (
2018-12-07 16:17:45 +08:00
<div className={`${props.prefixCls}-panel-addon`}>{props.addon(panel)}</div>
) : null;
2018-09-16 19:45:27 +08:00
const { suffixIcon, prefixCls } = props;
2018-12-07 16:17:45 +08:00
const clockIcon = (suffixIcon &&
(React.isValidElement<{ className?: string }>(suffixIcon) ? (
React.cloneElement(suffixIcon, {
className: classNames({
[suffixIcon.props.className!]: suffixIcon.props.className,
[`${prefixCls}-clock-icon`]: true,
}),
})
) : (
<span className={`${prefixCls}-clock-icon`}>{suffixIcon}</span>
))) || <Icon type="clock-circle" className={`${prefixCls}-clock-icon`} theme="outlined" />;
const inputIcon = <span className={`${prefixCls}-icon`}>{clockIcon}</span>;
2018-08-14 14:45:04 +08:00
const clearIcon = (
2018-12-07 16:17:45 +08:00
<Icon type="close-circle" className={`${prefixCls}-panel-clear-btn-icon`} theme="filled" />
2018-08-14 14:45:04 +08:00
);
2015-11-16 23:11:41 +08:00
return (
2018-11-26 12:06:42 +08:00
<ConfigConsumer>
{({ getPopupContainer: getContextPopupContainer }: ConfigProviderProps) => {
return (
<RcTimePicker
{...generateShowHourMinuteSecond(format)}
{...props}
getPopupContainer={getPopupContainer || getContextPopupContainer}
ref={this.saveTimePicker}
format={format}
className={className}
value={this.state.value}
placeholder={props.placeholder === undefined ? locale.placeholder : props.placeholder}
onChange={this.handleChange}
onOpen={this.handleOpenClose}
onClose={this.handleOpenClose}
addon={addon}
inputIcon={inputIcon}
clearIcon={clearIcon}
/>
);
}}
</ConfigConsumer>
2015-11-16 23:11:41 +08:00
);
2018-12-07 16:17:45 +08:00
};
render() {
return (
2018-12-07 16:17:45 +08:00
<LocaleReceiver componentName="TimePicker" defaultLocale={defaultLocale}>
{this.renderTimePicker}
</LocaleReceiver>
);
}
}
2018-08-08 14:47:13 +08:00
polyfill(TimePicker);
export default TimePicker;