mirror of
https://github.com/ant-design/ant-design.git
synced 2025-01-08 12:28:19 +08:00
7dd4a019d5
+ DatePicker showTime: 面板上的日期展示输入框统一为一个,格式和外面的输入框同步,并且支持手动修改。 面板上的 TimePicker + DatePicker showTime: 输入框不再展示,改造为『选择时间』,点击后不再展开浮层,直接盖住日期区域。 + RangePicker showTime : 只选中开始日期,“确定”和“选择时间”灰置。 如未选择日期直接选择时间,开始日期和结束日期默认选中当天。 + RangePicker showTime : 点击框外和确定均为确定操作。 + RangePicker showTime : 在时间页面,开始时间的默认状态为当前时间,结束时间的默认状态跟随开始时间。 + RangePicker showTime : 开始时间的选择范围没有限制,结束时间的选择范围必须大于等于开始时间。 + RangePicker showTime : 当开始时间选择了结束时间之后的时间(发生冲突),结束时间则自动切换到与开始时间相同的时间。
116 lines
3.3 KiB
TypeScript
116 lines
3.3 KiB
TypeScript
import { PropTypes } from 'react';
|
|
import * as React from 'react';
|
|
import TimePickerPanel from 'rc-time-picker/lib/module/Panel';
|
|
import DateTimeFormat from 'gregorian-calendar-format';
|
|
import GregorianCalendar from 'gregorian-calendar';
|
|
import classNames from 'classnames';
|
|
import defaultLocale from './locale/zh_CN';
|
|
import assign from 'object-assign';
|
|
export default function wrapPicker(Picker, defaultFormat) {
|
|
const PickerWrapper = React.createClass({
|
|
getDefaultProps() {
|
|
return {
|
|
format: defaultFormat || 'yyyy-MM-dd',
|
|
transitionName: 'slide-up',
|
|
popupStyle: {},
|
|
onChange() {
|
|
},
|
|
onOk() {
|
|
},
|
|
toggleOpen() {
|
|
},
|
|
locale: {},
|
|
align: {
|
|
offset: [0, -9],
|
|
},
|
|
};
|
|
},
|
|
|
|
contextTypes: {
|
|
antLocale: PropTypes.object,
|
|
},
|
|
|
|
getLocale() {
|
|
const props = this.props;
|
|
let locale = defaultLocale;
|
|
const context = this.context;
|
|
if (context.antLocale && context.antLocale.DatePicker) {
|
|
locale = context.antLocale.DatePicker;
|
|
}
|
|
// 统一合并为完整的 Locale
|
|
const result = assign({}, locale, props.locale);
|
|
result.lang = assign({}, locale.lang, props.locale.lang);
|
|
return result;
|
|
},
|
|
|
|
getFormatter() {
|
|
const format = this.props.format;
|
|
const formatter = new DateTimeFormat(format, this.getLocale().lang.format);
|
|
return formatter;
|
|
},
|
|
|
|
parseDateFromValue(value) {
|
|
if (value) {
|
|
if (typeof value === 'string') {
|
|
return this.getFormatter().parse(value, {locale: this.getLocale()});
|
|
} else if (value instanceof Date) {
|
|
let date = new GregorianCalendar(this.getLocale());
|
|
date.setTime(+value);
|
|
return date;
|
|
}
|
|
}
|
|
return value;
|
|
},
|
|
|
|
toggleOpen ({open}) {
|
|
this.props.toggleOpen({open});
|
|
},
|
|
|
|
render() {
|
|
const props = this.props;
|
|
const pickerClass = classNames({
|
|
'ant-calendar-picker': true,
|
|
});
|
|
const pickerInputClass = classNames({
|
|
'ant-calendar-range-picker': true,
|
|
'ant-input': true,
|
|
'ant-input-lg': props.size === 'large',
|
|
'ant-input-sm': props.size === 'small',
|
|
});
|
|
|
|
const locale = this.getLocale();
|
|
|
|
const timeFormat = props.showTime && props.showTime.format;
|
|
const rcTimePickerProps = {
|
|
formatter: new DateTimeFormat(timeFormat || 'HH:mm:ss', locale.timePickerLocale.format),
|
|
showSecond: timeFormat && timeFormat.indexOf('ss') >= 0,
|
|
showHour: timeFormat && timeFormat.indexOf('HH') >= 0,
|
|
};
|
|
const timePicker = props.showTime ? (
|
|
<TimePickerPanel
|
|
{...rcTimePickerProps}
|
|
{...props.showTime}
|
|
prefixCls="ant-calendar-time-picker"
|
|
placeholder={locale.timePickerLocale.placeholder}
|
|
locale={locale.timePickerLocale}
|
|
transitionName="slide-up"
|
|
/>
|
|
) : null;
|
|
|
|
return (
|
|
<Picker
|
|
{...props}
|
|
pickerClass={pickerClass}
|
|
pickerInputClass={pickerInputClass}
|
|
locale={locale}
|
|
timePicker={timePicker}
|
|
toggleOpen={this.toggleOpen}
|
|
getFormatter={this.getFormatter}
|
|
parseDateFromValue={this.parseDateFromValue}
|
|
/>
|
|
);
|
|
},
|
|
});
|
|
return PickerWrapper;
|
|
}
|