form-group-repeater.js
3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { IMG_URL } from '../../../utils/imageBase.js'
const { buildRowValuesFromFields } = require('../../utils/order-list-params.js')
const { resolveSubformMaxRows } = require('../../form-engine/subform-limits.js')
const {
buildUpgradeIconUrl,
FORM_NAV_ICON_LEFT,
FORM_NAV_ICON_RIGHT
} = require('../../utils/upgrade-icon-url.js')
Component({
options: {
styleIsolation: 'apply-shared'
},
properties: {
group: {
type: Object,
value: {}
},
visibleRowFields: {
type: Array,
value: []
},
rowTotal: {
type: Number,
value: 0
},
activeIndex: {
type: Number,
value: 0
},
disabled: {
type: Boolean,
value: false
},
errors: {
type: Object,
value: {}
},
requestContext: {
type: Object,
value: {}
}
},
data: {
IMG_URL,
rowValues: {},
canAdd: true,
navIconLeft: '',
navIconRight: ''
},
lifetimes: {
attached() {
this.setData({
navIconLeft: buildUpgradeIconUrl(FORM_NAV_ICON_LEFT),
navIconRight: buildUpgradeIconUrl(FORM_NAV_ICON_RIGHT)
})
}
},
observers: {
visibleRowFields(visibleRowFields = []) {
this.setData({
rowValues: buildRowValuesFromFields(visibleRowFields)
})
},
'group, rowTotal, disabled'() {
const group = this.properties.group || {}
const rowTotal = this.properties.rowTotal || 0
const disabled = this.properties.disabled
const maxRows = resolveSubformMaxRows(group)
this.setData({
canAdd: !disabled && rowTotal < maxRows
})
}
},
methods: {
getActiveRowIndex() {
const total = this.properties.rowTotal || 0
let index = this.properties.activeIndex || 0
if (total > 0 && index >= total) index = total - 1
if (index < 0) index = 0
return index
},
onAdd() {
if (this.properties.disabled || !this.data.canAdd) return
this.triggerEvent('add', { groupKey: this.properties.group?.key })
},
onRemove() {
if (this.properties.disabled) return
this.triggerEvent('remove', {
groupKey: this.properties.group?.key,
rowIndex: this.getActiveRowIndex()
})
},
onNav(e) {
const direction = Number(e.currentTarget.dataset.direction) || 0
this.triggerEvent('nav', {
groupKey: this.properties.group?.key,
direction
})
},
resolveRowIndexFromDetail(detail = {}) {
const raw = detail.rowIndex
if (raw !== undefined && raw !== null && raw !== '') {
const index = Number(raw)
if (!Number.isNaN(index) && index >= 0) {
return index
}
}
return this.getActiveRowIndex()
},
onFieldChange(e) {
const childKey = e.detail?.key
const value = e.detail?.value
const displayLabel = e.detail?.displayLabel
const metaValue = e.detail?.metaValue
const orderRaw = e.detail?.orderRaw
if (!childKey) return
this.triggerEvent('change', {
groupKey: this.properties.group?.key,
rowIndex: this.resolveRowIndexFromDetail(e.detail),
childKey,
value,
displayLabel,
metaValue,
orderRaw
})
},
onFieldBlur(e) {
this.triggerEvent('blur', {
...e.detail,
groupKey: this.properties.group?.key,
rowIndex: this.resolveRowIndexFromDetail(e.detail)
})
}
}
})