1 line
13 KiB
Plaintext
1 line
13 KiB
Plaintext
|
{"version":3,"file":"validation.mjs","names":["makeFocusProps","useForm","useProxiedModel","useToggleScope","computed","nextTick","onBeforeMount","onBeforeUnmount","onMounted","ref","shallowRef","unref","watch","getCurrentInstanceName","getUid","propsFactory","wrapInArray","makeValidationProps","disabled","type","Boolean","default","error","errorMessages","Array","String","maxErrors","Number","name","label","readonly","rules","modelValue","validateOn","validationValue","useValidation","props","arguments","length","undefined","id","model","validationModel","value","form","internalErrorMessages","isPristine","isDirty","isDisabled","isReadonly","concat","slice","Math","max","set","Set","split","blur","has","input","submit","lazy","isValid","isValidating","validationClasses","uid","register","validate","reset","resetValidation","unregister","update","focused","unwatch","val","silent","results","rule","handler","result","console","warn","push"],"sources":["../../src/composables/validation.ts"],"sourcesContent":["// Composables\nimport { makeFocusProps } from '@/composables/focus'\nimport { useForm } from '@/composables/form'\nimport { useProxiedModel } from '@/composables/proxiedModel'\nimport { useToggleScope } from '@/composables/toggleScope'\n\n// Utilities\nimport { computed, nextTick, onBeforeMount, onBeforeUnmount, onMounted, ref, shallowRef, unref, watch } from 'vue'\nimport { getCurrentInstanceName, getUid, propsFactory, wrapInArray } from '@/util'\n\n// Types\nimport type { PropType } from 'vue'\nimport type { EventProp, MaybeRef } from '@/util'\n\nexport type ValidationResult = string | boolean\nexport type ValidationRule =\n | ValidationResult\n | PromiseLike<ValidationResult>\n | ((value: any) => ValidationResult)\n | ((value: any) => PromiseLike<ValidationResult>)\n\ntype ValidateOnValue = 'blur' | 'input' | 'submit'\n\nexport interface ValidationProps {\n disabled: boolean | null\n error: boolean\n errorMessages: string | readonly string[] | null\n focused: boolean\n maxErrors: string | number\n name: string | undefined\n label: string | undefined\n readonly: boolean | null\n rules: readonly ValidationRule[]\n modelValue: any\n 'onUpdate:modelValue': EventProp | undefined\n validateOn?: ValidateOnValue | `${ValidateOnValue} lazy` | `lazy ${ValidateOnValue}` | 'lazy'\n validationValue: any\n}\n\nexport const makeValidationProps = propsFactory({\n disabled: {\n type: Boolean as PropType<boolean | null>,\n default: null,\n },\n error: Boolean,\n errorMessages: {\n type: [Array, String] as PropType<string | readonly string[] | null>,\n default: () => ([]),\n },\n maxErrors: {\n type: [Number, String],\n default: 1,\n },\n name: String,\n label: String,\n readonly: {\n type: Boolean as PropType<boolean | null>,\n default: null,\n },\n rules: {\n type: Array as PropType<readonly ValidationRule[]>,\n default: () => ([]),\n },\n modelValue: null,\n validateOn: String as PropType<ValidationProps['validateOn']>,\n validationValue: null,\n\n ...makeFocusProps(),\n}, 'validation')\n\nexport function useValidation (\n props: ValidationProps,\n name = getCurrentInstanceName(),\n id: MaybeRef<string | number> = getUid(),\n) {\n const model = useProxiedModel(props, 'modelValue')\n const validationModel = computed(() => props.validationValue === undefined ? model.value : props.validationValue)\n const form = useForm()\n const internalErrorMessages = ref<string[]>([])\n const isPristine = shallowRef(true)\n const isDirty = computed(() => !!(\n wrapInArray(model.value === '' ? null : model.value).length ||\n wrapInArray(validationModel.value === '' ? null : validationModel.value).length\n ))\n const isDisabled = computed(() => !!(props.disabled ?? form?.isDisabled.value))\n const isReadonly = computed(() => !!(props.readonly ?? form?.isReadonly.value))\n const errorMessages = computed(() => {\n return props.errorMessages?.length\n ? wrapInArray(props.errorMessages).concat(internalErrorMessages.value).slice(0, Math.max(0, +props.maxErrors))\
|