-
Notifications
You must be signed in to change notification settings - Fork 1
/
Calendar.js
315 lines (233 loc) · 10.7 KB
/
Calendar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(['exports', 'react', 'melon-core/InputComponent', 'melon-core/classname/cxBuilder', 'melon-core/Validity', 'melon-core/util/syncPropsToState', 'melon/Icon', 'melon/Confirm', './calendar/Panel', './util', 'lodash/omit', './babelHelpers'], factory);
} else if (typeof exports !== "undefined") {
factory(exports, require('react'), require('melon-core/InputComponent'), require('melon-core/classname/cxBuilder'), require('melon-core/Validity'), require('melon-core/util/syncPropsToState'), require('melon/Icon'), require('melon/Confirm'), require('./calendar/Panel'), require('./util'), require('lodash/omit'), require('./babelHelpers'));
} else {
var mod = {
exports: {}
};
factory(mod.exports, global.react, global.InputComponent, global.cxBuilder, global.Validity, global.syncPropsToState, global.Icon, global.Confirm, global.Panel, global.util, global.omit, global.babelHelpers);
global.Calendar = mod.exports;
}
})(this, function (exports, _react, _InputComponent2, _cxBuilder, _Validity, _syncPropsToState, _Icon, _Confirm, _Panel, _util, _omit, babelHelpers) {
'use strict';
exports.__esModule = true;
var _react2 = babelHelpers.interopRequireDefault(_react);
var _InputComponent3 = babelHelpers.interopRequireDefault(_InputComponent2);
var _Validity2 = babelHelpers.interopRequireDefault(_Validity);
var _Icon2 = babelHelpers.interopRequireDefault(_Icon);
var _Confirm2 = babelHelpers.interopRequireDefault(_Confirm);
var _Panel2 = babelHelpers.interopRequireDefault(_Panel);
var DateTime = babelHelpers.interopRequireWildcard(_util);
var _omit2 = babelHelpers.interopRequireDefault(_omit);
/**
* @file melon/Calendar
* @author cxtom<cxtom2008@gmail.com>
*/
var cx = (0, _cxBuilder.create)('Calendar');
/**
* melon 日期选择器
*
* @class
* @extends {melon-core/InputComponent}
*/
var Calendar = function (_InputComponent) {
babelHelpers.inherits(Calendar, _InputComponent);
/**
* 构造函数
*
* @param {Object} props 组件属性
* @param {Object} context 组件上下文
* @public
*/
function Calendar(props, context) {
babelHelpers.classCallCheck(this, Calendar);
var _this = babelHelpers.possibleConstructorReturn(this, _InputComponent.call(this, props, context));
var value = _this.state.value;
_this.onLabelClick = _this.onLabelClick.bind(_this);
_this.onConfirm = _this.onConfirm.bind(_this);
_this.onLabelClick = _this.onLabelClick.bind(_this);
_this.onCancel = _this.onCancel.bind(_this);
_this.onDateChange = _this.onDateChange.bind(_this);
/**
* 组件状态
*
* @type {Object}
*/
_this.state = babelHelpers['extends']({}, _this.state, {
// 缓存用户在 confirm 前的选中值
date: value ? _this.parseDate(value) : undefined,
// 是否打开选择窗
open: false
});
return _this;
}
/**
* 组件每次更新 (componentWillRecieveProps) 时,需要
* 更新组件状态,包括校验信息、同步 date 和 value
*
* @param {Object} nextProps 组件更新的属性
* @return {Object} 最新的组件状态
* @public
*/
Calendar.prototype.getSyncUpdates = function getSyncUpdates(nextProps) {
var disabled = nextProps.disabled,
readOnly = nextProps.readOnly,
customValidity = nextProps.customValidity,
defaultValue = nextProps.defaultValue;
var value = nextProps.value ? nextProps.value : defaultValue;
// 如果有值,那么就试着解析一下;否则设置为 null
var date = value ? this.parseDate(value) : null;
// 如果 date 为 null 或者是 invalid date,那么就用上默认值;
// 否则就用解析出来的这个值
date = !date || isNaN(date.getTime()) ? new Date() : date;
var vilidity = (0, _syncPropsToState.getNextValidity)(this, { value: value, disabled: disabled, customValidity: customValidity });
return {
date: date,
vilidity: vilidity,
value: disabled || readOnly || !value ? value : this.stringifyValue(date)
};
};
Calendar.prototype.stringifyValue = function stringifyValue(rawValue) {
if (!DateTime.isDate(rawValue)) {
return rawValue;
}
var dateFormat = this.props.dateFormat;
return DateTime.format(rawValue, dateFormat);
};
Calendar.prototype.parseDate = function parseDate(date) {
if (typeof date !== 'string') {
return date;
}
var format = this.props.dateFormat;
return DateTime.parse(date, format);
};
Calendar.prototype.onLabelClick = function onLabelClick() {
var _props = this.props,
disabled = _props.disabled,
readOnly = _props.readOnly;
if (disabled || readOnly) {
return;
}
this.setState({ open: true });
};
Calendar.prototype.onConfirm = function onConfirm() {
var _this2 = this;
var _state = this.state,
value = _state.value,
date = _state.date;
value = this.parseDate(value);
if (value && DateTime.isEqualDate(date, this.parseDate(value))) {
this.setState({ open: false });
return;
}
var newDate = date ? date : new Date();
this.setState({ open: false, date: newDate }, function () {
_InputComponent.prototype.onChange.call(_this2, {
type: 'change',
target: _this2,
value: _this2.stringifyValue(newDate)
});
});
};
Calendar.prototype.onCancel = function onCancel() {
this.setState({ open: false });
};
Calendar.prototype.onDateChange = function onDateChange(e) {
var _this3 = this;
var value = e.value;
this.setState({ date: this.parseDate(value) }, this.props.autoConfirm ? function () {
return _this3.onConfirm();
} : null);
};
Calendar.prototype.renderHiddenInput = function renderHiddenInput() {
var _props2 = this.props,
name = _props2.name,
value = _props2.value;
return name ? _react2['default'].createElement('input', {
name: name,
type: 'hidden',
value: value }) : null;
};
Calendar.prototype.render = function render() {
var state = this.state,
props = this.props;
var lang = props.lang,
disabled = props.disabled,
readOnly = props.readOnly,
size = props.size,
placeholder = props.placeholder,
others = babelHelpers.objectWithoutProperties(props, ['lang', 'disabled', 'readOnly', 'size', 'placeholder']);
var value = state.value,
validity = state.validity;
var begin = props.begin,
end = props.end;
begin = begin ? this.parseDate(begin) : null;
end = end ? this.parseDate(end) : null;
var open = state.open,
date = state.date;
var className = cx(props).addStates({ focus: open }).addStates(this.getStyleStates()).build();
return _react2['default'].createElement(
'div',
babelHelpers['extends']({}, (0, _omit2['default'])(others, ['dateFormat', 'name', 'autoConfirm', 'variants']), { className: className }),
this.renderHiddenInput(),
_react2['default'].createElement(
'label',
{ onClick: disabled || readOnly ? null : this.onLabelClick },
value ? value : _react2['default'].createElement(
'span',
{ className: cx().part('label-placeholder').build() },
placeholder
),
_react2['default'].createElement(_Icon2['default'], { icon: 'expand-more' })
),
_react2['default'].createElement(_Validity2['default'], { validity: validity }),
_react2['default'].createElement(
_Confirm2['default'],
{
open: open,
variants: ['calendar'],
onConfirm: this.onConfirm,
onCancel: this.onCancel,
size: size,
buttonVariants: ['secondery', 'calendar'] },
_react2['default'].createElement(_Panel2['default'], {
date: date,
begin: begin,
end: end,
lang: lang,
onChange: this.onDateChange })
)
);
};
return Calendar;
}(_InputComponent3['default']);
exports['default'] = Calendar;
Calendar.displayName = 'Calendar';
Calendar.LANG = {
// 对于 '周' 的称呼
week: '周',
// 星期对应的顺序表示
days: '日,一,二,三,四,五,六'
};
Calendar.defaultProps = babelHelpers['extends']({}, _InputComponent3['default'].defaultProps, {
dateFormat: 'YYYY-MM-DD',
lang: Calendar.LANG,
placeholder: '请选择'
});
Calendar.propTypes = babelHelpers['extends']({}, _InputComponent3['default'].propTypes, {
value: _react.PropTypes.string,
autoConfirm: _react.PropTypes.bool,
dateFormat: _react.PropTypes.string,
end: _react.PropTypes.oneOfType([_react.PropTypes.object, _react.PropTypes.string]),
begin: _react.PropTypes.oneOfType([_react.PropTypes.object, _react.PropTypes.string]),
lang: _react.PropTypes.shape({
week: _react.PropTypes.string,
days: _react.PropTypes.string
})
});
Calendar.childContextTypes = _InputComponent3['default'].childContextTypes;
Calendar.contextTypes = _InputComponent3['default'].contextTypes;
});
//# sourceMappingURL=Calendar.js.map