You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 lines
15 KiB
1 lines
15 KiB
{"ast":null,"code":"import { defineComponent, computed, watch, provide, reactive, toRefs, openBlock, createElementBlock, normalizeClass, unref, renderSlot } from 'vue';\nimport { formContextKey } from './constants.mjs';\nimport { formProps, formEmits } from './form.mjs';\nimport { useFormLabelWidth, filterFields } from './utils.mjs';\nimport _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';\nimport { useFormSize } from './hooks/use-form-common-props.mjs';\nimport { useNamespace } from '../../../hooks/use-namespace/index.mjs';\nimport { debugWarn } from '../../../utils/error.mjs';\nimport { isFunction } from '@vue/shared';\nconst COMPONENT_NAME = \"ElForm\";\nconst __default__ = defineComponent({\n name: COMPONENT_NAME\n});\nconst _sfc_main = /* @__PURE__ */defineComponent({\n ...__default__,\n props: formProps,\n emits: formEmits,\n setup(__props, {\n expose,\n emit\n }) {\n const props = __props;\n const fields = [];\n const formSize = useFormSize();\n const ns = useNamespace(\"form\");\n const formClasses = computed(() => {\n const {\n labelPosition,\n inline\n } = props;\n return [ns.b(), ns.m(formSize.value || \"default\"), {\n [ns.m(`label-${labelPosition}`)]: labelPosition,\n [ns.m(\"inline\")]: inline\n }];\n });\n const getField = prop => {\n return fields.find(field => field.prop === prop);\n };\n const addField = field => {\n fields.push(field);\n };\n const removeField = field => {\n if (field.prop) {\n fields.splice(fields.indexOf(field), 1);\n }\n };\n const resetFields = (properties = []) => {\n if (!props.model) {\n debugWarn(COMPONENT_NAME, \"model is required for resetFields to work.\");\n return;\n }\n filterFields(fields, properties).forEach(field => field.resetField());\n };\n const clearValidate = (props2 = []) => {\n filterFields(fields, props2).forEach(field => field.clearValidate());\n };\n const isValidatable = computed(() => {\n const hasModel = !!props.model;\n if (!hasModel) {\n debugWarn(COMPONENT_NAME, \"model is required for validate to work.\");\n }\n return hasModel;\n });\n const obtainValidateFields = props2 => {\n if (fields.length === 0) return [];\n const filteredFields = filterFields(fields, props2);\n if (!filteredFields.length) {\n debugWarn(COMPONENT_NAME, \"please pass correct props!\");\n return [];\n }\n return filteredFields;\n };\n const validate = async callback => validateField(void 0, callback);\n const doValidateField = async (props2 = []) => {\n if (!isValidatable.value) return false;\n const fields2 = obtainValidateFields(props2);\n if (fields2.length === 0) return true;\n let validationErrors = {};\n for (const field of fields2) {\n try {\n await field.validate(\"\");\n if (field.validateState === \"error\") field.resetField();\n } catch (fields3) {\n validationErrors = {\n ...validationErrors,\n ...fields3\n };\n }\n }\n if (Object.keys(validationErrors).length === 0) return true;\n return Promise.reject(validationErrors);\n };\n const validateField = async (modelProps = [], callback) => {\n const shouldThrow = !isFunction(callback);\n try {\n const result = await doValidateField(modelProps);\n if (result === true) {\n await (callback == null ? void 0 : callback(result));\n }\n return result;\n } catch (e) {\n if (e instanceof Error) throw e;\n const invalidFields = e;\n if (props.scrollToError) {\n scrollToField(Object.keys(invalidFields)[0]);\n }\n await (callback == null ? void 0 : callback(false, invalidFields));\n return shouldThrow && Promise.reject(invalidFields);\n }\n };\n const scrollToField = prop => {\n var _a;\n const field = filterFields(fields, prop)[0];\n if (field) {\n (_a = field.$el) == null ? void 0 : _a.scrollIntoView(props.scrollIntoViewOptions);\n }\n };\n watch(() => props.rules, () => {\n if (props.validateOnRuleChange) {\n validate().catch(err => debugWarn(err));\n }\n }, {\n deep: true,\n flush: \"post\"\n });\n provide(formContextKey, reactive({\n ...toRefs(props),\n emit,\n resetFields,\n clearValidate,\n validateField,\n getField,\n addField,\n removeField,\n ...useFormLabelWidth()\n }));\n expose({\n validate,\n validateField,\n resetFields,\n clearValidate,\n scrollToField,\n fields\n });\n return (_ctx, _cache) => {\n return openBlock(), createElementBlock(\"form\", {\n class: normalizeClass(unref(formClasses))\n }, [renderSlot(_ctx.$slots, \"default\")], 2);\n };\n }\n});\nvar Form = /* @__PURE__ */_export_sfc(_sfc_main, [[\"__file\", \"form.vue\"]]);\nexport { Form as default };","map":{"version":3,"names":["name","COMPONENT_NAME","fields","formSize","useFormSize","ns","useNamespace","formClasses","computed","labelPosition","inline","props","b","m","value","getField","prop","find","field","addField","push","removeField","splice","indexOf","resetFields","properties","model","debugWarn","filterFields","forEach","resetField","clearValidate","props2","isValidatable","hasModel","obtainValidateFields","length","filteredFields","validate","callback","validateField","doValidateField","fields2","validationErrors","validateState","fields3","Object","keys","Promise","reject","modelProps","shouldThrow","isFunction","result","e","Error","invalidFields","scrollToError","scrollToField","_a","$el","scrollIntoView","scrollIntoViewOptions","watch","rules","validateOnRuleChange","catch","err","deep","flush","provide","formContextKey","reactive","toRefs","emit","useFormLabelWidth","expose","_ctx","_cache","openBlock","createElementBlock","class","normalizeClass","unref","renderSlot","$slots","Form","_export_sfc","_sfc_main"],"sources":["../../../../../../packages/components/form/src/form.vue"],"sourcesContent":["<template>\n <form :class=\"formClasses\">\n <slot />\n </form>\n</template>\n\n<script lang=\"ts\" setup>\nimport { computed, provide, reactive, toRefs, watch } from 'vue'\nimport { debugWarn, isFunction } from '@element-plus/utils'\nimport { useNamespace } from '@element-plus/hooks'\nimport { useFormSize } from './hooks'\nimport { formContextKey } from './constants'\nimport { formEmits, formProps } from './form'\nimport { filterFields, useFormLabelWidth } from './utils'\n\nimport type { ValidateFieldsError } from 'async-validator'\nimport type { Arrayable } from '@element-plus/utils'\nimport type {\n FormContext,\n FormItemContext,\n FormValidateCallback,\n FormValidationResult,\n} from './types'\nimport type { FormItemProp } from './form-item'\n\nconst COMPONENT_NAME = 'ElForm'\ndefineOptions({\n name: COMPONENT_NAME,\n})\nconst props = defineProps(formProps)\nconst emit = defineEmits(formEmits)\n\nconst fields: FormItemContext[] = []\n\nconst formSize = useFormSize()\nconst ns = useNamespace('form')\nconst formClasses = computed(() => {\n const { labelPosition, inline } = props\n return [\n ns.b(),\n // todo: in v2.2.0, we can remove default\n // in fact, remove it doesn't affect the final style\n ns.m(formSize.value || 'default'),\n {\n [ns.m(`label-${labelPosition}`)]: labelPosition,\n [ns.m('inline')]: inline,\n },\n ]\n})\n\nconst getField: FormContext['getField'] = (prop) => {\n return fields.find((field) => field.prop === prop)\n}\n\nconst addField: FormContext['addField'] = (field) => {\n fields.push(field)\n}\n\nconst removeField: FormContext['removeField'] = (field) => {\n if (field.prop) {\n fields.splice(fields.indexOf(field), 1)\n }\n}\n\nconst resetFields: FormContext['resetFields'] = (properties = []) => {\n if (!props.model) {\n debugWarn(COMPONENT_NAME, 'model is required for resetFields to work.')\n return\n }\n filterFields(fields, properties).forEach((field) => field.resetField())\n}\n\nconst clearValidate: FormContext['clearValidate'] = (props = []) => {\n filterFields(fields, props).forEach((field) => field.clearValidate())\n}\n\nconst isValidatable = computed(() => {\n const hasModel = !!props.model\n if (!hasModel) {\n debugWarn(COMPONENT_NAME, 'model is required for validate to work.')\n }\n return hasModel\n})\n\nconst obtainValidateFields = (props: Arrayable<FormItemProp>) => {\n if (fields.length === 0) return []\n\n const filteredFields = filterFields(fields, props)\n if (!filteredFields.length) {\n debugWarn(COMPONENT_NAME, 'please pass correct props!')\n return []\n }\n return filteredFields\n}\n\nconst validate = async (\n callback?: FormValidateCallback\n): FormValidationResult => validateField(undefined, callback)\n\nconst doValidateField = async (\n props: Arrayable<FormItemProp> = []\n): Promise<boolean> => {\n if (!isValidatable.value) return false\n\n const fields = obtainValidateFields(props)\n if (fields.length === 0) return true\n\n let validationErrors: ValidateFieldsError = {}\n for (const field of fields) {\n try {\n await field.validate('')\n if (field.validateState === 'error') field.resetField()\n } catch (fields) {\n validationErrors = {\n ...validationErrors,\n ...(fields as ValidateFieldsError),\n }\n }\n }\n\n if (Object.keys(validationErrors).length === 0) return true\n return Promise.reject(validationErrors)\n}\n\nconst validateField: FormContext['validateField'] = async (\n modelProps = [],\n callback\n) => {\n const shouldThrow = !isFunction(callback)\n try {\n const result = await doValidateField(modelProps)\n // When result is false meaning that the fields are not validatable\n if (result === true) {\n await callback?.(result)\n }\n return result\n } catch (e) {\n if (e instanceof Error) throw e\n\n const invalidFields = e as ValidateFieldsError\n\n if (props.scrollToError) {\n scrollToField(Object.keys(invalidFields)[0])\n }\n await callback?.(false, invalidFields)\n return shouldThrow && Promise.reject(invalidFields)\n }\n}\n\nconst scrollToField = (prop: FormItemProp) => {\n const field = filterFields(fields, prop)[0]\n if (field) {\n field.$el?.scrollIntoView(props.scrollIntoViewOptions)\n }\n}\n\nwatch(\n () => props.rules,\n () => {\n if (props.validateOnRuleChange) {\n validate().catch((err) => debugWarn(err))\n }\n },\n { deep: true, flush: 'post' }\n)\n\nprovide(\n formContextKey,\n reactive({\n ...toRefs(props),\n emit,\n\n resetFields,\n clearValidate,\n validateField,\n getField,\n addField,\n removeField,\n\n ...useFormLabelWidth(),\n })\n)\n\ndefineExpose({\n /**\n * @description Validate the whole form. Receives a callback or returns `Promise`.\n */\n validate,\n /**\n * @description Validate specified fields.\n */\n validateField,\n /**\n * @description Reset specified fields and remove validation result.\n */\n resetFields,\n /**\n * @description Clear validation message for specified fields.\n */\n clearValidate,\n /**\n * @description Scroll to the specified fields.\n */\n scrollToField,\n /**\n * @description All fields context.\n */\n fields,\n})\n</script>\n"],"mappings":";;;;;;;;;;mCA0Bc;EACZA,IAAM,EAAAC;AACR;;;;;;;;;;IAIA,MAAMC,MAAA,GAA4B,EAAC;IAEnC,MAAMC,QAAA,GAAWC,WAAY;IACvB,MAAAC,EAAA,GAAKC,YAAA,CAAa,MAAM;IACxB,MAAAC,WAAA,GAAcC,QAAA,CAAS,MAAM;MAC3B;QAAEC,aAAe;QAAAC;MAAA,CAAW,GAAAC,KAAA;MAC3B,QACLN,EAAA,CAAGO,CAAE,IAAAP,EAAA,CAAAQ,CAAA,CAAAV,QAAA,CAAAW,KAAA;QAGF,CAAAT,EAAE,CAASQ,CAAA,UAAAJ,aAAkB,MAAAA,aAAA;QAChC,CAAAJ,EAAA,CAAAQ,CAAA,aAAAH;MAAA,EACoC;IAChB,CACpB;IACF,MAAAK,QAAA,GAAAC,IAAA;MACD,OAAAd,MAAA,CAAAe,IAAA,CAAAC,KAAA,IAAAA,K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