188 lines
5.3 KiB
JavaScript
188 lines
5.3 KiB
JavaScript
var __defProp = Object.defineProperty;
|
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __export = (target, all) => {
|
|
for (var name2 in all)
|
|
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
};
|
|
var __copyProps = (to, from, except, desc) => {
|
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
for (let key of __getOwnPropNames(from))
|
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
}
|
|
return to;
|
|
};
|
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
var stdin_exports = {};
|
|
__export(stdin_exports, {
|
|
default: () => stdin_default,
|
|
formProps: () => formProps
|
|
});
|
|
module.exports = __toCommonJS(stdin_exports);
|
|
var import_vue = require("vue");
|
|
var import_utils = require("../utils");
|
|
var import_use = require("@vant/use");
|
|
var import_use_expose = require("../composables/use-expose");
|
|
const [name, bem] = (0, import_utils.createNamespace)("form");
|
|
const formProps = {
|
|
colon: Boolean,
|
|
disabled: Boolean,
|
|
readonly: Boolean,
|
|
required: [Boolean, String],
|
|
showError: Boolean,
|
|
labelWidth: import_utils.numericProp,
|
|
labelAlign: String,
|
|
inputAlign: String,
|
|
scrollToError: Boolean,
|
|
scrollToErrorPosition: String,
|
|
validateFirst: Boolean,
|
|
submitOnEnter: import_utils.truthProp,
|
|
showErrorMessage: import_utils.truthProp,
|
|
errorMessageAlign: String,
|
|
validateTrigger: {
|
|
type: [String, Array],
|
|
default: "onBlur"
|
|
}
|
|
};
|
|
var stdin_default = (0, import_vue.defineComponent)({
|
|
name,
|
|
props: formProps,
|
|
emits: ["submit", "failed"],
|
|
setup(props, {
|
|
emit,
|
|
slots
|
|
}) {
|
|
const {
|
|
children,
|
|
linkChildren
|
|
} = (0, import_use.useChildren)(import_utils.FORM_KEY);
|
|
const getFieldsByNames = (names) => {
|
|
if (names) {
|
|
return children.filter((field) => names.includes(field.name));
|
|
}
|
|
return children;
|
|
};
|
|
const validateSeq = (names) => new Promise((resolve, reject) => {
|
|
const errors = [];
|
|
const fields = getFieldsByNames(names);
|
|
fields.reduce((promise, field) => promise.then(() => {
|
|
if (!errors.length) {
|
|
return field.validate().then((error) => {
|
|
if (error) {
|
|
errors.push(error);
|
|
}
|
|
});
|
|
}
|
|
}), Promise.resolve()).then(() => {
|
|
if (errors.length) {
|
|
reject(errors);
|
|
} else {
|
|
resolve();
|
|
}
|
|
});
|
|
});
|
|
const validateAll = (names) => new Promise((resolve, reject) => {
|
|
const fields = getFieldsByNames(names);
|
|
Promise.all(fields.map((item) => item.validate())).then((errors) => {
|
|
errors = errors.filter(Boolean);
|
|
if (errors.length) {
|
|
reject(errors);
|
|
} else {
|
|
resolve();
|
|
}
|
|
});
|
|
});
|
|
const validateField = (name2) => {
|
|
const matched = children.find((item) => item.name === name2);
|
|
if (matched) {
|
|
return new Promise((resolve, reject) => {
|
|
matched.validate().then((error) => {
|
|
if (error) {
|
|
reject(error);
|
|
} else {
|
|
resolve();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
return Promise.reject();
|
|
};
|
|
const validate = (name2) => {
|
|
if (typeof name2 === "string") {
|
|
return validateField(name2);
|
|
}
|
|
return props.validateFirst ? validateSeq(name2) : validateAll(name2);
|
|
};
|
|
const resetValidation = (name2) => {
|
|
if (typeof name2 === "string") {
|
|
name2 = [name2];
|
|
}
|
|
const fields = getFieldsByNames(name2);
|
|
fields.forEach((item) => {
|
|
item.resetValidation();
|
|
});
|
|
};
|
|
const getValidationStatus = () => children.reduce((form, field) => {
|
|
form[field.name] = field.getValidationStatus();
|
|
return form;
|
|
}, {});
|
|
const scrollToField = (name2, options) => {
|
|
children.some((item) => {
|
|
if (item.name === name2) {
|
|
item.$el.scrollIntoView(options);
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
};
|
|
const getValues = () => children.reduce((form, field) => {
|
|
if (field.name !== void 0) {
|
|
form[field.name] = field.formValue.value;
|
|
}
|
|
return form;
|
|
}, {});
|
|
const submit = () => {
|
|
const values = getValues();
|
|
validate().then(() => emit("submit", values)).catch((errors) => {
|
|
emit("failed", {
|
|
values,
|
|
errors
|
|
});
|
|
const {
|
|
scrollToError,
|
|
scrollToErrorPosition
|
|
} = props;
|
|
if (scrollToError && errors[0].name) {
|
|
scrollToField(errors[0].name, scrollToErrorPosition ? {
|
|
block: scrollToErrorPosition
|
|
} : void 0);
|
|
}
|
|
});
|
|
};
|
|
const onSubmit = (event) => {
|
|
(0, import_utils.preventDefault)(event);
|
|
submit();
|
|
};
|
|
linkChildren({
|
|
props
|
|
});
|
|
(0, import_use_expose.useExpose)({
|
|
submit,
|
|
validate,
|
|
getValues,
|
|
scrollToField,
|
|
resetValidation,
|
|
getValidationStatus
|
|
});
|
|
return () => {
|
|
var _a;
|
|
return (0, import_vue.createVNode)("form", {
|
|
"class": bem(),
|
|
"onSubmit": onSubmit
|
|
}, [(_a = slots.default) == null ? void 0 : _a.call(slots)]);
|
|
};
|
|
}
|
|
});
|