@vxe-ui/plugin-render-antd 3.0.1 → 3.0.3

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