@carefrees/form-utils-vue 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/assets/index.css +207 -0
- package/esm/component.d.ts +3 -0
- package/esm/component.mjs +12 -0
- package/esm/form/index.d.ts +37 -0
- package/esm/form/index.mjs +6 -0
- package/esm/form/index.vue.d.ts +19 -0
- package/esm/form/index.vue.mjs +110 -0
- package/esm/form/index.vue2.mjs +4 -0
- package/esm/formItem/form.Item.base.vue.d.ts +16 -0
- package/esm/formItem/form.Item.base.vue.mjs +114 -0
- package/esm/formItem/form.Item.base.vue2.mjs +4 -0
- package/esm/formItem/form.hide.item.vue.d.ts +16 -0
- package/esm/formItem/form.hide.item.vue.mjs +78 -0
- package/esm/formItem/form.hide.item.vue2.mjs +4 -0
- package/esm/formItem/formItem.vue.d.ts +18 -0
- package/esm/formItem/formItem.vue.mjs +102 -0
- package/esm/formItem/formItem.vue2.mjs +4 -0
- package/esm/formItem/index.d.ts +71 -0
- package/esm/formItem/index.mjs +12 -0
- package/esm/hooks/attr/attr.FormItem.d.ts +876 -0
- package/esm/hooks/attr/attr.FormItem.mjs +120 -0
- package/esm/hooks/index.d.ts +13 -0
- package/esm/hooks/register/register.FormHideItem.d.ts +9 -0
- package/esm/hooks/register/register.FormHideItem.mjs +38 -0
- package/esm/hooks/register/register.FormItem.d.ts +802 -0
- package/esm/hooks/register/register.FormItem.mjs +55 -0
- package/esm/hooks/register/register.FormList.d.ts +789 -0
- package/esm/hooks/register/register.FormList.mjs +43 -0
- package/esm/hooks/register/register.form.d.ts +3 -0
- package/esm/hooks/register/register.form.mjs +12 -0
- package/esm/hooks/useAttrs.d.ts +24 -0
- package/esm/hooks/useAttrs.mjs +45 -0
- package/esm/hooks/useEffect.d.ts +2 -0
- package/esm/hooks/useEffect.mjs +14 -0
- package/esm/hooks/useForm.d.ts +8 -0
- package/esm/hooks/useForm.mjs +26 -0
- package/esm/hooks/useFormItem.d.ts +7 -0
- package/esm/hooks/useFormItem.mjs +26 -0
- package/esm/hooks/useFormItemParentName.d.ts +18 -0
- package/esm/hooks/useFormItemParentName.mjs +37 -0
- package/esm/hooks/useFormList.d.ts +7 -0
- package/esm/hooks/useFormList.mjs +26 -0
- package/esm/hooks/useHtmlFor.d.ts +2 -0
- package/esm/hooks/useHtmlFor.mjs +11 -0
- package/esm/hooks/useMultipleForm.d.ts +7 -0
- package/esm/hooks/useMultipleForm.mjs +27 -0
- package/esm/index.d.ts +11 -0
- package/esm/index.mjs +59 -0
- package/esm/instance/ruleIntsnace.d.ts +49 -0
- package/esm/instance/ruleIntsnace.mjs +73 -0
- package/esm/interface/index.d.ts +36 -0
- package/esm/interface/layout.d.ts +28 -0
- package/esm/interface/layout.formItem.d.ts +39 -0
- package/esm/layout/index.d.ts +65 -0
- package/esm/layout/index.mjs +12 -0
- package/esm/layout/layout.form.rows.vue.d.ts +10 -0
- package/esm/layout/layout.form.rows.vue.mjs +31 -0
- package/esm/layout/layout.form.rows.vue2.mjs +4 -0
- package/esm/layout/layout.formItem.vue.d.ts +18 -0
- package/esm/layout/layout.formItem.vue.mjs +216 -0
- package/esm/layout/layout.formItem.vue2.mjs +4 -0
- package/esm/layout/layout.vue.d.ts +16 -0
- package/esm/layout/layout.vue.mjs +166 -0
- package/esm/layout/layout.vue2.mjs +4 -0
- package/esm/utils/index.d.ts +1 -0
- package/esm/utils/withInstall.d.ts +3 -0
- package/esm/utils/withInstall.mjs +22 -0
- package/lib/component.d.ts +3 -0
- package/lib/component.js +12 -0
- package/lib/form/index.d.ts +37 -0
- package/lib/form/index.js +6 -0
- package/lib/form/index.vue.d.ts +19 -0
- package/lib/form/index.vue.js +110 -0
- package/lib/form/index.vue2.js +4 -0
- package/lib/formItem/form.Item.base.vue.d.ts +16 -0
- package/lib/formItem/form.Item.base.vue.js +114 -0
- package/lib/formItem/form.Item.base.vue2.js +4 -0
- package/lib/formItem/form.hide.item.vue.d.ts +16 -0
- package/lib/formItem/form.hide.item.vue.js +78 -0
- package/lib/formItem/form.hide.item.vue2.js +4 -0
- package/lib/formItem/formItem.vue.d.ts +18 -0
- package/lib/formItem/formItem.vue.js +102 -0
- package/lib/formItem/formItem.vue2.js +4 -0
- package/lib/formItem/index.d.ts +71 -0
- package/lib/formItem/index.js +12 -0
- package/lib/hooks/attr/attr.FormItem.d.ts +876 -0
- package/lib/hooks/attr/attr.FormItem.js +120 -0
- package/lib/hooks/index.d.ts +13 -0
- package/lib/hooks/register/register.FormHideItem.d.ts +9 -0
- package/lib/hooks/register/register.FormHideItem.js +38 -0
- package/lib/hooks/register/register.FormItem.d.ts +802 -0
- package/lib/hooks/register/register.FormItem.js +55 -0
- package/lib/hooks/register/register.FormList.d.ts +789 -0
- package/lib/hooks/register/register.FormList.js +43 -0
- package/lib/hooks/register/register.form.d.ts +3 -0
- package/lib/hooks/register/register.form.js +12 -0
- package/lib/hooks/useAttrs.d.ts +24 -0
- package/lib/hooks/useAttrs.js +45 -0
- package/lib/hooks/useEffect.d.ts +2 -0
- package/lib/hooks/useEffect.js +14 -0
- package/lib/hooks/useForm.d.ts +8 -0
- package/lib/hooks/useForm.js +26 -0
- package/lib/hooks/useFormItem.d.ts +7 -0
- package/lib/hooks/useFormItem.js +26 -0
- package/lib/hooks/useFormItemParentName.d.ts +18 -0
- package/lib/hooks/useFormItemParentName.js +37 -0
- package/lib/hooks/useFormList.d.ts +7 -0
- package/lib/hooks/useFormList.js +26 -0
- package/lib/hooks/useHtmlFor.d.ts +2 -0
- package/lib/hooks/useHtmlFor.js +11 -0
- package/lib/hooks/useMultipleForm.d.ts +7 -0
- package/lib/hooks/useMultipleForm.js +27 -0
- package/lib/index.d.ts +11 -0
- package/lib/index.js +59 -0
- package/lib/instance/ruleIntsnace.d.ts +49 -0
- package/lib/instance/ruleIntsnace.js +73 -0
- package/lib/interface/index.d.ts +36 -0
- package/lib/interface/layout.d.ts +28 -0
- package/lib/interface/layout.formItem.d.ts +39 -0
- package/lib/layout/index.d.ts +65 -0
- package/lib/layout/index.js +12 -0
- package/lib/layout/layout.form.rows.vue.d.ts +10 -0
- package/lib/layout/layout.form.rows.vue.js +31 -0
- package/lib/layout/layout.form.rows.vue2.js +4 -0
- package/lib/layout/layout.formItem.vue.d.ts +18 -0
- package/lib/layout/layout.formItem.vue.js +216 -0
- package/lib/layout/layout.formItem.vue2.js +4 -0
- package/lib/layout/layout.vue.d.ts +16 -0
- package/lib/layout/layout.vue.js +166 -0
- package/lib/layout/layout.vue2.js +4 -0
- package/lib/utils/index.d.ts +1 -0
- package/lib/utils/withInstall.d.ts +3 -0
- package/lib/utils/withInstall.js +22 -0
- package/package.json +33 -0
- package/src/component.ts +3 -0
- package/src/form/index.ts +3 -0
- package/src/form/index.vue +66 -0
- package/src/formItem/form.Item.base.vue +54 -0
- package/src/formItem/form.hide.item.vue +28 -0
- package/src/formItem/formItem.vue +38 -0
- package/src/formItem/index.ts +7 -0
- package/src/hooks/attr/attr.FormItem.tsx +170 -0
- package/src/hooks/index.ts +13 -0
- package/src/hooks/register/register.FormHideItem.ts +45 -0
- package/src/hooks/register/register.FormItem.ts +80 -0
- package/src/hooks/register/register.FormList.ts +49 -0
- package/src/hooks/register/register.form.ts +12 -0
- package/src/hooks/useAttrs.ts +66 -0
- package/src/hooks/useEffect.ts +13 -0
- package/src/hooks/useForm.ts +28 -0
- package/src/hooks/useFormItem.ts +28 -0
- package/src/hooks/useFormItemParentName.ts +49 -0
- package/src/hooks/useFormList.ts +28 -0
- package/src/hooks/useHtmlFor.ts +9 -0
- package/src/hooks/useMultipleForm.ts +29 -0
- package/src/index.ts +16 -0
- package/src/instance/ruleIntsnace.ts +105 -0
- package/src/interface/index.ts +40 -0
- package/src/interface/layout.formItem.ts +42 -0
- package/src/interface/layout.ts +29 -0
- package/src/layout/index.ts +8 -0
- package/src/layout/layout.form.rows.vue +19 -0
- package/src/layout/layout.formItem.vue +119 -0
- package/src/layout/layout.vue +97 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/withInstall.ts +28 -0
- package/src/vite-env.d.ts +1 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<form :class='cls' :style='style' @submit='onSubmit'>
|
|
3
|
+
<FormLayout v-bind='rest' :class='layoutClass' :style='layoutStyle'>
|
|
4
|
+
<slot />
|
|
5
|
+
</FormLayout>
|
|
6
|
+
</form>
|
|
7
|
+
|
|
8
|
+
</template>
|
|
9
|
+
|
|
10
|
+
<script lang="ts" setup>
|
|
11
|
+
import { computed, defineProps, withDefaults, reactive, defineEmits } from "vue"
|
|
12
|
+
import type { FormProps } from "../interface/index"
|
|
13
|
+
import { useRegisterForm } from "../hooks/register/register.form"
|
|
14
|
+
import { useForm, useFormProvide } from "./../hooks/useForm"
|
|
15
|
+
import FormLayout from "../layout/layout.vue"
|
|
16
|
+
import clx from 'classnames';
|
|
17
|
+
|
|
18
|
+
defineOptions({
|
|
19
|
+
name: 'Form',
|
|
20
|
+
inheritAttrs: false, // 可选,防止属性自动应用到根元素
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const props = withDefaults(defineProps<FormProps>(), {
|
|
24
|
+
})
|
|
25
|
+
const {
|
|
26
|
+
form,
|
|
27
|
+
style,
|
|
28
|
+
formData,
|
|
29
|
+
hideData,
|
|
30
|
+
hideRuleData,
|
|
31
|
+
name,
|
|
32
|
+
onFinish,
|
|
33
|
+
onFinishFailed,
|
|
34
|
+
onValuesChange,
|
|
35
|
+
layoutStyle,
|
|
36
|
+
layoutClass,
|
|
37
|
+
...rest
|
|
38
|
+
} = props
|
|
39
|
+
|
|
40
|
+
const cls = computed(() => clx('carefrees-form', props.class));
|
|
41
|
+
|
|
42
|
+
const formInstance = useForm(props.form);
|
|
43
|
+
formInstance.value.ctor(reactive(props.formData || {}), reactive(props.hideData || {}), reactive(props.hideRuleData || {}));
|
|
44
|
+
|
|
45
|
+
const emits = defineEmits(["finish", 'valuesChange', 'finishFailed'])
|
|
46
|
+
|
|
47
|
+
formInstance.value.onFinish = (...rest) => {
|
|
48
|
+
emits("finish", ...rest)
|
|
49
|
+
};
|
|
50
|
+
formInstance.value.onValuesChange = (...rest) => {
|
|
51
|
+
emits("valuesChange", ...rest)
|
|
52
|
+
};
|
|
53
|
+
formInstance.value.onFinishFailed = (...rest) => {
|
|
54
|
+
emits("finishFailed", ...rest)
|
|
55
|
+
};
|
|
56
|
+
useRegisterForm(formInstance.value, props.name)
|
|
57
|
+
|
|
58
|
+
const onSubmit = (event: Event) => {
|
|
59
|
+
event?.preventDefault?.();
|
|
60
|
+
event?.stopPropagation?.();
|
|
61
|
+
formInstance.value.submit?.();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
useFormProvide(formInstance.value);
|
|
65
|
+
|
|
66
|
+
</script>
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<template v-if='props.noStyle'>
|
|
3
|
+
<component :is="props.input" v-bind='toValue(formAttrs.attrsLastData)' />
|
|
4
|
+
</template>
|
|
5
|
+
<template v-else>
|
|
6
|
+
<LayoutFormItem :label='props.label' :required='isRequired' :error-layout='props.errorLayout'
|
|
7
|
+
:label-mode='props.labelMode' :only-rule-style='props.onlyRuleStyle' :show-colon='props.showColon'
|
|
8
|
+
:col-span='props.colSpan' :row-span='props.rowSpan' :html-for='formAttrs.htmlFor' :style='props.style'
|
|
9
|
+
:class='props.class' :label-style='props.labelStyle' :label-class='props.labelClass'
|
|
10
|
+
:validate-result='formAttrs.validateResult'>
|
|
11
|
+
<template v-for="(item, name) in slots" #[name]="slotProps">
|
|
12
|
+
<slot :name="name" v-bind="slotProps"></slot>
|
|
13
|
+
</template>
|
|
14
|
+
<template v-slot:default>
|
|
15
|
+
<component :is="props.input" v-bind='toValue(formAttrs.attrsLastData)' />
|
|
16
|
+
</template>
|
|
17
|
+
</LayoutFormItem>
|
|
18
|
+
</template>
|
|
19
|
+
</template>
|
|
20
|
+
|
|
21
|
+
<script lang="ts" setup>
|
|
22
|
+
import { defineProps, withDefaults, computed, toValue } from "vue"
|
|
23
|
+
import type { FormItemProps } from "../interface/index"
|
|
24
|
+
import { useFormItemAttr } from "../hooks/attr/attr.FormItem"
|
|
25
|
+
import LayoutFormItem from "../layout/layout.formItem.vue";
|
|
26
|
+
import { useFormItemParentNameProvide } from "../hooks/useFormItemParentName"
|
|
27
|
+
|
|
28
|
+
defineOptions({
|
|
29
|
+
name: 'FormItemBase',
|
|
30
|
+
inheritAttrs: false, // 可选,防止属性自动应用到根元素
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const props = withDefaults(defineProps<FormItemProps>(), {
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
const formAttrs = useFormItemAttr(props)
|
|
37
|
+
|
|
38
|
+
useFormItemParentNameProvide({
|
|
39
|
+
name: formAttrs.newName,
|
|
40
|
+
sort: formAttrs.newSort
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
const slots = defineSlots<{
|
|
44
|
+
label: (params: any) => any,
|
|
45
|
+
helpText: (params: any) => any,
|
|
46
|
+
extra: (params: any) => any,
|
|
47
|
+
}>()
|
|
48
|
+
|
|
49
|
+
const isRequired = computed(() => {
|
|
50
|
+
return !!props.required || !!(toValue(formAttrs.ruleInstance?.value?.rules) || [])?.find((item) => {
|
|
51
|
+
return item.required;
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
</script>
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<template v-if='!isHide'>
|
|
3
|
+
<FormItemBase v-bind='props'>
|
|
4
|
+
<template v-for="(item, name) in slots" #[name]="slotProps">
|
|
5
|
+
<slot :name="name" v-bind="slotProps"></slot>
|
|
6
|
+
</template>
|
|
7
|
+
</FormItemBase>
|
|
8
|
+
</template>
|
|
9
|
+
</template>
|
|
10
|
+
|
|
11
|
+
<script lang="ts" setup>
|
|
12
|
+
import { defineProps, withDefaults, } from "vue"
|
|
13
|
+
import type { FormItemProps } from "../interface/index"
|
|
14
|
+
import FormItemBase from "./form.Item.base.vue"
|
|
15
|
+
import { useRegisterFormHideItem } from "../hooks/register/register.FormHideItem"
|
|
16
|
+
defineOptions({
|
|
17
|
+
name: 'FormHideItem',
|
|
18
|
+
inheritAttrs: false, // 可选,防止属性自动应用到根元素
|
|
19
|
+
});
|
|
20
|
+
const props = withDefaults(defineProps<FormItemProps>(), {
|
|
21
|
+
})
|
|
22
|
+
const slots = defineSlots<{
|
|
23
|
+
label: (params: any) => any,
|
|
24
|
+
helpText: (params: any) => any,
|
|
25
|
+
extra: (params: any) => any,
|
|
26
|
+
}>()
|
|
27
|
+
const { isHide } = useRegisterFormHideItem({ name: props.name, sort: props.sort, isJoinParentField: props.isJoinParentField })
|
|
28
|
+
</script>
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<template v-if='props.name'>
|
|
3
|
+
<FormItemBase v-bind='props as FormItemProps'>
|
|
4
|
+
<template v-for="(_, name) in slots" #[name]="slotProps">
|
|
5
|
+
<slot :name="name" v-bind="slotProps"></slot>
|
|
6
|
+
</template>
|
|
7
|
+
</FormItemBase>
|
|
8
|
+
</template>
|
|
9
|
+
<template v-else>
|
|
10
|
+
<LayoutFormItem :label='props.label' :errorLayout='props.errorLayout' :labelMode='props.labelMode'
|
|
11
|
+
:onlyRuleStyle='props.onlyRuleStyle' :showColon='props.showColon' :colSpan='props.colSpan'
|
|
12
|
+
:rowSpan='props.rowSpan' :style='props.style' :class='props.class' :labelStyle='props.labelStyle'
|
|
13
|
+
:labelClass='props.labelClass'>
|
|
14
|
+
<template v-for="(_, name) in slots" #[name]="slotProps">
|
|
15
|
+
<slot :name="name" v-bind="slotProps"></slot>
|
|
16
|
+
</template>
|
|
17
|
+
</LayoutFormItem>
|
|
18
|
+
</template>
|
|
19
|
+
</template>
|
|
20
|
+
|
|
21
|
+
<script lang="ts" setup>
|
|
22
|
+
import { defineProps, withDefaults } from "vue"
|
|
23
|
+
import type { FormItemProps } from "../interface/index"
|
|
24
|
+
import LayoutFormItem from "../layout/layout.formItem.vue";
|
|
25
|
+
import FormItemBase from "./form.Item.base.vue"
|
|
26
|
+
defineOptions({
|
|
27
|
+
name: 'FormItem',
|
|
28
|
+
inheritAttrs: false, // 可选,防止属性自动应用到根元素
|
|
29
|
+
});
|
|
30
|
+
const props = withDefaults(defineProps<Partial<FormItemProps>>(), {
|
|
31
|
+
})
|
|
32
|
+
const slots = defineSlots<{
|
|
33
|
+
default: (params: any) => any,
|
|
34
|
+
label: (params: any) => any,
|
|
35
|
+
helpText: (params: any) => any,
|
|
36
|
+
extra: (params: any) => any,
|
|
37
|
+
}>()
|
|
38
|
+
</script>
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import _FormItemBase from './form.Item.base.vue';
|
|
2
|
+
import _FormItem from './formItem.vue';
|
|
3
|
+
import _FormHideItem from './form.hide.item.vue';
|
|
4
|
+
import { withInstall } from '../utils';
|
|
5
|
+
export const FormItemBase = withInstall(_FormItemBase);
|
|
6
|
+
export const FormItem = withInstall(_FormItem);
|
|
7
|
+
export const FormHideItem = withInstall(_FormHideItem);
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { FormInstanceBase, FormItemInstanceBase, get } from '@carefrees/form-utils';
|
|
2
|
+
import { useHtmlFor } from './../useHtmlFor';
|
|
3
|
+
import { useRegisterFormItem, RegisterFormItemOptions } from './../register/register.FormItem';
|
|
4
|
+
import { computed, ref, toValue, watch } from 'vue';
|
|
5
|
+
import type { RuleInstanceBase2 } from '../../instance/ruleIntsnace';
|
|
6
|
+
|
|
7
|
+
export interface FormItemAttrOptions extends RegisterFormItemOptions {
|
|
8
|
+
/**依赖更新项*/
|
|
9
|
+
dependencies?: string[];
|
|
10
|
+
/**通知 只用于校验规则提示 字段 */
|
|
11
|
+
noticeOnlyRuleDataField?: string[];
|
|
12
|
+
/**通知父级字段监听方法更新*/
|
|
13
|
+
isNoticeParentField?: boolean;
|
|
14
|
+
/**通知watch监听方法更新*/
|
|
15
|
+
noticeWatchField?: string[];
|
|
16
|
+
/**是否保护值(不进行表单项组件卸载重置初始值)*/
|
|
17
|
+
preserve?: boolean;
|
|
18
|
+
/**重写规则*/
|
|
19
|
+
useRules?: (ruleInstance: RuleInstanceBase2, form: FormInstanceBase, formItemInstance: FormItemInstanceBase) => void;
|
|
20
|
+
/**输入框属性重写*/
|
|
21
|
+
useAttrs?: (attrs: any, form: FormInstanceBase, formItemInstance: FormItemInstanceBase) => any;
|
|
22
|
+
/**输入框属性*/
|
|
23
|
+
inputAttrs?: Object;
|
|
24
|
+
/**传递组件字段*/
|
|
25
|
+
valuePropName?: string;
|
|
26
|
+
/**取值字段(默认和valuePropName值相同)*/
|
|
27
|
+
getValuePath?: string;
|
|
28
|
+
/**自定义获取值*/
|
|
29
|
+
getValueFromEvent?: (event: any, form: FormInstanceBase, formItemInstance: FormItemInstanceBase) => any;
|
|
30
|
+
/**值格式化*/
|
|
31
|
+
formatValue?: (value: any, form: FormInstanceBase, formItemInstance: FormItemInstanceBase, event: any) => any;
|
|
32
|
+
/**触发数据更新之后触发(用于数据联动之类的)*/
|
|
33
|
+
onAfterUpdate?: (value: any, form: FormInstanceBase, formItemInstance: FormItemInstanceBase, event: any) => void;
|
|
34
|
+
/**事件名称*/
|
|
35
|
+
trigger?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**表单项参数*/
|
|
39
|
+
export const useFormItemAttr = (options: FormItemAttrOptions) => {
|
|
40
|
+
const {
|
|
41
|
+
trigger = 'onChange',
|
|
42
|
+
dependencies,
|
|
43
|
+
noticeOnlyRuleDataField,
|
|
44
|
+
isNoticeParentField,
|
|
45
|
+
noticeWatchField,
|
|
46
|
+
preserve,
|
|
47
|
+
valuePropName = 'value',
|
|
48
|
+
getValuePath = valuePropName,
|
|
49
|
+
getValueFromEvent,
|
|
50
|
+
formatValue,
|
|
51
|
+
onAfterUpdate,
|
|
52
|
+
useAttrs,
|
|
53
|
+
useRules,
|
|
54
|
+
inputAttrs,
|
|
55
|
+
...rest
|
|
56
|
+
} = options;
|
|
57
|
+
const { formItemInstance, form, ruleInstance, newName, newSort, deepRefData } = useRegisterFormItem({ ...rest });
|
|
58
|
+
formItemInstance.dependencies = dependencies;
|
|
59
|
+
formItemInstance.noticeOnlyRuleDataField = noticeOnlyRuleDataField;
|
|
60
|
+
formItemInstance.isNoticeParentField = isNoticeParentField;
|
|
61
|
+
formItemInstance.onAfterUpdate = onAfterUpdate;
|
|
62
|
+
formItemInstance.noticeWatchField = noticeWatchField;
|
|
63
|
+
formItemInstance.preserve = preserve;
|
|
64
|
+
/**获取值*/
|
|
65
|
+
const oldValue = ref(get(form.formData, toValue(newName)));
|
|
66
|
+
|
|
67
|
+
watch(
|
|
68
|
+
() => [get(form.formData, toValue(newName)), toValue(newName)],
|
|
69
|
+
() => {
|
|
70
|
+
oldValue.value = form.getFieldValue(newName.value);
|
|
71
|
+
},
|
|
72
|
+
{ immediate: true },
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const onValueChange = (event: any) => {
|
|
76
|
+
try {
|
|
77
|
+
let value = event;
|
|
78
|
+
if (typeof getValueFromEvent === 'function') {
|
|
79
|
+
value = getValueFromEvent(event, form, formItemInstance);
|
|
80
|
+
} else if (event && event.target && typeof event.target === 'object' && getValuePath in event.target) {
|
|
81
|
+
value = get(event.target, getValuePath);
|
|
82
|
+
}
|
|
83
|
+
if (typeof formatValue === 'function') {
|
|
84
|
+
value = formatValue(value, form, formItemInstance, event);
|
|
85
|
+
}
|
|
86
|
+
if (oldValue.value !== value) {
|
|
87
|
+
form.updatedFieldValue(newName.value, value, 'validate');
|
|
88
|
+
formItemInstance.onAfterUpdate?.(value, form, formItemInstance, event);
|
|
89
|
+
if (Array.isArray(formItemInstance.noticeWatchField) && formItemInstance.noticeWatchField.length) {
|
|
90
|
+
form.noticeWatch(formItemInstance.noticeWatchField);
|
|
91
|
+
}
|
|
92
|
+
if (
|
|
93
|
+
Array.isArray(formItemInstance.noticeOnlyRuleDataField) &&
|
|
94
|
+
formItemInstance.noticeOnlyRuleDataField.length
|
|
95
|
+
) {
|
|
96
|
+
form.onlyValidate(formItemInstance.noticeOnlyRuleDataField);
|
|
97
|
+
}
|
|
98
|
+
if (formItemInstance.isNoticeParentField && formItemInstance.parentDataField) {
|
|
99
|
+
form.notice(formItemInstance.parentDataField);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
} catch (error) {
|
|
103
|
+
console.log(error);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
formItemInstance.onChange = onValueChange;
|
|
108
|
+
const htmlFor = useHtmlFor(newName);
|
|
109
|
+
|
|
110
|
+
watch(
|
|
111
|
+
() => htmlFor,
|
|
112
|
+
() => {
|
|
113
|
+
formItemInstance.htmlFor = htmlFor.value;
|
|
114
|
+
},
|
|
115
|
+
{ immediate: true },
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
/**属性处理*/
|
|
119
|
+
const newAttrs = computed(() => {
|
|
120
|
+
const _attr = inputAttrs || {};
|
|
121
|
+
return {
|
|
122
|
+
..._attr,
|
|
123
|
+
[trigger]: onValueChange,
|
|
124
|
+
name: toValue(newName),
|
|
125
|
+
id: toValue(htmlFor),
|
|
126
|
+
[valuePropName]: toValue(oldValue),
|
|
127
|
+
};
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const attrsLastData = ref(toValue(newAttrs));
|
|
131
|
+
|
|
132
|
+
watch(
|
|
133
|
+
() => [toValue(newAttrs), toValue(deepRefData), toValue(oldValue)],
|
|
134
|
+
() => {
|
|
135
|
+
attrsLastData.value = useAttrs?.(newAttrs.value, form, formItemInstance) || newAttrs.value;
|
|
136
|
+
},
|
|
137
|
+
{ immediate: true },
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
/**规则处理**/
|
|
141
|
+
const validateResult = ref(ruleInstance.value.getValidateResult());
|
|
142
|
+
|
|
143
|
+
watch(
|
|
144
|
+
() => [toValue(deepRefData), toValue(oldValue)],
|
|
145
|
+
() => {
|
|
146
|
+
useRules?.(ruleInstance.value as any, form, formItemInstance);
|
|
147
|
+
},
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
watch(
|
|
151
|
+
() => [ruleInstance.value.messages, ruleInstance.value.rules, toValue(oldValue)],
|
|
152
|
+
() => {
|
|
153
|
+
validateResult.value = ruleInstance.value.getValidateResult();
|
|
154
|
+
},
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
valuePropName,
|
|
159
|
+
htmlFor,
|
|
160
|
+
onChange: onValueChange,
|
|
161
|
+
formItemInstance,
|
|
162
|
+
form,
|
|
163
|
+
ruleInstance,
|
|
164
|
+
newAttrs,
|
|
165
|
+
attrsLastData,
|
|
166
|
+
validateResult,
|
|
167
|
+
newSort,
|
|
168
|
+
newName,
|
|
169
|
+
};
|
|
170
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from './useAttrs';
|
|
2
|
+
export * from './useForm';
|
|
3
|
+
export * from './useFormItem';
|
|
4
|
+
export * from './useFormList';
|
|
5
|
+
export * from './useHtmlFor';
|
|
6
|
+
export * from './useMultipleForm';
|
|
7
|
+
export * from './useFormItemParentName';
|
|
8
|
+
export * from './useEffect';
|
|
9
|
+
export * from './register/register.FormHideItem';
|
|
10
|
+
export * from './register/register.FormItem';
|
|
11
|
+
export * from './register/register.FormList';
|
|
12
|
+
export * from './register/register.form';
|
|
13
|
+
export * from './attr/attr.FormItem';
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { ref, watch } from 'vue';
|
|
2
|
+
import { RegisterFormItemOptions } from './register.FormItem';
|
|
3
|
+
import { FormHideItemInstanceBase } from '@carefrees/form-utils';
|
|
4
|
+
import { useFormItemParentNameInject } from '../useFormItemParentName';
|
|
5
|
+
import { useFormInject } from '../useForm';
|
|
6
|
+
import { useEffect } from '../useEffect';
|
|
7
|
+
|
|
8
|
+
interface RegisterFormHideItemOptions extends Omit<RegisterFormItemOptions, 'rules'> {}
|
|
9
|
+
|
|
10
|
+
/**注册表单隐藏表单项到表单实例中*/
|
|
11
|
+
export const useRegisterFormHideItem = (options: RegisterFormHideItemOptions) => {
|
|
12
|
+
const { name, sort, isJoinParentField = true } = options;
|
|
13
|
+
const form = useFormInject();
|
|
14
|
+
const { newName, newSort } = useFormItemParentNameInject({ name, sort, isJoinParentField });
|
|
15
|
+
const hideItemInstance = ref<FormHideItemInstanceBase>(new FormHideItemInstanceBase().ctor(newName.value));
|
|
16
|
+
const isHide = ref(form.getFieldHideValue(newName.value));
|
|
17
|
+
watch(
|
|
18
|
+
[isHide.value],
|
|
19
|
+
() => {
|
|
20
|
+
hideItemInstance.value.preHideValue = isHide.value;
|
|
21
|
+
},
|
|
22
|
+
{ immediate: true },
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const setValue = () => {
|
|
26
|
+
isHide.value = form.getFieldHideValue(newName.value);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
hideItemInstance.value.updatedItem = setValue;
|
|
30
|
+
|
|
31
|
+
watch(
|
|
32
|
+
[form, newSort.value],
|
|
33
|
+
() => {
|
|
34
|
+
hideItemInstance.value.instance = form;
|
|
35
|
+
hideItemInstance.value.sort = newSort.value;
|
|
36
|
+
},
|
|
37
|
+
{ immediate: true },
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
return form.registerFormHideItem(hideItemInstance.value as FormHideItemInstanceBase);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
return { form, isHide };
|
|
45
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 注册组件
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { ref, toValue, watch } from 'vue';
|
|
6
|
+
import type { RuleItem } from 'async-validator';
|
|
7
|
+
import { useFormInject } from '../useForm';
|
|
8
|
+
import { useFormItem } from '../useFormItem';
|
|
9
|
+
import { useFormItemParentNameInject } from '../useFormItemParentName';
|
|
10
|
+
import { useEffect } from '../useEffect';
|
|
11
|
+
import { RuleInstanceBase2 } from '../../instance/ruleIntsnace';
|
|
12
|
+
|
|
13
|
+
export interface RegisterFormItemOptions {
|
|
14
|
+
/**字段*/
|
|
15
|
+
name: string;
|
|
16
|
+
/**规则*/
|
|
17
|
+
rules?: RuleItem[];
|
|
18
|
+
/**排序值*/
|
|
19
|
+
sort?: string;
|
|
20
|
+
/**是否拼接父级字段*/
|
|
21
|
+
isJoinParentField?: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**注册表单项到表单实例中*/
|
|
25
|
+
export const useRegisterFormItem = (options: RegisterFormItemOptions) => {
|
|
26
|
+
const { name, rules, sort, isJoinParentField = true } = options;
|
|
27
|
+
const form = useFormInject();
|
|
28
|
+
const deepRefData = ref<any>({});
|
|
29
|
+
const { newName, newSort, parentItem } = useFormItemParentNameInject({ name, sort, isJoinParentField });
|
|
30
|
+
// 注册规则
|
|
31
|
+
// 注册单个实例
|
|
32
|
+
const ruleInstance = ref(new RuleInstanceBase2());
|
|
33
|
+
const formItemInstance = useFormItem();
|
|
34
|
+
|
|
35
|
+
watch(
|
|
36
|
+
[form],
|
|
37
|
+
() => {
|
|
38
|
+
ruleInstance.value.instance = form;
|
|
39
|
+
formItemInstance.instance = form;
|
|
40
|
+
},
|
|
41
|
+
{ immediate: true },
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
watch(
|
|
45
|
+
[newName.value],
|
|
46
|
+
() => {
|
|
47
|
+
ruleInstance.value.ctor(newName.value, rules || []);
|
|
48
|
+
formItemInstance.ctor(newName.value, ruleInstance.value as any);
|
|
49
|
+
},
|
|
50
|
+
{ immediate: true },
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
watch(
|
|
54
|
+
[newSort.value],
|
|
55
|
+
() => {
|
|
56
|
+
ruleInstance.value.sort = newSort.value;
|
|
57
|
+
formItemInstance.sort = newSort.value;
|
|
58
|
+
},
|
|
59
|
+
{ immediate: true },
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
watch(
|
|
63
|
+
[parentItem.value],
|
|
64
|
+
() => {
|
|
65
|
+
formItemInstance.parentDataField = toValue(parentItem.value.name);
|
|
66
|
+
},
|
|
67
|
+
{ immediate: true },
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const updated = () => {
|
|
71
|
+
deepRefData.value = { a: new Date().valueOf() };
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
formItemInstance.updated = updated;
|
|
75
|
+
ruleInstance.value.updated = updated;
|
|
76
|
+
|
|
77
|
+
useEffect(() => form.registerFormItem(formItemInstance));
|
|
78
|
+
|
|
79
|
+
return { ruleInstance, formItemInstance, form, newName, newSort, parentItem, deepRefData };
|
|
80
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { watch, toValue } from 'vue';
|
|
2
|
+
import { RegisterFormItemOptions, useRegisterFormItem } from './register.FormItem';
|
|
3
|
+
import { useFormList } from '../useFormList';
|
|
4
|
+
import { useEffect } from '../useEffect';
|
|
5
|
+
|
|
6
|
+
export interface RegisterFormListOptions extends RegisterFormItemOptions {}
|
|
7
|
+
|
|
8
|
+
/**注册表单List到表单实例中*/
|
|
9
|
+
export const useRegisterFormList = (options: RegisterFormListOptions) => {
|
|
10
|
+
const { ruleInstance, formItemInstance, form, newName, parentItem } = useRegisterFormItem(options);
|
|
11
|
+
const formListInstance = useFormList();
|
|
12
|
+
|
|
13
|
+
watch(
|
|
14
|
+
[newName.value],
|
|
15
|
+
() => {
|
|
16
|
+
formListInstance.ctor(newName.value);
|
|
17
|
+
},
|
|
18
|
+
{ immediate: true },
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
watch(
|
|
22
|
+
[form, formItemInstance, ruleInstance.value],
|
|
23
|
+
() => {
|
|
24
|
+
formListInstance.instance = form;
|
|
25
|
+
formListInstance.formItemInstance = formItemInstance;
|
|
26
|
+
formListInstance.rule = ruleInstance.value as any;
|
|
27
|
+
},
|
|
28
|
+
{ immediate: true },
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
watch(
|
|
32
|
+
[options.sort, parentItem.value],
|
|
33
|
+
() => {
|
|
34
|
+
formListInstance.sort = toValue(parentItem.value.sort);
|
|
35
|
+
formListInstance.parentDataField = toValue(parentItem.value.name);
|
|
36
|
+
},
|
|
37
|
+
{ immediate: true },
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
return form.registerFormList(newName.value, formListInstance);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
ruleInstance,
|
|
46
|
+
formItemInstance,
|
|
47
|
+
formListInstance,
|
|
48
|
+
};
|
|
49
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useMultipleFormInject } from '../useMultipleForm';
|
|
2
|
+
import { FormInstanceBase } from '@carefrees/form-utils';
|
|
3
|
+
import { useEffect } from '../useEffect';
|
|
4
|
+
|
|
5
|
+
/**注册表单实例到多表单收集实例中*/
|
|
6
|
+
export const useRegisterForm = (form: FormInstanceBase, name?: string) => {
|
|
7
|
+
const multipleForm = useMultipleFormInject();
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
if (name) return multipleForm.ctor(name, form);
|
|
10
|
+
});
|
|
11
|
+
return multipleForm;
|
|
12
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**公共属性*/
|
|
2
|
+
import { provide, inject, computed, StyleValue, ComputedRef, ref, toRefs, reactive } from 'vue';
|
|
3
|
+
import { ComputedRefBase } from '../interface';
|
|
4
|
+
|
|
5
|
+
export type AttrsOptions = {
|
|
6
|
+
/**列数据*/
|
|
7
|
+
colCount?: ComputedRefBase<number | undefined>;
|
|
8
|
+
/**规则校验失败错误提示位置*/
|
|
9
|
+
errorLayout?: ComputedRefBase<'left-bottom' | 'right-bottom' | 'top-right' | 'top-left' | undefined>;
|
|
10
|
+
/**label显示模式*/
|
|
11
|
+
labelMode?: ComputedRefBase<'left' | 'top' | 'hide' | undefined>;
|
|
12
|
+
/**是否显示label后的冒号*/
|
|
13
|
+
showColon?: ComputedRefBase<boolean | undefined>;
|
|
14
|
+
/**表单项 className*/
|
|
15
|
+
formItemClass?: ComputedRefBase<string | undefined>;
|
|
16
|
+
/**表单项 style*/
|
|
17
|
+
formItemStyle?: ComputedRefBase<StyleValue | undefined>;
|
|
18
|
+
/**表单项 label className*/
|
|
19
|
+
formItemLabelClass?: ComputedRefBase<string | undefined>;
|
|
20
|
+
/**表单项 label style*/
|
|
21
|
+
formItemLabelStyle?: ComputedRefBase<StyleValue | undefined>;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const attrsProvideSymbol = Symbol('carefrees-attrs');
|
|
25
|
+
|
|
26
|
+
/**公共属性 Context */
|
|
27
|
+
export function useAttrsProvide(options: AttrsOptions) {
|
|
28
|
+
const {
|
|
29
|
+
colCount = ref(4),
|
|
30
|
+
errorLayout = ref('left-bottom'),
|
|
31
|
+
labelMode = ref('top'),
|
|
32
|
+
showColon = ref(true),
|
|
33
|
+
formItemClass,
|
|
34
|
+
formItemStyle,
|
|
35
|
+
formItemLabelClass,
|
|
36
|
+
formItemLabelStyle,
|
|
37
|
+
} = options;
|
|
38
|
+
const data = computed(() => {
|
|
39
|
+
return {
|
|
40
|
+
colCount,
|
|
41
|
+
errorLayout,
|
|
42
|
+
labelMode,
|
|
43
|
+
showColon,
|
|
44
|
+
formItemClass,
|
|
45
|
+
formItemStyle,
|
|
46
|
+
formItemLabelClass,
|
|
47
|
+
formItemLabelStyle,
|
|
48
|
+
};
|
|
49
|
+
});
|
|
50
|
+
provide(attrsProvideSymbol, reactive(data));
|
|
51
|
+
}
|
|
52
|
+
/**子项中获取公共属性*/
|
|
53
|
+
export function useAttrsInject() {
|
|
54
|
+
const attrs = inject<ComputedRef<AttrsOptions>>(
|
|
55
|
+
attrsProvideSymbol,
|
|
56
|
+
computed(() =>
|
|
57
|
+
toRefs({
|
|
58
|
+
colCount: 4,
|
|
59
|
+
errorLayout: 'left-bottom',
|
|
60
|
+
labelMode: 'top',
|
|
61
|
+
showColon: true,
|
|
62
|
+
}),
|
|
63
|
+
),
|
|
64
|
+
);
|
|
65
|
+
return attrs;
|
|
66
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { onMounted, onUnmounted, ref } from 'vue';
|
|
2
|
+
|
|
3
|
+
/**挂载卸载*/
|
|
4
|
+
export const useEffect = (fun: () => Function | undefined) => {
|
|
5
|
+
const unRegisterRef = ref();
|
|
6
|
+
onMounted(() => {
|
|
7
|
+
unRegisterRef.value = fun();
|
|
8
|
+
});
|
|
9
|
+
onUnmounted(() => {
|
|
10
|
+
typeof unRegisterRef.value === 'function' && unRegisterRef.value();
|
|
11
|
+
unRegisterRef.value = undefined;
|
|
12
|
+
});
|
|
13
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { FormInstanceBase } from '@carefrees/form-utils';
|
|
2
|
+
import { provide, inject, ref, Ref } from 'vue';
|
|
3
|
+
|
|
4
|
+
const formProvideSymbol = Symbol('carefrees-form');
|
|
5
|
+
|
|
6
|
+
/**表单实例 Context */
|
|
7
|
+
export function useFormProvide<T = any>(form: FormInstanceBase<T>) {
|
|
8
|
+
provide(formProvideSymbol, form);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**子项中获取表单实例*/
|
|
12
|
+
export function useFormInject<T = any>() {
|
|
13
|
+
const form = inject<FormInstanceBase<T>>(formProvideSymbol, new FormInstanceBase<T>());
|
|
14
|
+
return form;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**初始化表单实例*/
|
|
18
|
+
export function useForm<T = any>(form?: FormInstanceBase<T>) {
|
|
19
|
+
const refForm = ref<FormInstanceBase<T>>();
|
|
20
|
+
if (!refForm.value) {
|
|
21
|
+
if (form) {
|
|
22
|
+
refForm.value = form;
|
|
23
|
+
} else {
|
|
24
|
+
refForm.value = new FormInstanceBase<T>();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return refForm as Ref<FormInstanceBase<T>>;
|
|
28
|
+
}
|