ant-design/components/date-picker/wrapPicker.tsx

115 lines
3.2 KiB
TypeScript
Raw Normal View History

2016-06-22 13:18:43 +08:00
import React, {PropTypes} from 'react';
2016-03-30 10:52:15 +08:00
import TimePicker from 'rc-time-picker';
import DateTimeFormat from 'gregorian-calendar-format';
import GregorianCalendar from 'gregorian-calendar';
import classNames from 'classnames';
import defaultLocale from './locale/zh_CN';
2016-06-22 13:18:43 +08:00
import assign from 'object-assign';
2016-03-30 10:52:15 +08:00
export default function wrapPicker(Picker, defaultFormat) {
2016-06-22 13:18:43 +08:00
const PickerWrapper = React.createClass({
getDefaultProps() {
return {
format: defaultFormat || 'yyyy-MM-dd',
transitionName: 'slide-up',
popupStyle: {},
onChange() {
},
onOk() {
},
toggleOpen() {
},
locale: {},
align: {
offset: [0, -9],
},
};
},
2016-03-30 10:52:15 +08:00
2016-06-22 13:18:43 +08:00
contextTypes: {
2016-03-30 10:52:15 +08:00
antLocale: PropTypes.object,
2016-06-22 13:18:43 +08:00
},
2016-03-30 10:52:15 +08:00
getLocale() {
const props = this.props;
let locale = defaultLocale;
const context = this.context;
if (context.antLocale && context.antLocale.DatePicker) {
locale = context.antLocale.DatePicker;
}
// 统一合并为完整的 Locale
2016-06-22 13:18:43 +08:00
const result = assign({}, locale, props.locale);
result.lang = assign({}, locale.lang, props.locale.lang);
2016-03-30 10:52:15 +08:00
return result;
2016-06-22 13:18:43 +08:00
},
2016-03-30 10:52:15 +08:00
2016-06-22 13:18:43 +08:00
getFormatter() {
2016-03-30 10:52:15 +08:00
const format = this.props.format;
const formatter = new DateTimeFormat(format, this.getLocale().lang.format);
return formatter;
2016-06-22 13:18:43 +08:00
},
2016-03-30 10:52:15 +08:00
2016-06-22 13:18:43 +08:00
parseDateFromValue(value) {
2016-03-30 10:52:15 +08:00
if (value) {
if (typeof value === 'string') {
2016-06-22 13:18:43 +08:00
return this.getFormatter().parse(value, {locale: this.getLocale()});
2016-03-30 10:52:15 +08:00
} else if (value instanceof Date) {
let date = new GregorianCalendar(this.getLocale());
date.setTime(+value);
return date;
}
}
return value;
2016-06-22 13:18:43 +08:00
},
2016-03-30 10:52:15 +08:00
2016-06-22 13:18:43 +08:00
toggleOpen ({open}) {
this.props.toggleOpen({open});
},
2016-03-30 10:52:15 +08:00
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();
2016-04-14 12:32:25 +08:00
const timeFormat = props.showTime && props.showTime.format;
const rcTimePickerProps = {
2016-05-11 14:59:32 +08:00
formatter: new DateTimeFormat(timeFormat || 'HH:mm:ss', locale.timePickerLocale.format),
showSecond: timeFormat && timeFormat.indexOf('ss') >= 0,
2016-05-11 09:32:33 +08:00
showHour: timeFormat && timeFormat.indexOf('HH') >= 0,
};
2016-03-30 10:52:15 +08:00
const timePicker = props.showTime ? (
<TimePicker
2016-04-14 16:48:34 +08:00
{...rcTimePickerProps}
{...props.showTime}
2016-03-30 10:52:15 +08:00
prefixCls="ant-time-picker"
2016-04-07 17:55:06 +08:00
placeholder={locale.timePickerLocale.placeholder}
locale={locale.timePickerLocale}
2016-04-14 12:32:25 +08:00
transitionName="slide-up"
/>
2016-03-30 10:52:15 +08:00
) : null;
return (
<Picker
{...props}
2016-03-30 10:52:15 +08:00
pickerClass={pickerClass}
pickerInputClass={pickerInputClass}
locale={locale}
timePicker={timePicker}
toggleOpen={this.toggleOpen}
getFormatter={this.getFormatter}
parseDateFromValue={this.parseDateFromValue}
/>
);
2016-06-22 13:18:43 +08:00
},
});
return PickerWrapper;
2016-03-30 10:52:15 +08:00
}