@vxe-ui/plugin-render-antd 3.3.0 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,556 @@
1
+ import XEUtils from 'xe-utils';
2
+ /**
3
+ * 表格 - 渲染器
4
+ */
5
+ export function defineTableRender(VxeUI) {
6
+ function isEmptyValue(cellValue) {
7
+ return cellValue === null || cellValue === undefined || cellValue === '';
8
+ }
9
+ function getOnName(type) {
10
+ return XEUtils.kebabCase(type);
11
+ }
12
+ function getModelProp(renderOpts) {
13
+ let prop = 'value';
14
+ switch (renderOpts.name) {
15
+ case 'ASwitch':
16
+ prop = 'checked';
17
+ break;
18
+ }
19
+ return prop;
20
+ }
21
+ function getModelEvent(renderOpts) {
22
+ let type = 'change';
23
+ switch (renderOpts.name) {
24
+ case 'AInput':
25
+ type = 'change.value';
26
+ break;
27
+ case 'ARadio':
28
+ case 'ACheckbox':
29
+ type = 'input';
30
+ break;
31
+ }
32
+ return type;
33
+ }
34
+ function dateFormatToVxeFormat(format) {
35
+ if (format) {
36
+ return `${format}`.replace('YYYY', 'yyyy').replace('DD', 'dd');
37
+ }
38
+ return format;
39
+ }
40
+ function getChangeEvent(renderOpts) {
41
+ return 'change';
42
+ }
43
+ function getCellEditFilterProps(renderOpts, params, value, defaultProps) {
44
+ return XEUtils.assign({}, defaultProps, renderOpts.props, { [getModelProp(renderOpts)]: value });
45
+ }
46
+ function formatText(cellValue) {
47
+ return '' + (isEmptyValue(cellValue) ? '' : cellValue);
48
+ }
49
+ function getCellLabelVNs(h, renderOpts, params, cellLabel) {
50
+ const { placeholder } = renderOpts;
51
+ return [
52
+ h('span', {
53
+ class: 'vxe-cell--label'
54
+ }, placeholder && isEmptyValue(cellLabel)
55
+ ? [
56
+ h('span', {
57
+ class: 'vxe-cell--placeholder'
58
+ }, formatText(placeholder))
59
+ ]
60
+ : formatText(cellLabel))
61
+ ];
62
+ }
63
+ function getOns(renderOpts, params, inputFunc, changeFunc) {
64
+ const { events } = renderOpts;
65
+ const modelEvent = getModelEvent(renderOpts);
66
+ const changeEvent = getChangeEvent(renderOpts);
67
+ const isSameEvent = changeEvent === modelEvent;
68
+ const ons = {};
69
+ XEUtils.objectEach(events, (func, key) => {
70
+ ons[getOnName(key)] = function (...args) {
71
+ func(params, ...args);
72
+ };
73
+ });
74
+ if (inputFunc) {
75
+ ons[getOnName(modelEvent)] = function (targetEvnt) {
76
+ inputFunc(targetEvnt);
77
+ if (events && events[modelEvent]) {
78
+ events[modelEvent](params, targetEvnt);
79
+ }
80
+ if (isSameEvent && changeFunc) {
81
+ changeFunc(targetEvnt);
82
+ }
83
+ };
84
+ }
85
+ if (!isSameEvent && changeFunc) {
86
+ ons[getOnName(changeEvent)] = function (...args) {
87
+ changeFunc(...args);
88
+ if (events && events[changeEvent]) {
89
+ events[changeEvent](params, ...args);
90
+ }
91
+ };
92
+ }
93
+ return ons;
94
+ }
95
+ function getEditOns(renderOpts, params) {
96
+ const { $table, row, column } = params;
97
+ return getOns(renderOpts, params, (value) => {
98
+ // 处理 model 值双向绑定
99
+ XEUtils.set(row, column.field, value);
100
+ }, () => {
101
+ // 处理 change 事件相关逻辑
102
+ $table.updateStatus(params);
103
+ });
104
+ }
105
+ function getFilterOns(renderOpts, params, option, changeFunc) {
106
+ return getOns(renderOpts, params, (value) => {
107
+ // 处理 model 值双向绑定
108
+ option.data = value;
109
+ }, changeFunc);
110
+ }
111
+ function matchCascaderData(index, list, values, labels) {
112
+ const val = values[index];
113
+ if (list && values.length > index) {
114
+ XEUtils.each(list, (item) => {
115
+ if (item.value === val) {
116
+ labels.push(item.label);
117
+ matchCascaderData(++index, item.children, values, labels);
118
+ }
119
+ });
120
+ }
121
+ }
122
+ function formatDatePicker(defaultFormat) {
123
+ return function (h, renderOpts, params) {
124
+ return getCellLabelVNs(h, renderOpts, params, getDatePickerCellValue(renderOpts, params, defaultFormat));
125
+ };
126
+ }
127
+ function formatTimePicker(defaultFormat) {
128
+ return function (h, renderOpts, params) {
129
+ const { props = {} } = renderOpts;
130
+ const { row, column } = params;
131
+ let cellValue = XEUtils.get(row, column.field);
132
+ try {
133
+ if (cellValue) {
134
+ if (!XEUtils.isString(cellValue)) {
135
+ cellValue = cellValue.format ? cellValue.format(props.format || props.valueFormat || defaultFormat) : XEUtils.toDateString(cellValue, dateFormatToVxeFormat(props.format || props.valueFormat || defaultFormat));
136
+ }
137
+ }
138
+ }
139
+ catch (e) { }
140
+ return getCellLabelVNs(h, renderOpts, params, cellValue);
141
+ };
142
+ }
143
+ function handleSelectCellValue(cellValue, renderOpts) {
144
+ const { options = [], optionGroups, props = {}, optionProps = {}, optionGroupProps = {} } = renderOpts;
145
+ const labelProp = optionProps.label || 'label';
146
+ const valueProp = optionProps.value || 'value';
147
+ const groupOptions = optionGroupProps.options || 'options';
148
+ if (!isEmptyValue(cellValue)) {
149
+ return XEUtils.map(props.mode === 'multiple' ? cellValue : [cellValue], optionGroups
150
+ ? (value) => {
151
+ let selectItem;
152
+ for (let index = 0; index < optionGroups.length; index++) {
153
+ selectItem = XEUtils.find(optionGroups[index][groupOptions], (item) => item[valueProp] === value);
154
+ if (selectItem) {
155
+ break;
156
+ }
157
+ }
158
+ return selectItem ? selectItem[labelProp] : value;
159
+ }
160
+ : (value) => {
161
+ const selectItem = XEUtils.find(options, (item) => item[valueProp] === value);
162
+ return selectItem ? selectItem[labelProp] : value;
163
+ }).join(', ');
164
+ }
165
+ return '';
166
+ }
167
+ function getSelectCellValue(renderOpts, params) {
168
+ const { row, column } = params;
169
+ const cellValue = XEUtils.get(row, column.field);
170
+ return handleSelectCellValue(cellValue, renderOpts);
171
+ }
172
+ function getCascaderCellValue(renderOpts, params) {
173
+ const { props = {} } = renderOpts;
174
+ const { row, column } = params;
175
+ const cellValue = XEUtils.get(row, column.field);
176
+ const values = cellValue || [];
177
+ const labels = [];
178
+ matchCascaderData(0, props.options, values, labels);
179
+ return (props.showAllLevels === false ? labels.slice(labels.length - 1, labels.length) : labels).join(` ${props.separator || '/'} `);
180
+ }
181
+ function getRangePickerCellValue(renderOpts, params) {
182
+ const { props = {} } = renderOpts;
183
+ const { row, column } = params;
184
+ let cellValue = XEUtils.get(row, column.field);
185
+ if (cellValue) {
186
+ cellValue = XEUtils.map(cellValue, (date) => {
187
+ return date && date.format ? date.format(props.format || 'YYYY-MM-DD') : XEUtils.toDateString(date, dateFormatToVxeFormat(props.format || 'YYYY-MM-DD'));
188
+ }).join(' ~ ');
189
+ }
190
+ return cellValue;
191
+ }
192
+ function getTreeSelectCellValue(renderOpts, params) {
193
+ const { props = {} } = renderOpts;
194
+ const { treeData, treeCheckable } = props;
195
+ const { row, column } = params;
196
+ const cellValue = XEUtils.get(row, column.field);
197
+ if (!isEmptyValue(cellValue)) {
198
+ return XEUtils.map(treeCheckable ? cellValue : [cellValue], (value) => {
199
+ const matchObj = XEUtils.findTree(treeData, (item) => item.value === value, { children: 'children' });
200
+ return matchObj ? matchObj.item.title : value;
201
+ }).join(', ');
202
+ }
203
+ return cellValue;
204
+ }
205
+ function getDatePickerCellValue(renderOpts, params, defaultFormat) {
206
+ const { props = {} } = renderOpts;
207
+ const { row, column } = params;
208
+ let cellValue = XEUtils.get(row, column.field);
209
+ try {
210
+ if (cellValue) {
211
+ if (!defaultFormat) {
212
+ if (renderOpts.name === 'ADatePicker') {
213
+ switch (props.picker) {
214
+ case 'week':
215
+ defaultFormat = 'YYYY-WW周';
216
+ break;
217
+ case 'month':
218
+ defaultFormat = 'YYYY-MM';
219
+ break;
220
+ case 'year':
221
+ defaultFormat = 'YYYY';
222
+ break;
223
+ default:
224
+ defaultFormat = props.showTime ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD';
225
+ break;
226
+ }
227
+ }
228
+ }
229
+ cellValue = cellValue.format ? cellValue.format(props.format || defaultFormat) : XEUtils.toDateString(cellValue, dateFormatToVxeFormat(props.format || defaultFormat));
230
+ }
231
+ }
232
+ catch (e) { }
233
+ return cellValue;
234
+ }
235
+ function createEditRender(defaultProps) {
236
+ return function (h, renderOpts, params) {
237
+ const { row, column } = params;
238
+ const { name, attrs } = renderOpts;
239
+ const cellValue = XEUtils.get(row, column.field);
240
+ return [
241
+ h(name, {
242
+ attrs,
243
+ props: getCellEditFilterProps(renderOpts, params, cellValue, defaultProps),
244
+ on: getEditOns(renderOpts, params)
245
+ })
246
+ ];
247
+ };
248
+ }
249
+ function defaultButtonEditRender(h, renderOpts, params) {
250
+ const { attrs } = renderOpts;
251
+ return [
252
+ h('a-button', {
253
+ attrs,
254
+ props: getCellEditFilterProps(renderOpts, params, null),
255
+ on: getOns(renderOpts, params)
256
+ }, cellText(renderOpts.content))
257
+ ];
258
+ }
259
+ function defaultButtonsEditRender(h, renderOpts, params) {
260
+ const { children } = renderOpts;
261
+ if (children) {
262
+ return children.map((childRenderOpts) => defaultButtonEditRender(h, childRenderOpts, params)[0]);
263
+ }
264
+ return [];
265
+ }
266
+ function createFilterRender(defaultProps) {
267
+ return function (h, renderOpts, params) {
268
+ const { column } = params;
269
+ const { name, attrs } = renderOpts;
270
+ return [
271
+ h('div', {
272
+ class: 'vxe-table--filter-antd-wrapper'
273
+ }, column.filters.map((option, oIndex) => {
274
+ const optionValue = option.data;
275
+ return h(name, {
276
+ key: oIndex,
277
+ attrs,
278
+ props: Object.assign({}, getCellEditFilterProps(renderOpts, params, optionValue, defaultProps)),
279
+ on: getFilterOns(renderOpts, params, option, () => {
280
+ // 处理 change 事件相关逻辑
281
+ handleConfirmFilter(params, !!option.data, option);
282
+ })
283
+ });
284
+ }))
285
+ ];
286
+ };
287
+ }
288
+ function handleConfirmFilter(params, checked, option) {
289
+ const { $panel } = params;
290
+ $panel.changeOption(null, checked, option);
291
+ }
292
+ /**
293
+ * 模糊匹配
294
+ * @param params
295
+ */
296
+ function defaultFuzzyFilterMethod(params) {
297
+ const { option, row, column } = params;
298
+ const { data } = option;
299
+ const cellValue = XEUtils.get(row, column.field);
300
+ return XEUtils.toValueString(cellValue).indexOf(data) > -1;
301
+ }
302
+ /**
303
+ * 精确匹配
304
+ * @param params
305
+ */
306
+ function defaultExactFilterMethod(params) {
307
+ const { option, row, column } = params;
308
+ const { data } = option;
309
+ const cellValue = XEUtils.get(row, column.field);
310
+ /* eslint-disable eqeqeq */
311
+ return cellValue === data;
312
+ }
313
+ function renderOptions(h, options, optionProps) {
314
+ const labelProp = optionProps.label || 'label';
315
+ const valueProp = optionProps.value || 'value';
316
+ return XEUtils.map(options, (item, oIndex) => {
317
+ return h('a-select-option', {
318
+ key: oIndex,
319
+ props: {
320
+ value: item[valueProp],
321
+ disabled: item.disabled
322
+ }
323
+ }, item[labelProp]);
324
+ });
325
+ }
326
+ function cellText(cellValue) {
327
+ return [formatText(cellValue)];
328
+ }
329
+ function createDatePickerExportMethod(defaultFormat) {
330
+ return function (params) {
331
+ const { row, column, options } = params;
332
+ return options && options.original ? XEUtils.get(row, column.field) : getDatePickerCellValue(column.editRender || column.cellRender, params, defaultFormat);
333
+ };
334
+ }
335
+ function createExportMethod(getExportCellValue) {
336
+ return function (params) {
337
+ const { row, column, options } = params;
338
+ return options && options.original ? XEUtils.get(row, column.field) : getExportCellValue(column.editRender || column.cellRender, params);
339
+ };
340
+ }
341
+ VxeUI.renderer.mixin({
342
+ AAutoComplete: {
343
+ tableAutoFocus: 'input',
344
+ renderTableDefault: createEditRender(),
345
+ renderTableEdit: createEditRender(),
346
+ renderTableFilter: createFilterRender(),
347
+ tableFilterDefaultMethod: defaultFuzzyFilterMethod
348
+ },
349
+ AInput: {
350
+ tableAutoFocus: 'input',
351
+ renderTableDefault: createEditRender(),
352
+ renderTableEdit: createEditRender(),
353
+ renderTableFilter: createFilterRender(),
354
+ tableFilterDefaultMethod: defaultFuzzyFilterMethod
355
+ },
356
+ AInputNumber: {
357
+ tableAutoFocus: 'input',
358
+ renderTableDefault: createEditRender(),
359
+ renderTableEdit: createEditRender(),
360
+ renderTableFilter: createFilterRender(),
361
+ tableFilterDefaultMethod: defaultFuzzyFilterMethod
362
+ },
363
+ ASelect: {
364
+ renderTableEdit(h, renderOpts, params) {
365
+ const { options = [], optionGroups, optionProps = {}, optionGroupProps = {} } = renderOpts;
366
+ const { row, column } = params;
367
+ const { attrs } = renderOpts;
368
+ const cellValue = XEUtils.get(row, column.field);
369
+ const props = getCellEditFilterProps(renderOpts, params, cellValue);
370
+ const ons = getEditOns(renderOpts, params);
371
+ if (optionGroups) {
372
+ const groupOptions = optionGroupProps.options || 'options';
373
+ const groupLabel = optionGroupProps.label || 'label';
374
+ return [
375
+ h('a-select', {
376
+ attrs,
377
+ props: Object.assign(Object.assign({}, props), { options: undefined }),
378
+ on: ons
379
+ }, XEUtils.map(optionGroups, (group, gIndex) => {
380
+ return h('a-select-opt-group', {
381
+ key: gIndex
382
+ }, [
383
+ h('span', {
384
+ slot: 'label'
385
+ }, group[groupLabel])
386
+ ].concat(renderOptions(h, group[groupOptions], optionProps)));
387
+ }))
388
+ ];
389
+ }
390
+ return [
391
+ h('a-select', {
392
+ attrs,
393
+ props: Object.assign(Object.assign({}, props), { options: props.options || options }),
394
+ on: ons
395
+ })
396
+ ];
397
+ },
398
+ renderTableCell(h, renderOpts, params) {
399
+ const { options, optionGroups } = renderOpts;
400
+ const { $table, row, column } = params;
401
+ const opSize = options ? options.length : null;
402
+ const ogSize = optionGroups ? optionGroups.length : null;
403
+ if ($table.effectCellData) {
404
+ const { cellResult } = $table.effectCellData(row, column, {
405
+ key: 'render_table_cell',
406
+ isChanged({ oldValue, cellValue }) {
407
+ return oldValue && oldValue[0] === cellValue && oldValue[1] === opSize && oldValue[2] === ogSize;
408
+ },
409
+ setValue({ cellValue }) {
410
+ return [cellValue, opSize, ogSize];
411
+ },
412
+ getResult({ cellValue }) {
413
+ return handleSelectCellValue(cellValue, renderOpts);
414
+ }
415
+ });
416
+ return getCellLabelVNs(h, renderOpts, params, cellResult);
417
+ }
418
+ const cellResult = getSelectCellValue(renderOpts, params);
419
+ return getCellLabelVNs(h, renderOpts, params, cellResult);
420
+ },
421
+ renderTableFilter(h, renderOpts, params) {
422
+ const { options = [], optionGroups, optionGroupProps = {} } = renderOpts;
423
+ const groupOptions = optionGroupProps.options || 'options';
424
+ const { column } = params;
425
+ const { attrs } = renderOpts;
426
+ return [
427
+ h('div', {
428
+ class: 'vxe-table--filter-antd-wrapper'
429
+ }, optionGroups
430
+ ? column.filters.map((option, oIndex) => {
431
+ const optionValue = option.data;
432
+ const props = getCellEditFilterProps(renderOpts, params, optionValue);
433
+ return h('a-select', {
434
+ key: oIndex,
435
+ attrs,
436
+ props: Object.assign(Object.assign({}, props), { options: groupOptions }),
437
+ on: getFilterOns(renderOpts, params, option, () => {
438
+ // 处理 change 事件相关逻辑
439
+ handleConfirmFilter(params, props.mode === 'multiple' ? (option.data && option.data.length > 0) : !XEUtils.eqNull(option.data), option);
440
+ })
441
+ });
442
+ })
443
+ : column.filters.map((option, oIndex) => {
444
+ const optionValue = option.data;
445
+ const props = getCellEditFilterProps(renderOpts, params, optionValue);
446
+ return h('a-select', {
447
+ key: oIndex,
448
+ attrs,
449
+ props: Object.assign(Object.assign({}, props), { options: props.options || options }),
450
+ on: getFilterOns(renderOpts, params, option, () => {
451
+ // 处理 change 事件相关逻辑
452
+ handleConfirmFilter(params, props.mode === 'multiple' ? (option.data && option.data.length > 0) : !XEUtils.eqNull(option.data), option);
453
+ })
454
+ });
455
+ }))
456
+ ];
457
+ },
458
+ tableFilterDefaultMethod(params) {
459
+ const { option, row, column } = params;
460
+ const { data } = option;
461
+ const { field, filterRender: renderOpts } = column;
462
+ const { props = {} } = renderOpts;
463
+ const cellValue = XEUtils.get(row, field);
464
+ if (props.mode === 'multiple') {
465
+ if (XEUtils.isArray(cellValue)) {
466
+ return XEUtils.includeArrays(cellValue, data);
467
+ }
468
+ return data.indexOf(cellValue) > -1;
469
+ }
470
+ /* eslint-disable eqeqeq */
471
+ return cellValue == data;
472
+ },
473
+ tableExportMethod: createExportMethod(getSelectCellValue)
474
+ },
475
+ ACascader: {
476
+ renderTableEdit: createEditRender(),
477
+ renderTableCell(h, renderOpts, params) {
478
+ return getCellLabelVNs(h, renderOpts, params, getCascaderCellValue(renderOpts, params));
479
+ },
480
+ tableExportMethod: createExportMethod(getCascaderCellValue)
481
+ },
482
+ ADatePicker: {
483
+ renderTableEdit: createEditRender(),
484
+ renderTableCell: formatDatePicker(),
485
+ tableExportMethod: createDatePickerExportMethod()
486
+ },
487
+ AMonthPicker: {
488
+ renderTableEdit: createEditRender(),
489
+ renderTableCell: formatDatePicker('YYYY-MM'),
490
+ tableExportMethod: createDatePickerExportMethod('YYYY-MM')
491
+ },
492
+ ARangePicker: {
493
+ renderTableEdit: createEditRender(),
494
+ renderTableCell(h, renderOpts, params) {
495
+ return getCellLabelVNs(h, renderOpts, params, getRangePickerCellValue(renderOpts, params));
496
+ },
497
+ tableExportMethod: createExportMethod(getRangePickerCellValue)
498
+ },
499
+ AWeekPicker: {
500
+ renderTableEdit: createEditRender(),
501
+ renderTableCell: formatDatePicker('YYYY-WW周'),
502
+ tableExportMethod: createDatePickerExportMethod('YYYY-WW周')
503
+ },
504
+ ATimePicker: {
505
+ renderTableEdit: createEditRender(),
506
+ renderTableCell: formatTimePicker('HH:mm:ss'),
507
+ tableExportMethod: createDatePickerExportMethod('HH:mm:ss')
508
+ },
509
+ ATreeSelect: {
510
+ renderTableEdit: createEditRender(),
511
+ renderTableCell(h, renderOpts, params) {
512
+ return getCellLabelVNs(h, renderOpts, params, getTreeSelectCellValue(renderOpts, params));
513
+ },
514
+ tableExportMethod: createExportMethod(getTreeSelectCellValue)
515
+ },
516
+ ARate: {
517
+ renderTableDefault: createEditRender(),
518
+ renderTableEdit: createEditRender(),
519
+ renderTableFilter: createFilterRender(),
520
+ tableFilterDefaultMethod: defaultExactFilterMethod
521
+ },
522
+ ASwitch: {
523
+ renderTableDefault: createEditRender(),
524
+ renderTableEdit: createEditRender(),
525
+ renderTableFilter(h, renderOpts, params) {
526
+ const { column } = params;
527
+ const { name, attrs } = renderOpts;
528
+ return [
529
+ h('div', {
530
+ class: 'vxe-table--filter-antd-wrapper'
531
+ }, column.filters.map((option, oIndex) => {
532
+ const optionValue = option.data;
533
+ return h(name, {
534
+ key: oIndex,
535
+ attrs,
536
+ props: getCellEditFilterProps(renderOpts, params, optionValue),
537
+ on: getFilterOns(renderOpts, params, option, () => {
538
+ // 处理 change 事件相关逻辑
539
+ handleConfirmFilter(params, XEUtils.isBoolean(option.data), option);
540
+ })
541
+ });
542
+ }))
543
+ ];
544
+ },
545
+ tableFilterDefaultMethod: defaultExactFilterMethod
546
+ },
547
+ AButton: {
548
+ renderTableEdit: defaultButtonEditRender,
549
+ renderTableDefault: defaultButtonEditRender
550
+ },
551
+ AButtons: {
552
+ renderTableEdit: defaultButtonsEditRender,
553
+ renderTableDefault: defaultButtonsEditRender
554
+ }
555
+ });
556
+ }
@@ -0,0 +1,8 @@
1
+ import { componentMaps } from '../store';
2
+ export function getCurrComponent(name) {
3
+ const comp = componentMaps[name];
4
+ if (comp) {
5
+ return comp;
6
+ }
7
+ return 'name';
8
+ }