ant-design/components/date-picker/index.jsx

147 lines
4.4 KiB
React
Raw Normal View History

2015-06-19 12:29:22 +08:00
import React from 'react';
2015-10-20 16:47:55 +08:00
import Calendar from 'rc-calendar';
import MonthCalendar from 'rc-calendar/lib/MonthCalendar';
import DatePicker from 'rc-calendar/lib/Picker';
2015-06-19 12:29:22 +08:00
import GregorianCalendar from 'gregorian-calendar';
2015-11-03 13:50:36 +08:00
import CalendarLocale from 'rc-calendar/lib/locale/zh_CN';
2015-12-11 11:11:24 +08:00
import AntRangePicker from './RangePicker';
import PickerMixin from './PickerMixin';
import TimePicker from 'rc-time-picker';
import classNames from 'classnames';
2015-10-23 11:52:05 +08:00
2015-11-30 14:41:01 +08:00
function createPicker(TheCalendar, defaultFormat) {
2015-08-25 17:27:38 +08:00
return React.createClass({
2015-10-23 11:52:05 +08:00
getDefaultProps() {
return {
2015-11-30 14:41:01 +08:00
format: defaultFormat || 'yyyy-MM-dd',
2015-10-23 11:52:05 +08:00
transitionName: 'slide-up',
2015-11-11 12:21:00 +08:00
popupStyle: {},
2015-11-03 13:50:36 +08:00
onChange() {
}, // onChange 可用于 Validator
2015-10-28 19:48:32 +08:00
locale: {},
2015-10-31 15:15:55 +08:00
align: {
offset: [0, -9],
},
open: false
2015-10-23 11:52:05 +08:00
};
},
2015-08-25 17:27:38 +08:00
getInitialState() {
return {
2015-11-03 13:50:36 +08:00
value: this.parseDateFromValue(this.props.value || this.props.defaultValue)
2015-08-25 17:27:38 +08:00
};
},
2015-12-11 11:11:24 +08:00
mixins: [ PickerMixin ],
2015-08-25 17:27:38 +08:00
componentWillReceiveProps(nextProps) {
2015-09-01 10:59:12 +08:00
if ('value' in nextProps) {
2015-08-25 17:27:38 +08:00
this.setState({
2015-10-23 11:52:05 +08:00
value: this.parseDateFromValue(nextProps.value)
2015-08-25 17:27:38 +08:00
});
}
},
2015-10-23 15:55:48 +08:00
handleChange(value) {
if (!('value' in this.props)) {
this.setState({ value });
}
2015-11-03 13:50:36 +08:00
const timeValue = value ? new Date(value.getTime()) : null;
this.props.onChange(timeValue);
2015-08-25 17:27:38 +08:00
},
render() {
const locale = this.getLocale();
// 以下两行代码
// 给没有初始值的日期选择框提供本地化信息
// 否则会以周日开始排
let defaultCalendarValue = new GregorianCalendar(locale);
defaultCalendarValue.setTime(Date.now());
2015-11-19 17:20:00 +08:00
2015-11-19 19:28:36 +08:00
const placeholder = ('placeholder' in this.props)
? this.props.placeholder : locale.lang.placeholder;
const timePicker = this.props.showTime
? <TimePicker prefixCls="ant-time-picker"
placeholder={locale.lang.timePlaceholder}
transitionName="slide-up" />
: null;
const calendarClassName = classNames({
['ant-calendar-time']: this.props.showTime,
});
2015-10-23 10:51:56 +08:00
const calendar = (
2015-09-01 16:18:46 +08:00
<TheCalendar
2015-08-25 17:27:38 +08:00
disabledDate={this.props.disabledDate}
locale={locale.lang}
timePicker={timePicker}
2015-10-23 11:52:05 +08:00
defaultValue={defaultCalendarValue}
2015-11-19 17:20:00 +08:00
dateInputPlaceholder={placeholder}
2015-08-25 17:27:38 +08:00
prefixCls="ant-calendar"
className={calendarClassName}
2015-08-25 17:27:38 +08:00
showOk={this.props.showTime}
2015-11-25 17:47:55 +08:00
showClear />
2015-08-25 17:27:38 +08:00
);
2015-09-01 16:18:46 +08:00
let sizeClass = '';
2015-08-25 17:27:38 +08:00
if (this.props.size === 'large') {
sizeClass = ' ant-input-lg';
} else if (this.props.size === 'small') {
sizeClass = ' ant-input-sm';
2015-08-04 18:42:41 +08:00
}
2015-11-03 13:50:36 +08:00
let pickerClass = 'ant-calendar-picker';
if (this.state.open) {
pickerClass += ' ant-calendar-picker-open';
}
return <span className={pickerClass}>
<DatePicker
2015-08-25 17:27:38 +08:00
transitionName={this.props.transitionName}
disabled={this.props.disabled}
calendar={calendar}
value={this.state.value}
2015-10-31 15:15:55 +08:00
prefixCls="ant-calendar-picker-container"
2015-11-11 12:21:00 +08:00
style={this.props.popupStyle}
2015-10-31 15:15:55 +08:00
align={this.props.align}
onOpen={this.toggleOpen}
onClose={this.toggleOpen}
2015-08-25 17:27:38 +08:00
onChange={this.handleChange}>
2015-10-20 16:47:55 +08:00
{
2015-11-10 15:39:59 +08:00
({value}) => {
return (
<span>
2015-11-10 14:20:29 +08:00
<input disabled={this.props.disabled}
onChange={this.handleInputChange}
value={value && this.getFormatter().format(value)}
2015-11-19 17:20:00 +08:00
placeholder={placeholder}
2015-11-20 21:05:46 +08:00
style={this.props.style}
2015-11-10 14:20:29 +08:00
className={'ant-calendar-picker-input ant-input' + sizeClass}/>
<span className="ant-calendar-picker-icon"/>
</span>
);
2015-10-20 16:47:55 +08:00
}
2015-11-10 15:39:59 +08:00
}
</DatePicker>
2015-10-31 15:15:55 +08:00
</span>;
2015-08-25 17:27:38 +08:00
}
});
}
const AntDatePicker = createPicker(Calendar);
2015-11-30 14:41:01 +08:00
const AntMonthPicker = createPicker(MonthCalendar, 'yyyy-MM');
2015-08-24 14:30:08 +08:00
const AntCalendar = React.createClass({
getDefaultProps() {
return {
locale: CalendarLocale,
prefixCls: 'ant-calendar',
};
},
render() {
2015-10-23 10:51:56 +08:00
return <Calendar {...this.props} />;
2015-08-24 14:30:08 +08:00
}
});
2015-08-25 17:27:38 +08:00
AntDatePicker.Calendar = AntCalendar;
2015-12-11 11:11:24 +08:00
AntDatePicker.RangePicker = AntRangePicker;
2015-08-25 17:27:38 +08:00
AntDatePicker.MonthPicker = AntMonthPicker;
2015-08-24 14:30:08 +08:00
2015-08-25 17:27:38 +08:00
export default AntDatePicker;