form-field-number.js
3.88 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
148
149
const {
resolveNumberInputMaxLength,
truncateToMaxLength,
isNonNegativeIntegerInput,
sanitizeNonNegativeIntegerInput,
normalizeNonNegativeIntegerDisplay,
coerceNonNegativeIntegerInputValue,
isDecimalInput,
sanitizeDecimalInput,
normalizeDecimalDisplay,
coerceDecimalInputValue
} = require('../../form-engine/field-input-limits.js')
Component({
properties: {
field: {
type: Object,
value: {}
},
value: {
type: null,
observer: 'syncDisplayValue'
},
disabled: {
type: Boolean,
value: false
},
error: {
type: String,
value: ''
},
groupKey: { type: String, value: '' },
rowIndex: { type: Number, value: -1 }
},
data: {
displayValue: '',
unit: '',
maxLength: 10,
inputType: 'digit'
},
observers: {
field: function (field) {
const f = field || {}
const rules = f.rules || {}
const unit = f.unit || rules.unit || ''
const maxLength = resolveNumberInputMaxLength(f) ?? 10
const inputType = isNonNegativeIntegerInput(f) ? 'number' : 'digit'
const next = {}
if (unit !== this.data.unit) {
next.unit = unit
}
if (maxLength !== this.data.maxLength) {
next.maxLength = maxLength
}
if (inputType !== this.data.inputType) {
next.inputType = inputType
}
if (Object.keys(next).length) {
this.setData(next)
}
}
},
methods: {
syncDisplayValue(value) {
const displayValue =
value === null || value === undefined || value === '' ? '' : String(value)
this.setData({ displayValue })
},
buildChangeDetail(field, value) {
const detail = { key: field.key, value }
if (this.properties.groupKey) {
detail.groupKey = this.properties.groupKey
const rowIndex = Number(this.properties.rowIndex)
if (!Number.isNaN(rowIndex) && rowIndex >= 0) {
detail.rowIndex = rowIndex
}
}
return detail
},
onInput(e) {
if (this.data.disabled) return
const field = this.data.field || {}
if (isNonNegativeIntegerInput(field)) {
const rawValue = sanitizeNonNegativeIntegerInput(
e.detail.value.trim(),
field
)
const value = coerceNonNegativeIntegerInputValue(rawValue)
this.setData({ displayValue: rawValue })
this.triggerEvent('change', this.buildChangeDetail(field, value))
return
}
if (isDecimalInput(field)) {
const rawValue = sanitizeDecimalInput(e.detail.value.trim(), field)
const value = coerceDecimalInputValue(rawValue)
this.setData({ displayValue: rawValue })
this.triggerEvent('change', this.buildChangeDetail(field, value))
return
}
const maxLength = resolveNumberInputMaxLength(field) ?? this.data.maxLength
const rawValue = truncateToMaxLength(e.detail.value.trim(), maxLength)
let value = rawValue
if (rawValue !== '') {
const num = Number(rawValue)
if (!Number.isNaN(num)) {
value = num
}
}
this.setData({ displayValue: rawValue })
this.triggerEvent('change', this.buildChangeDetail(field, value))
},
onBlur() {
const field = this.data.field || {}
let blurValue = this.properties.value
if (isNonNegativeIntegerInput(field)) {
const normalized = normalizeNonNegativeIntegerDisplay(this.data.displayValue)
const value = coerceNonNegativeIntegerInputValue(normalized)
blurValue = value
if (normalized !== this.data.displayValue || value !== this.properties.value) {
this.setData({ displayValue: normalized })
this.triggerEvent('change', this.buildChangeDetail(field, value))
}
} else if (isDecimalInput(field)) {
const normalized = normalizeDecimalDisplay(this.data.displayValue)
const value = coerceDecimalInputValue(normalized)
blurValue = value
if (normalized !== this.data.displayValue || value !== this.properties.value) {
this.setData({ displayValue: normalized })
this.triggerEvent('change', this.buildChangeDetail(field, value))
}
}
this.triggerEvent('blur', {
key: field.key,
value: blurValue
})
}
}
})