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

93 lines
2.4 KiB
TypeScript
Raw Normal View History

import React from 'react';
2016-07-07 20:25:03 +08:00
import { PropTypes } from 'react';
import TimePickerPanel from 'rc-time-picker/lib/Panel';
2016-03-30 10:52:15 +08:00
import classNames from 'classnames';
import defaultLocale from './locale/zh_CN';
2016-06-22 13:18:43 +08:00
import assign from 'object-assign';
2016-07-25 11:28:19 +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',
2016-06-22 13:18:43 +08:00
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;
const context = this.context;
let locale = defaultLocale;
2016-03-30 10:52:15 +08:00
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
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 = {
format: timeFormat || 'HH:mm:ss',
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 ? (
<TimePickerPanel
2016-04-14 16:48:34 +08:00
{...rcTimePickerProps}
{...props.showTime}
prefixCls="ant-calendar-time-picker"
2016-04-07 17:55:06 +08:00
placeholder={locale.timePickerLocale.placeholder}
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}
/>
);
2016-06-22 13:18:43 +08:00
},
});
return PickerWrapper;
2016-03-30 10:52:15 +08:00
}