field-input-limits.js 871 Bytes
/** 数字输入长度限制 — @see docs/features/packageDynamicForm/ARCHITECTURE.md */
import { DEFAULT_NUMBER_INPUT_MAX_LENGTH } from '../config/formFieldLimits.js'

/** 解析 numberInput 字段的有效 maxLength(schema 与默认值取较小者) */
export function resolveNumberInputMaxLength(field = {}) {
	if (field.type !== 'numberInput') {
		return undefined
	}
	const schemaMax = field.rules?.maxLength
	if (schemaMax != null && schemaMax < DEFAULT_NUMBER_INPUT_MAX_LENGTH) {
		return schemaMax
	}
	return DEFAULT_NUMBER_INPUT_MAX_LENGTH
}

/** 将字符串截断至 maxLength */
export function truncateToMaxLength(value, maxLength) {
	if (value == null || maxLength == null) {
		return value ?? ''
	}
	const str = String(value)
	if (str.length <= maxLength) {
		return str
	}
	return str.slice(0, maxLength)
}

export { DEFAULT_NUMBER_INPUT_MAX_LENGTH }