UNPKG

vue-admin-core

Version:
182 lines (179 loc) 5.14 kB
import { getCurrentInstance, defineComponent, ref, watch, createVNode, mergeProps } from 'vue'; import { useForm, createForm, FragmentComponent, FormProvider } from '@formily/vue'; import '../useRequest/index.mjs'; import '../../formily/index.mjs'; import { isObjectField } from '@formily/core'; import { isFunction } from 'lodash-es'; import useRequest from '../useRequest/src/useRequest.mjs'; import { searchFormProps } from '../../formily/search-form/src/index.mjs'; import { inputEnterEffect } from '../../formily/__builtins__/shared/effects.mjs'; import { Submit } from '../../formily/submit/src/index.mjs'; import { Reset } from '../../formily/reset/src/index.mjs'; const useSearchForm = (request, options, formProps, SchemaField) => { const app = getCurrentInstance(); const form = useForm(); if (!form.value) { form.value = createForm(formProps); } const SelfSchemaField = (app == null ? void 0 : app.appContext.components.SchemaField) || SchemaField; if (!SelfSchemaField) { throw new Error(`SchemaField \u627E\u4E0D\u5230\uFF0C\u5728\u5168\u5C40\u8BBE\u7F6Eapp.use(setupFormily)\u6216\u8005\u4F20\u5165SchemaField`); } const _uid = "searchForm"; const result = useRequest(request, options); const SearchForm = defineComponent({ name: "UseSearchForm", inheritAttrs: false, props: { ...searchFormProps, schema: { type: Object, required: true }, components: { type: Object }, scope: { type: Object }, name: { type: [String, Number] }, definitions: { type: Object }, onSearch: { type: Function }, onReset: { type: Function }, addonAfter: { type: Object }, addonBefore: { type: Object }, searchButtonText: { type: String, default: "\u67E5\u8BE2" }, searchButtonProps: { type: Object }, resetButtonText: { type: String, default: "\u91CD\u7F6E" }, resetButtonProps: { type: Object } }, setup(props, { slots, expose }) { const { schema, components, scope, name, ...reset } = props; const resetLoading = ref(false); const _form = useForm(); if (_form.value) form.value = _form.value; const handleReset = async () => { var _a; resetLoading.value = true; if (isFunction(props.onReset)) { const res2 = await ((_a = props.onReset) == null ? void 0 : _a.call(props)); if (res2) { await result.runAsync(res2); } } else { await result.runAsync(res); } resetLoading.value = false; }; const handleSearch = async (params) => { var _a; if (isFunction(props.onSearch)) { const res2 = await ((_a = props.onSearch) == null ? void 0 : _a.call(props, params)); if (res2) { await result.runAsync(res2); } } else { await result.runAsync(params); } }; form.value.addEffects("search_form_enter", () => { inputEnterEffect("*", () => { form.value.query(_uid).take((field) => { if (isObjectField(field)) { field.submit(handleSearch); } }); }); }); expose({ search: handleSearch, reset: handleReset }); watch(() => options == null ? void 0 : options.defaultParams, (value) => { result.runAsync({ ...form.value.values[_uid], ...value == null ? void 0 : value[0] }); }, { deep: true }); const actions = () => createVNode(FragmentComponent, null, { default: () => [createVNode(Submit, mergeProps(props.searchButtonProps, { "onSubmit": handleSearch }), { default: () => [props.searchButtonText] }), createVNode(Reset, mergeProps({ "resetValidateSuccess": handleReset }, props.resetButtonProps, { "loading": resetLoading.value }), { default: () => [props.resetButtonText] })] }); const _schema = { type: "object", definitions: props.definitions, properties: { [_uid]: { type: "object", "x-component": "SearchForm", "x-component-props": reset, "x-content": { actions: slots.actions ? slots.actions : actions }, properties: schema } } }; return () => createVNode(FormProvider, { "form": form.value }, { default: () => [createVNode(SelfSchemaField, { "schema": _schema, "components": components, "scope": scope, "name": name }, null)] }); } }); return { form, SearchForm, request: result }; }; export { useSearchForm as default, useSearchForm }; //# sourceMappingURL=useSearchForm.mjs.map