@ithinkdt/core
Version:
iThinkDT Core
25 lines (24 loc) • 1.01 kB
JavaScript
import { unref } from 'vue'
function minmax(min, max, message, { trigger = 'blur' } = {}) {
return {
message,
trigger,
validator: (value) => {
if (value === undefined || value === null) {
return
}
const minVal = unref(min)
const maxVal = unref(max)
if (typeof value === 'number') {
if (typeof minVal === 'number' && value < minVal) return false
if (typeof maxVal === 'number' && value > maxVal) return false
} else if (typeof value === 'string' || Array.isArray(value)) {
if (typeof minVal === 'number' && value.length < minVal) return false
if (typeof maxVal === 'number' && value.length > maxVal) return false
}
},
}
}
const min = (min, message, options) => minmax(min, undefined, message, options)
const max = (max, message, options) => minmax(undefined, max, message, options)
export { minmax, min, max }