form-field-number.js 3.88 KB
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
			})
		}
	}
})