UNPKG

10.3 kBJavaScriptView Raw
1import _extends from 'babel-runtime/helpers/extends';
2import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
3import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
4import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
5import _inherits from 'babel-runtime/helpers/inherits';
6
7var _class, _temp;
8
9import React from 'react';
10import { polyfill } from 'react-lifecycles-compat';
11import * as PT from 'prop-types';
12import classnames from 'classnames';
13import SharedPT from '../prop-types';
14import { DATE_INPUT_TYPE, DATE_PICKER_MODE } from '../constant';
15import { func, datejs, obj } from '../../util';
16import { fmtValue } from '../util';
17
18import Input from '../../input';
19import Icon from '../../icon';
20
21var DATE = DATE_PICKER_MODE.DATE,
22 WEEK = DATE_PICKER_MODE.WEEK,
23 MONTH = DATE_PICKER_MODE.MONTH,
24 QUARTER = DATE_PICKER_MODE.QUARTER,
25 YEAR = DATE_PICKER_MODE.YEAR;
26var DateInput = (_temp = _class = function (_React$Component) {
27 _inherits(DateInput, _React$Component);
28
29 function DateInput(props) {
30 _classCallCheck(this, DateInput);
31
32 var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));
33
34 _this.setInputRef = function (el, index) {
35 if (_this.props.isRange) {
36 if (!_this.input) {
37 _this.input = [];
38 }
39 _this.input[index] = el;
40 } else {
41 _this.input = el;
42 }
43 };
44
45 _this.setValue = function (v) {
46 var _this$props = _this.props,
47 isRange = _this$props.isRange,
48 inputType = _this$props.inputType,
49 value = _this$props.value;
50
51 var newVal = v;
52
53 if (isRange) {
54 newVal = [].concat(value);
55 newVal[inputType] = v;
56 }
57
58 return newVal;
59 };
60
61 _this.formatter = function (v) {
62 var format = _this.props.format;
63
64 return typeof format === 'function' ? format(v) : v.format(format);
65 };
66
67 _this.onInput = function (v, e, eventType) {
68 v = _this.setValue(v);
69
70 if (eventType === 'clear') {
71 v = null;
72 e.stopPropagation();
73 }
74
75 func.invoke(_this.props, 'onInput', [v, eventType]);
76 };
77
78 _this.handleTypeChange = function (inputType) {
79 if (inputType !== _this.props.inputType) {
80 func.invoke(_this.props, 'onInputTypeChange', [inputType]);
81 }
82 };
83
84 _this.getPlaceholder = function () {
85 var _mode2placeholder;
86
87 var _this$props2 = _this.props,
88 locale = _this$props2.locale,
89 isRange = _this$props2.isRange,
90 mode = _this$props2.mode;
91 var placeholder = locale.placeholder,
92 selectDate = locale.selectDate,
93 monthPlaceholder = locale.monthPlaceholder,
94 weekPlaceholder = locale.weekPlaceholder,
95 yearPlaceholder = locale.yearPlaceholder,
96 startPlaceholder = locale.startPlaceholder,
97 quarterPlaceholder = locale.quarterPlaceholder,
98 endPlaceholder = locale.endPlaceholder;
99
100 var mode2placeholder = (_mode2placeholder = {}, _mode2placeholder[DATE] = selectDate, _mode2placeholder[WEEK] = weekPlaceholder, _mode2placeholder[MONTH] = monthPlaceholder, _mode2placeholder[QUARTER] = quarterPlaceholder, _mode2placeholder[YEAR] = yearPlaceholder, _mode2placeholder);
101
102 var holder = _this.props.placeholder;
103
104 holder = holder || (isRange ? [startPlaceholder, endPlaceholder] : mode2placeholder[mode] || placeholder);
105
106 if (isRange && !Array.isArray(holder)) {
107 holder = Array(2).fill(holder);
108 }
109
110 return holder;
111 };
112
113 _this.getHtmlSize = function () {
114 var _this$props3 = _this.props,
115 isRange = _this$props3.isRange,
116 format = _this$props3.format,
117 hasBorder = _this$props3.hasBorder;
118
119 var value = '2020-12-12 12:12:12';
120 var size = 0;
121
122 if (isRange) {
123 var fmtStr = fmtValue([value, value].map(datejs), format);
124 size = Math.max.apply(Math, fmtStr.map(function (s) {
125 return s && s.length || 0;
126 }));
127 } else {
128 var _fmtStr = fmtValue(datejs(value), format);
129 fmtValue(datejs(value), format);
130 size = _fmtStr && _fmtStr.length || 0;
131 }
132
133 return String(Math.max(size, hasBorder ? 12 : 8));
134 };
135
136 _this.prefixCls = props.prefix + 'date-picker2-input';
137 return _this;
138 }
139
140 /**
141 * 根据 format 计算输入框 htmlSize
142 */
143
144
145 DateInput.prototype.render = function render() {
146 var _classnames2;
147
148 var onInput = this.onInput,
149 setInputRef = this.setInputRef,
150 handleTypeChange = this.handleTypeChange,
151 prefixCls = this.prefixCls;
152
153 var _props = this.props,
154 autoFocus = _props.autoFocus,
155 readOnly = _props.readOnly,
156 isRange = _props.isRange,
157 value = _props.value,
158 prefix = _props.prefix,
159 hasClear = _props.hasClear,
160 inputType = _props.inputType,
161 size = _props.size,
162 focus = _props.focus,
163 hasBorder = _props.hasBorder,
164 separator = _props.separator,
165 disabled = _props.disabled,
166 inputProps = _props.inputProps,
167 dateInputAriaLabel = _props.dateInputAriaLabel,
168 state = _props.state,
169 label = _props.label,
170 restProps = _objectWithoutProperties(_props, ['autoFocus', 'readOnly', 'isRange', 'value', 'prefix', 'hasClear', 'inputType', 'size', 'focus', 'hasBorder', 'separator', 'disabled', 'inputProps', 'dateInputAriaLabel', 'state', 'label']);
171
172 var placeholder = this.getPlaceholder();
173 var htmlSize = this.getHtmlSize();
174
175 var sharedProps = _extends({}, obj.pickProps(restProps, Input), inputProps, {
176 size: size,
177 htmlSize: htmlSize,
178 readOnly: readOnly,
179 hasBorder: false,
180 onChange: onInput
181 });
182
183 var rangeProps = void 0;
184 if (isRange) {
185 rangeProps = [DATE_INPUT_TYPE.BEGIN, DATE_INPUT_TYPE.END].map(function (idx) {
186 var _classnames;
187
188 var _disabled = Array.isArray(disabled) ? disabled[idx] : disabled;
189
190 return _extends({}, sharedProps, {
191 autoFocus: autoFocus,
192 placeholder: placeholder[idx],
193 value: value[idx] || '',
194 'aria-label': Array.isArray(dateInputAriaLabel) ? dateInputAriaLabel[idx] : dateInputAriaLabel,
195 disabled: _disabled,
196 ref: function ref(_ref) {
197 return setInputRef(_ref, idx);
198 },
199 onFocus: _disabled ? undefined : function () {
200 return handleTypeChange(idx);
201 },
202 className: classnames((_classnames = {}, _classnames[prefixCls + '-active'] = inputType === idx, _classnames))
203 });
204 });
205 }
206
207 var className = classnames([prefixCls, prefixCls + '-' + size, prefixCls + '-' + (isRange ? 'range' : 'date')], (_classnames2 = {}, _classnames2[prefixCls + '-focus'] = focus, _classnames2[prefixCls + '-noborder'] = !hasBorder, _classnames2[prefixCls + '-disabled'] = isRange && Array.isArray(disabled) ? disabled.every(function (v) {
208 return v;
209 }) : disabled, _classnames2));
210
211 var calendarIcon = React.createElement(Icon, { type: 'calendar', className: prefix + 'date-picker2-symbol-calendar-icon' });
212
213 return React.createElement(
214 'div',
215 { className: className },
216 isRange ? React.createElement(
217 React.Fragment,
218 null,
219 React.createElement(Input, _extends({}, rangeProps[0], {
220 label: label,
221 autoFocus: autoFocus // eslint-disable-line jsx-a11y/no-autofocus
222 })),
223 React.createElement(
224 'div',
225 { className: prefixCls + '-separator' },
226 separator
227 ),
228 React.createElement(Input, _extends({}, rangeProps[1], {
229 state: state,
230 hasClear: !state && hasClear,
231 hint: state ? null : calendarIcon
232 }))
233 ) : React.createElement(Input, _extends({}, sharedProps, {
234 label: label,
235 state: state,
236 disabled: disabled,
237 hasClear: !state && hasClear,
238 placeholder: placeholder,
239 autoFocus: autoFocus // eslint-disable-line jsx-a11y/no-autofocus
240 , ref: setInputRef,
241 'aria-label': dateInputAriaLabel,
242 value: value || '',
243 hint: state ? null : calendarIcon
244 }))
245 );
246 };
247
248 return DateInput;
249}(React.Component), _class.propTypes = {
250 prefix: PT.string,
251 rtl: PT.bool,
252 locale: PT.object,
253 value: SharedPT.inputValue,
254 inputType: SharedPT.inputType,
255 format: SharedPT.format,
256 isRange: PT.bool,
257 hasClear: PT.bool,
258 onInputTypeChange: PT.func,
259 autoFocus: PT.bool,
260 readOnly: SharedPT.readOnly,
261 placeholder: SharedPT.placeholder,
262 size: SharedPT.size,
263 focus: PT.bool,
264 hasBorder: PT.bool,
265 separator: PT.node,
266 disabled: SharedPT.disabled,
267 inputProps: PT.object,
268 dateInputAriaLabel: SharedPT.ariaLabel,
269 label: PT.node
270}, _class.defaultProps = {
271 autoFocus: false,
272 readOnly: false,
273 hasClear: true,
274 separator: '-',
275 hasBorder: true,
276 size: 'medium'
277}, _temp);
278DateInput.displayName = 'DateInput';
279
280
281export default polyfill(DateInput);
\No newline at end of file