hongluan-ui
Version:
Hongluan Component Library for Vue 3
151 lines (148 loc) • 4.77 kB
JavaScript
import { defineComponent, computed, watch, reactive, toRefs, provide, openBlock, createElementBlock, normalizeClass, normalizeStyle, renderSlot } from 'vue';
import '../../../hooks/index.mjs';
import '../../../utils/index.mjs';
import '../../../tokens/index.mjs';
import { formProps } from './form.mjs';
import { filterFields } from './utils.mjs';
import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
import { debugWarn } from '../../../utils/error.mjs';
import { isFunction } from '@vue/shared';
import { formContextKey } from '../../../tokens/form.mjs';
import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
const COMPONENT_NAME = "Form";
const _sfc_main = defineComponent({
name: COMPONENT_NAME,
props: formProps,
emits: ["validate"],
setup(props, { emit }) {
const fields = [];
const getField = (prop) => {
return fields.find((field) => field.prop === prop);
};
const addField = (field) => {
fields.push(field);
};
const removeField = (field) => {
if (field.prop) {
fields.splice(fields.indexOf(field), 1);
}
};
const resetFields = (properties = []) => {
if (!props.model) {
debugWarn(COMPONENT_NAME, "model is required for resetFields to work.");
return;
}
filterFields(fields, properties).forEach((field) => field.resetField());
};
const clearValidate = (props2 = []) => {
filterFields(fields, props2).forEach((field) => field.clearValidate());
};
const isValidatable = computed(() => {
const hasModel = !!props.model;
if (!hasModel) {
debugWarn(COMPONENT_NAME, "model is required for validate to work.");
}
return hasModel;
});
const obtainValidateFields = (props2) => {
if (fields.length === 0)
return [];
const filteredFields = filterFields(fields, props2);
if (!filteredFields.length) {
debugWarn(COMPONENT_NAME, "please pass correct props!");
return [];
}
return filteredFields;
};
const validate = async (callback) => validateField(void 0, callback);
const doValidateField = async (props2 = []) => {
if (!isValidatable.value)
return false;
const fields2 = obtainValidateFields(props2);
if (fields2.length === 0)
return true;
let validationErrors = {};
for (const field of fields2) {
try {
await field.validate("");
} catch (fields3) {
validationErrors = {
...validationErrors,
...fields3
};
}
}
if (Object.keys(validationErrors).length === 0)
return true;
return Promise.reject(validationErrors);
};
const validateField = async (modelProps = [], callback) => {
const shouldThrow = !isFunction(callback);
try {
const result = await doValidateField(modelProps);
if (result === true) {
await (callback == null ? void 0 : callback(result));
}
return result;
} catch (e) {
if (e instanceof Error)
throw e;
const invalidFields = e;
if (props.scrollToError) {
scrollToField(Object.keys(invalidFields)[0]);
}
await (callback == null ? void 0 : callback(false, invalidFields));
return shouldThrow && Promise.reject(invalidFields);
}
};
const scrollToField = (prop) => {
var _a, _b;
const field = filterFields(fields, prop)[0];
if (field) {
(_b = (_a = field.$el) == null ? void 0 : _a.$el) == null ? void 0 : _b.scrollIntoView(props.scrollIntoViewOptions);
}
};
watch(() => props.rules, () => {
if (props.validateOnRuleChange) {
validate().catch((err) => debugWarn(err));
}
}, { deep: true });
const formContext = reactive({
...toRefs(props),
emit,
resetFields,
clearValidate,
validateField,
getField,
addField,
removeField
});
provide(formContextKey, formContext);
const { namespace } = useNamespace("form");
return {
namespace,
validate,
resetFields,
clearValidate,
validateField,
scrollToField,
fields
};
}
});
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return openBlock(), createElementBlock("form", {
class: normalizeClass({
[_ctx.namespace]: true,
"inline": _ctx.inline
}),
style: normalizeStyle([
_ctx.gap ? `--form-gap:${_ctx.gap}` : ""
])
}, [
renderSlot(_ctx.$slots, "default")
], 6);
}
var Form = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
export { Form as default };
//# sourceMappingURL=form2.mjs.map