import html from "html";
import { defineAsyncComponent, ref, reactive, watch } from "vue";
export default {
components: { AppFormItem: defineAsyncComponent(() => import("./form-item.js")) },
name: "AppForm",
template: html`
`,
props: ["modelValue", "inline", "schema", "action", "hideButton", "isQueryForm", "mode"],
emits: ["update:modelValue", "submit"],
setup(props, context) {
// init
const model = reactive(props.modelValue);
watch(model, (value) => {
context.emit("update:modelValue", value);
});
// ref
const formRef = ref(null);
const loading = ref(false);
//
const errors = ref({});
// reset
const reset = () => {
formRef.value.resetFields();
};
// validate
const validate = async () => {
return formRef.value.validate();
};
// submit
const submit = async () => {
try {
const valid = await validate();
if (valid) {
loading.value = true;
context.emit(
"submit",
(serverErrors) => {
if (serverErrors) {
errors.value = serverErrors;
}
},
loading
);
}
} catch (error) {
console.log(error);
}
};
// expose
context.expose({ validate, reset });
return {
model,
formRef,
loading,
errors,
reset,
submit,
};
},
};