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