UNPKG

4.73 kBJavaScriptView Raw
1import _extends from 'babel-runtime/helpers/extends';
2import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
3import _createClass from 'babel-runtime/helpers/createClass';
4import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
5import _inherits from 'babel-runtime/helpers/inherits';
6/* tslint:disable:jsx-no-multiline-js */
7import PropTypes from 'prop-types';
8import React from 'react';
9import RCDatePicker from 'rmc-date-picker/es/DatePicker';
10import PopupDatePicker from 'rmc-date-picker/es/Popup';
11import { getComponentLocale } from '../_util/getLocale';
12import { formatFn } from './utils';
13
14var DatePicker = function (_React$Component) {
15 _inherits(DatePicker, _React$Component);
16
17 function DatePicker() {
18 _classCallCheck(this, DatePicker);
19
20 var _this = _possibleConstructorReturn(this, (DatePicker.__proto__ || Object.getPrototypeOf(DatePicker)).apply(this, arguments));
21
22 _this.setScrollValue = function (v) {
23 _this.scrollValue = v;
24 };
25 _this.onOk = function (v) {
26 if (_this.scrollValue !== undefined) {
27 v = _this.scrollValue;
28 }
29 if (_this.props.onChange) {
30 _this.props.onChange(v);
31 }
32 if (_this.props.onOk) {
33 _this.props.onOk(v);
34 }
35 };
36 _this.onVisibleChange = function (visible) {
37 _this.scrollValue = undefined;
38 if (_this.props.onVisibleChange) {
39 _this.props.onVisibleChange(visible);
40 }
41 };
42 _this.fixOnOk = function (picker) {
43 if (picker) {
44 picker.onOk = _this.onOk;
45 }
46 };
47 return _this;
48 }
49
50 _createClass(DatePicker, [{
51 key: 'render',
52 value: function render() {
53 // tslint:disable-next-line:no-this-assignment
54 var props = this.props,
55 context = this.context;
56 var children = props.children,
57 value = props.value,
58 popupPrefixCls = props.popupPrefixCls;
59
60 var locale = getComponentLocale(props, context, 'DatePicker', function () {
61 return require('./locale/zh_CN');
62 });
63 var okText = locale.okText,
64 dismissText = locale.dismissText,
65 extra = locale.extra,
66 DatePickerLocale = locale.DatePickerLocale;
67 /**
68 * 注意:
69 * 受控 表示 通过设置 value 属性、组件的最终状态跟 value 设置值一致。
70 * 默认不设置 value 或 只设置 defaultValue 表示非受控。
71 *
72 * DatePickerView 对外通过 value “只支持 受控” 模式(可以使用 defaultDate 支持 非受控 模式,但不对外)
73 * PickerView 对外通过 value “只支持 受控” 模式
74 *
75 * DatePicker / Picker 对外只有 value 属性 (没有 defaultValue),
76 * 其中 List 展示部分 “只支持 受控” 模式,
77 * 弹出的 选择器部分 会随外部 value 改变而变、同时能自由滚动
78 * (即不会因为传入的 value 不变而不能滚动 (不像原生 input 的受控行为))
79 *
80 */
81
82 var dataPicker = React.createElement(RCDatePicker, { minuteStep: props.minuteStep, locale: DatePickerLocale, minDate: props.minDate, maxDate: props.maxDate, mode: props.mode, pickerPrefixCls: props.pickerPrefixCls, prefixCls: props.prefixCls, defaultDate: value || new Date(), use12Hours: props.use12Hours, onValueChange: props.onValueChange, onScrollChange: this.setScrollValue });
83 return React.createElement(
84 PopupDatePicker,
85 _extends({ datePicker: dataPicker, WrapComponent: 'div', transitionName: 'am-slide-up', maskTransitionName: 'am-fade' }, props, { prefixCls: popupPrefixCls, date: value || new Date(), dismissText: this.props.dismissText || dismissText, okText: this.props.okText || okText, ref: this.fixOnOk, onVisibleChange: this.onVisibleChange }),
86 children && React.isValidElement(children) && React.cloneElement(children, {
87 extra: value ? formatFn(this, value) : this.props.extra || extra
88 })
89 );
90 }
91 }]);
92
93 return DatePicker;
94}(React.Component);
95
96export default DatePicker;
97
98DatePicker.defaultProps = {
99 mode: 'datetime',
100 prefixCls: 'am-picker',
101 pickerPrefixCls: 'am-picker-col',
102 popupPrefixCls: 'am-picker-popup',
103 minuteStep: 1,
104 use12Hours: false
105};
106DatePicker.contextTypes = {
107 antLocale: PropTypes.object
108};
\No newline at end of file