@carefrees/form-utils-vue-hooks 0.0.13
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 +7 -0
- package/esm/hooks/attr/attr.FormItem.d.ts +94 -0
- package/esm/hooks/attr/attr.FormItem.js +104 -0
- package/esm/hooks/index.d.ts +13 -0
- package/esm/hooks/index.js +13 -0
- package/esm/hooks/register/register.FormHideItem.d.ts +9 -0
- package/esm/hooks/register/register.FormHideItem.js +26 -0
- package/esm/hooks/register/register.FormItem.d.ts +24 -0
- package/esm/hooks/register/register.FormItem.js +67 -0
- package/esm/hooks/register/register.FormList.d.ts +9 -0
- package/esm/hooks/register/register.FormList.js +42 -0
- package/esm/hooks/register/register.form.d.ts +4 -0
- package/esm/hooks/register/register.form.js +10 -0
- package/esm/hooks/useAttrs.d.ts +25 -0
- package/esm/hooks/useAttrs.js +26 -0
- package/esm/hooks/useEffect.d.ts +2 -0
- package/esm/hooks/useEffect.js +12 -0
- package/esm/hooks/useForm.d.ts +8 -0
- package/esm/hooks/useForm.js +21 -0
- package/esm/hooks/useFormItem.d.ts +8 -0
- package/esm/hooks/useFormItem.js +21 -0
- package/esm/hooks/useFormItemParentName.d.ts +18 -0
- package/esm/hooks/useFormItemParentName.js +47 -0
- package/esm/hooks/useFormList.d.ts +8 -0
- package/esm/hooks/useFormList.js +21 -0
- package/esm/hooks/useHtmlFor.d.ts +2 -0
- package/esm/hooks/useHtmlFor.js +7 -0
- package/esm/hooks/useMultipleForm.d.ts +8 -0
- package/esm/hooks/useMultipleForm.js +21 -0
- package/esm/hooks/useRule.d.ts +4 -0
- package/esm/hooks/useRule.js +12 -0
- package/esm/index.d.ts +4 -0
- package/esm/index.js +4 -0
- package/esm/instance/formInstance.d.ts +58 -0
- package/esm/instance/formInstance.js +132 -0
- package/esm/instance/formItemBaseInstance.d.ts +23 -0
- package/esm/instance/formItemBaseInstance.js +6 -0
- package/esm/instance/formItemInstance.d.ts +18 -0
- package/esm/instance/formItemInstance.js +11 -0
- package/esm/instance/formListInstance.d.ts +43 -0
- package/esm/instance/formListInstance.js +95 -0
- package/esm/instance/index.d.ts +5 -0
- package/esm/instance/index.js +5 -0
- package/esm/instance/multipleInstance.d.ts +32 -0
- package/esm/instance/multipleInstance.js +77 -0
- package/esm/instance/ruleIntsnace.d.ts +45 -0
- package/esm/instance/ruleIntsnace.js +52 -0
- package/esm/interface/index.d.ts +31 -0
- package/esm/interface/index.js +0 -0
- package/esm/utils/cloneByNamePathList.d.ts +3 -0
- package/esm/utils/cloneByNamePathList.js +11 -0
- package/esm/utils/get.d.ts +26 -0
- package/esm/utils/get.js +13 -0
- package/esm/utils/index.d.ts +4 -0
- package/esm/utils/index.js +4 -0
- package/esm/utils/interface.d.ts +3 -0
- package/esm/utils/interface.js +0 -0
- package/esm/utils/set.d.ts +32 -0
- package/esm/utils/set.js +29 -0
- package/esm/utils/utils.d.ts +172 -0
- package/esm/utils/utils.js +131 -0
- package/lib/hooks/attr/attr.FormItem.d.ts +94 -0
- package/lib/hooks/attr/attr.FormItem.js +138 -0
- package/lib/hooks/index.d.ts +13 -0
- package/lib/hooks/index.js +168 -0
- package/lib/hooks/register/register.FormHideItem.d.ts +9 -0
- package/lib/hooks/register/register.FormHideItem.js +60 -0
- package/lib/hooks/register/register.FormItem.d.ts +24 -0
- package/lib/hooks/register/register.FormItem.js +101 -0
- package/lib/hooks/register/register.FormList.d.ts +9 -0
- package/lib/hooks/register/register.FormList.js +76 -0
- package/lib/hooks/register/register.form.d.ts +4 -0
- package/lib/hooks/register/register.form.js +44 -0
- package/lib/hooks/useAttrs.d.ts +25 -0
- package/lib/hooks/useAttrs.js +63 -0
- package/lib/hooks/useEffect.d.ts +2 -0
- package/lib/hooks/useEffect.js +46 -0
- package/lib/hooks/useForm.d.ts +8 -0
- package/lib/hooks/useForm.js +61 -0
- package/lib/hooks/useFormItem.d.ts +8 -0
- package/lib/hooks/useFormItem.js +61 -0
- package/lib/hooks/useFormItemParentName.d.ts +18 -0
- package/lib/hooks/useFormItemParentName.js +84 -0
- package/lib/hooks/useFormList.d.ts +8 -0
- package/lib/hooks/useFormList.js +61 -0
- package/lib/hooks/useHtmlFor.d.ts +2 -0
- package/lib/hooks/useHtmlFor.js +41 -0
- package/lib/hooks/useMultipleForm.d.ts +8 -0
- package/lib/hooks/useMultipleForm.js +61 -0
- package/lib/hooks/useRule.d.ts +4 -0
- package/lib/hooks/useRule.js +46 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +87 -0
- package/lib/instance/formInstance.d.ts +58 -0
- package/lib/instance/formInstance.js +166 -0
- package/lib/instance/formItemBaseInstance.d.ts +23 -0
- package/lib/instance/formItemBaseInstance.js +40 -0
- package/lib/instance/formItemInstance.d.ts +18 -0
- package/lib/instance/formItemInstance.js +45 -0
- package/lib/instance/formListInstance.d.ts +43 -0
- package/lib/instance/formListInstance.js +129 -0
- package/lib/instance/index.d.ts +5 -0
- package/lib/instance/index.js +96 -0
- package/lib/instance/multipleInstance.d.ts +32 -0
- package/lib/instance/multipleInstance.js +111 -0
- package/lib/instance/ruleIntsnace.d.ts +45 -0
- package/lib/instance/ruleIntsnace.js +96 -0
- package/lib/interface/index.d.ts +31 -0
- package/lib/interface/index.js +18 -0
- package/lib/utils/cloneByNamePathList.d.ts +3 -0
- package/lib/utils/cloneByNamePathList.js +45 -0
- package/lib/utils/get.d.ts +26 -0
- package/lib/utils/get.js +47 -0
- package/lib/utils/index.d.ts +4 -0
- package/lib/utils/index.js +87 -0
- package/lib/utils/interface.d.ts +3 -0
- package/lib/utils/interface.js +18 -0
- package/lib/utils/set.d.ts +32 -0
- package/lib/utils/set.js +63 -0
- package/lib/utils/utils.d.ts +172 -0
- package/lib/utils/utils.js +198 -0
- package/package.json +30 -0
- package/src/hooks/attr/attr.FormItem.tsx +185 -0
- package/src/hooks/index.ts +13 -0
- package/src/hooks/register/register.FormHideItem.ts +28 -0
- package/src/hooks/register/register.FormItem.ts +93 -0
- package/src/hooks/register/register.FormList.ts +49 -0
- package/src/hooks/register/register.form.ts +13 -0
- package/src/hooks/useAttrs.ts +64 -0
- package/src/hooks/useEffect.ts +13 -0
- package/src/hooks/useForm.ts +34 -0
- package/src/hooks/useFormItem.ts +33 -0
- package/src/hooks/useFormItemParentName.ts +49 -0
- package/src/hooks/useFormList.ts +33 -0
- package/src/hooks/useHtmlFor.ts +9 -0
- package/src/hooks/useMultipleForm.ts +30 -0
- package/src/hooks/useRule.ts +16 -0
- package/src/index.ts +4 -0
- package/src/instance/formInstance.ts +220 -0
- package/src/instance/formItemBaseInstance.ts +23 -0
- package/src/instance/formItemInstance.ts +23 -0
- package/src/instance/formListInstance.ts +108 -0
- package/src/instance/index.ts +5 -0
- package/src/instance/multipleInstance.ts +109 -0
- package/src/instance/ruleIntsnace.ts +89 -0
- package/src/interface/index.ts +38 -0
- package/src/utils/cloneByNamePathList.ts +13 -0
- package/src/utils/get.ts +53 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/interface.ts +4 -0
- package/src/utils/set.ts +85 -0
- package/src/utils/utils.ts +504 -0
package/README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { FormInstanceBase } from '../../instance/formInstance';
|
|
2
|
+
import { FormItemInstanceBase } from '../../instance/formItemInstance';
|
|
3
|
+
import { RegisterFormItemOptions } from './../register/register.FormItem';
|
|
4
|
+
import { Ref } from 'vue';
|
|
5
|
+
import type { RuleInstanceBase } from '../../instance/ruleIntsnace';
|
|
6
|
+
export interface FormItemAttrOptions extends RegisterFormItemOptions {
|
|
7
|
+
/**依赖更新项*/
|
|
8
|
+
dependencies?: string[];
|
|
9
|
+
/**通知 只用于校验规则提示 字段 */
|
|
10
|
+
noticeOnlyRuleDataField?: string[];
|
|
11
|
+
/**是否保护值(不进行表单项组件卸载重置初始值)*/
|
|
12
|
+
preserve?: boolean;
|
|
13
|
+
/**重写规则*/
|
|
14
|
+
useRules?: (ruleInstance: Ref<RuleInstanceBase>, form: Ref<FormInstanceBase>, formItemInstance: Ref<FormItemInstanceBase>) => void;
|
|
15
|
+
/**输入框属性重写*/
|
|
16
|
+
useAttrs?: (attrs: any, form: Ref<FormInstanceBase>, formItemInstance: Ref<FormItemInstanceBase>) => any;
|
|
17
|
+
/**输入框属性*/
|
|
18
|
+
inputAttrs?: Object;
|
|
19
|
+
/**传递组件字段*/
|
|
20
|
+
valuePropName?: string;
|
|
21
|
+
/**取值字段(默认和valuePropName值相同)*/
|
|
22
|
+
getValuePath?: string;
|
|
23
|
+
/**自定义获取值*/
|
|
24
|
+
getValueFromEvent?: (event: any, form: Ref<FormInstanceBase>, formItemInstance: Ref<FormItemInstanceBase>) => any;
|
|
25
|
+
/**值格式化*/
|
|
26
|
+
formatValue?: (value: any, form: Ref<FormInstanceBase>, formItemInstance: Ref<FormItemInstanceBase>, event: any) => any;
|
|
27
|
+
/**触发数据更新之后触发(用于数据联动之类的)*/
|
|
28
|
+
onAfterUpdate?: (value: any, form: Ref<FormInstanceBase>, formItemInstance: Ref<FormItemInstanceBase>, event: any) => void;
|
|
29
|
+
/**事件名称*/
|
|
30
|
+
trigger?: string;
|
|
31
|
+
}
|
|
32
|
+
/**表单项参数*/
|
|
33
|
+
export declare const useFormItemAttr: (options: FormItemAttrOptions) => {
|
|
34
|
+
valuePropName: string;
|
|
35
|
+
htmlFor: import("vue").ComputedRef<string>;
|
|
36
|
+
onChange: (event: any) => void;
|
|
37
|
+
formItemInstance: Ref<FormItemInstanceBase, FormItemInstanceBase>;
|
|
38
|
+
form: Ref<FormInstanceBase<any>, FormInstanceBase<any>>;
|
|
39
|
+
ruleInstance: Ref<RuleInstanceBase, RuleInstanceBase>;
|
|
40
|
+
newAttrs: import("vue").ComputedRef<{
|
|
41
|
+
name: string;
|
|
42
|
+
id: string;
|
|
43
|
+
constructor: Function;
|
|
44
|
+
toString(): string;
|
|
45
|
+
toLocaleString(): string;
|
|
46
|
+
valueOf(): Object;
|
|
47
|
+
hasOwnProperty(v: PropertyKey): boolean;
|
|
48
|
+
isPrototypeOf(v: Object): boolean;
|
|
49
|
+
propertyIsEnumerable(v: PropertyKey): boolean;
|
|
50
|
+
}>;
|
|
51
|
+
attrsLastData: Ref<{
|
|
52
|
+
name: string;
|
|
53
|
+
id: string;
|
|
54
|
+
constructor: Function;
|
|
55
|
+
toString: () => string;
|
|
56
|
+
toLocaleString: () => string;
|
|
57
|
+
valueOf: () => Object;
|
|
58
|
+
hasOwnProperty: (v: PropertyKey) => boolean;
|
|
59
|
+
isPrototypeOf: (v: Object) => boolean;
|
|
60
|
+
propertyIsEnumerable: (v: PropertyKey) => boolean;
|
|
61
|
+
}, {
|
|
62
|
+
name: string;
|
|
63
|
+
id: string;
|
|
64
|
+
constructor: Function;
|
|
65
|
+
toString(): string;
|
|
66
|
+
toLocaleString(): string;
|
|
67
|
+
valueOf(): Object;
|
|
68
|
+
hasOwnProperty(v: PropertyKey): boolean;
|
|
69
|
+
isPrototypeOf(v: Object): boolean;
|
|
70
|
+
propertyIsEnumerable(v: PropertyKey): boolean;
|
|
71
|
+
} | {
|
|
72
|
+
name: string;
|
|
73
|
+
id: string;
|
|
74
|
+
constructor: Function;
|
|
75
|
+
toString: () => string;
|
|
76
|
+
toLocaleString: () => string;
|
|
77
|
+
valueOf: () => Object;
|
|
78
|
+
hasOwnProperty: (v: PropertyKey) => boolean;
|
|
79
|
+
isPrototypeOf: (v: Object) => boolean;
|
|
80
|
+
propertyIsEnumerable: (v: PropertyKey) => boolean;
|
|
81
|
+
}>;
|
|
82
|
+
validateResult: Ref<{
|
|
83
|
+
tip: string | (string | undefined)[];
|
|
84
|
+
isInvalid: boolean;
|
|
85
|
+
}, {
|
|
86
|
+
tip: string | (string | undefined)[];
|
|
87
|
+
isInvalid: boolean;
|
|
88
|
+
} | {
|
|
89
|
+
tip: string | (string | undefined)[];
|
|
90
|
+
isInvalid: boolean;
|
|
91
|
+
}>;
|
|
92
|
+
newSort: import("vue").ComputedRef<string>;
|
|
93
|
+
newName: import("vue").ComputedRef<string>;
|
|
94
|
+
};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { get } from "../../utils/index.js";
|
|
2
|
+
import { useHtmlFor } from "../useHtmlFor.js";
|
|
3
|
+
import { useRegisterFormItem } from "../register/register.FormItem.js";
|
|
4
|
+
import { computed, ref, toValue, watch } from "vue";
|
|
5
|
+
const useFormItemAttr = (options)=>{
|
|
6
|
+
const { trigger = 'onChange', preserve, valuePropName = 'value', getValuePath = valuePropName, getValueFromEvent, formatValue, onAfterUpdate, useAttrs, useRules, noticeOnlyRuleDataField } = options;
|
|
7
|
+
const { formItemInstance, form, ruleInstance, newName, newSort } = useRegisterFormItem(options);
|
|
8
|
+
formItemInstance.value.noticeOnlyRuleDataField = noticeOnlyRuleDataField;
|
|
9
|
+
formItemInstance.value.onAfterUpdate = onAfterUpdate;
|
|
10
|
+
formItemInstance.value.preserve = preserve;
|
|
11
|
+
const oldValue = ref(get(toValue(form.value.formData), toValue(newName)));
|
|
12
|
+
const oldInputAttrs = ref(options.inputAttrs || {});
|
|
13
|
+
watch(()=>options.inputAttrs, (newVal)=>{
|
|
14
|
+
oldInputAttrs.value = newVal || {};
|
|
15
|
+
});
|
|
16
|
+
watch(()=>[
|
|
17
|
+
get(toValue(form.value.formData), toValue(newName)),
|
|
18
|
+
toValue(newName)
|
|
19
|
+
], ()=>{
|
|
20
|
+
oldValue.value = form.value.getFieldValue(toValue(newName));
|
|
21
|
+
}, {
|
|
22
|
+
immediate: true
|
|
23
|
+
});
|
|
24
|
+
watch(()=>[
|
|
25
|
+
toValue(formItemInstance).noticeOnlyRuleDataField,
|
|
26
|
+
toValue(oldValue)
|
|
27
|
+
], ()=>{
|
|
28
|
+
const formInstance = toValue(form);
|
|
29
|
+
const newVal = toValue(formItemInstance).noticeOnlyRuleDataField;
|
|
30
|
+
if (Array.isArray(newVal) && newVal.length) formInstance.onlyValidate(newVal);
|
|
31
|
+
}, {
|
|
32
|
+
immediate: true
|
|
33
|
+
});
|
|
34
|
+
const onValueChange = (event)=>{
|
|
35
|
+
try {
|
|
36
|
+
const formItem = toValue(formItemInstance);
|
|
37
|
+
const formInstance = toValue(form);
|
|
38
|
+
let value = event;
|
|
39
|
+
if ('function' == typeof getValueFromEvent) value = getValueFromEvent(event, form, formItemInstance);
|
|
40
|
+
else if (event && event.target && 'object' == typeof event.target && getValuePath in event.target) value = get(event.target, getValuePath);
|
|
41
|
+
if ('function' == typeof formatValue) value = formatValue(value, form, formItemInstance, event);
|
|
42
|
+
if (oldValue.value !== value) {
|
|
43
|
+
formInstance.updatedFieldValue(toValue(newName), value, 'validate');
|
|
44
|
+
formItem.onAfterUpdate?.(value, form, formItemInstance, event);
|
|
45
|
+
}
|
|
46
|
+
} catch (error) {
|
|
47
|
+
console.log(error);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const htmlFor = useHtmlFor(newName);
|
|
51
|
+
watch(()=>toValue(htmlFor), ()=>{
|
|
52
|
+
formItemInstance.value.htmlFor = htmlFor.value;
|
|
53
|
+
}, {
|
|
54
|
+
immediate: true
|
|
55
|
+
});
|
|
56
|
+
const newAttrs = computed(()=>{
|
|
57
|
+
const _attr = oldInputAttrs.value || {};
|
|
58
|
+
return {
|
|
59
|
+
..._attr,
|
|
60
|
+
[trigger]: onValueChange,
|
|
61
|
+
name: toValue(newName),
|
|
62
|
+
id: toValue(htmlFor),
|
|
63
|
+
[valuePropName]: toValue(oldValue)
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
const attrsLastData = ref(toValue(newAttrs));
|
|
67
|
+
watch(()=>[
|
|
68
|
+
toValue(newAttrs),
|
|
69
|
+
toValue(oldValue)
|
|
70
|
+
], ()=>{
|
|
71
|
+
attrsLastData.value = useAttrs?.(toValue(newAttrs), form, formItemInstance) || toValue(newAttrs);
|
|
72
|
+
formItemInstance.value.control = attrsLastData;
|
|
73
|
+
}, {
|
|
74
|
+
immediate: true
|
|
75
|
+
});
|
|
76
|
+
const validateResult = ref(toValue(ruleInstance).getValidateResult());
|
|
77
|
+
watch(()=>[
|
|
78
|
+
toValue(oldValue),
|
|
79
|
+
toValue(toValue(ruleInstance).rules)
|
|
80
|
+
], ()=>{
|
|
81
|
+
useRules?.(ruleInstance, form, formItemInstance);
|
|
82
|
+
ruleInstance.value.validate();
|
|
83
|
+
});
|
|
84
|
+
watch(()=>[
|
|
85
|
+
toValue(toValue(ruleInstance).messages),
|
|
86
|
+
toValue(toValue(ruleInstance).rules)
|
|
87
|
+
], ()=>{
|
|
88
|
+
validateResult.value = toValue(ruleInstance).getValidateResult();
|
|
89
|
+
});
|
|
90
|
+
return {
|
|
91
|
+
valuePropName,
|
|
92
|
+
htmlFor,
|
|
93
|
+
onChange: onValueChange,
|
|
94
|
+
formItemInstance,
|
|
95
|
+
form,
|
|
96
|
+
ruleInstance,
|
|
97
|
+
newAttrs,
|
|
98
|
+
attrsLastData,
|
|
99
|
+
validateResult,
|
|
100
|
+
newSort,
|
|
101
|
+
newName
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
export { useFormItemAttr };
|
|
@@ -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,13 @@
|
|
|
1
|
+
export * from "./useAttrs.js";
|
|
2
|
+
export * from "./useForm.js";
|
|
3
|
+
export * from "./useFormItem.js";
|
|
4
|
+
export * from "./useFormList.js";
|
|
5
|
+
export * from "./useHtmlFor.js";
|
|
6
|
+
export * from "./useMultipleForm.js";
|
|
7
|
+
export * from "./useFormItemParentName.js";
|
|
8
|
+
export * from "./useEffect.js";
|
|
9
|
+
export * from "./register/register.FormHideItem.js";
|
|
10
|
+
export * from "./register/register.FormItem.js";
|
|
11
|
+
export * from "./register/register.FormList.js";
|
|
12
|
+
export * from "./register/register.form.js";
|
|
13
|
+
export * from "./attr/attr.FormItem.js";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { RegisterFormItemOptions } from './register.FormItem';
|
|
2
|
+
interface RegisterFormHideItemOptions extends Omit<RegisterFormItemOptions, 'rules'> {
|
|
3
|
+
}
|
|
4
|
+
/**注册表单隐藏表单项到表单实例中*/
|
|
5
|
+
export declare const useRegisterFormHideItem: (options: RegisterFormHideItemOptions) => {
|
|
6
|
+
form: import("vue").Ref<import("../..").FormInstanceBase<any>, import("../..").FormInstanceBase<any>>;
|
|
7
|
+
isHide: import("vue").Ref<any, any>;
|
|
8
|
+
};
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ref, toValue, watch } from "vue";
|
|
2
|
+
import { useFormItemParentNameInject } from "../useFormItemParentName.js";
|
|
3
|
+
import { useFormInject } from "../useForm.js";
|
|
4
|
+
const useRegisterFormHideItem = (options)=>{
|
|
5
|
+
const { name, sort, isJoinParentField = true } = options;
|
|
6
|
+
const form = useFormInject();
|
|
7
|
+
const { newName } = useFormItemParentNameInject({
|
|
8
|
+
name,
|
|
9
|
+
sort,
|
|
10
|
+
isJoinParentField
|
|
11
|
+
});
|
|
12
|
+
const isHide = ref(form.value.getFieldHideValue(newName.value));
|
|
13
|
+
watch(()=>[
|
|
14
|
+
toValue(form),
|
|
15
|
+
toValue(newName)
|
|
16
|
+
], ()=>{
|
|
17
|
+
isHide.value = form.value.getFieldHideValue(toValue(newName));
|
|
18
|
+
}, {
|
|
19
|
+
immediate: true
|
|
20
|
+
});
|
|
21
|
+
return {
|
|
22
|
+
form,
|
|
23
|
+
isHide
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
export { useRegisterFormHideItem };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 注册组件
|
|
3
|
+
*/
|
|
4
|
+
import type { RuleItem } from 'async-validator';
|
|
5
|
+
export interface RegisterFormItemOptions {
|
|
6
|
+
/**字段*/
|
|
7
|
+
name: string;
|
|
8
|
+
/**规则*/
|
|
9
|
+
rules?: RuleItem[];
|
|
10
|
+
/**排序值*/
|
|
11
|
+
sort?: string;
|
|
12
|
+
/**是否拼接父级字段*/
|
|
13
|
+
isJoinParentField?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/**注册表单项到表单实例中*/
|
|
16
|
+
export declare const useRegisterFormItem: (options: RegisterFormItemOptions) => {
|
|
17
|
+
ruleInstance: import("vue").Ref<import("../..").RuleInstanceBase, import("../..").RuleInstanceBase>;
|
|
18
|
+
formItemInstance: import("vue").Ref<import("../..").FormItemInstanceBase, import("../..").FormItemInstanceBase>;
|
|
19
|
+
form: import("vue").Ref<import("../..").FormInstanceBase<any>, import("../..").FormInstanceBase<any>>;
|
|
20
|
+
newName: import("vue").ComputedRef<string>;
|
|
21
|
+
newSort: import("vue").ComputedRef<string>;
|
|
22
|
+
parentItem: import("vue").ComputedRef<import("../..").PartialComputedRefs<Omit<import("../useFormItemParentName").FormItemParentNamOptions, "isJoinParentField">>>;
|
|
23
|
+
deepRefData: import("vue").Ref<any, any>;
|
|
24
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { ref, toValue, watch } from "vue";
|
|
2
|
+
import { useFormInject } from "../useForm.js";
|
|
3
|
+
import { useFormItem } from "../useFormItem.js";
|
|
4
|
+
import { useFormItemParentNameInject } from "../useFormItemParentName.js";
|
|
5
|
+
import { useEffect } from "../useEffect.js";
|
|
6
|
+
import { useRules } from "../useRule.js";
|
|
7
|
+
const useRegisterFormItem = (options)=>{
|
|
8
|
+
const { name, rules, sort, isJoinParentField = true } = options;
|
|
9
|
+
const form = useFormInject();
|
|
10
|
+
const deepRefData = ref({});
|
|
11
|
+
const { newName, newSort, parentItem } = useFormItemParentNameInject({
|
|
12
|
+
name,
|
|
13
|
+
sort,
|
|
14
|
+
isJoinParentField
|
|
15
|
+
});
|
|
16
|
+
const formItemInstance = useFormItem();
|
|
17
|
+
const ruleInstance = useRules();
|
|
18
|
+
const ruleItems = ref(rules || []);
|
|
19
|
+
watch(()=>toValue(options.rules), (newVal)=>{
|
|
20
|
+
ruleItems.value = newVal || [];
|
|
21
|
+
});
|
|
22
|
+
watch(()=>[
|
|
23
|
+
toValue(form)
|
|
24
|
+
], ()=>{
|
|
25
|
+
ruleInstance.value.instance = form;
|
|
26
|
+
formItemInstance.value.instance = form;
|
|
27
|
+
}, {
|
|
28
|
+
immediate: true
|
|
29
|
+
});
|
|
30
|
+
watch(()=>[
|
|
31
|
+
toValue(newName),
|
|
32
|
+
toValue(ruleInstance)
|
|
33
|
+
], ()=>{
|
|
34
|
+
ruleInstance.value.name = toValue(newName);
|
|
35
|
+
ruleInstance.value.rules = ruleItems;
|
|
36
|
+
formItemInstance.value.name = toValue(newName);
|
|
37
|
+
formItemInstance.value.rule = ruleInstance;
|
|
38
|
+
}, {
|
|
39
|
+
immediate: true
|
|
40
|
+
});
|
|
41
|
+
watch(()=>[
|
|
42
|
+
toValue(newSort)
|
|
43
|
+
], ()=>{
|
|
44
|
+
ruleInstance.value.sort = toValue(newSort);
|
|
45
|
+
formItemInstance.value.sort = toValue(newSort);
|
|
46
|
+
}, {
|
|
47
|
+
immediate: true
|
|
48
|
+
});
|
|
49
|
+
watch(()=>[
|
|
50
|
+
toValue(parentItem)
|
|
51
|
+
], ()=>{
|
|
52
|
+
formItemInstance.value.parentDataField = toValue(toValue(parentItem).name);
|
|
53
|
+
}, {
|
|
54
|
+
immediate: true
|
|
55
|
+
});
|
|
56
|
+
useEffect(()=>form.value.registerFormItem(formItemInstance));
|
|
57
|
+
return {
|
|
58
|
+
ruleInstance,
|
|
59
|
+
formItemInstance,
|
|
60
|
+
form,
|
|
61
|
+
newName,
|
|
62
|
+
newSort,
|
|
63
|
+
parentItem,
|
|
64
|
+
deepRefData
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
export { useRegisterFormItem };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { RegisterFormItemOptions } from './register.FormItem';
|
|
2
|
+
export interface RegisterFormListOptions extends RegisterFormItemOptions {
|
|
3
|
+
}
|
|
4
|
+
/**注册表单List到表单实例中*/
|
|
5
|
+
export declare const useRegisterFormList: (options: RegisterFormListOptions) => {
|
|
6
|
+
ruleInstance: import("vue").Ref<import("../..").RuleInstanceBase, import("../..").RuleInstanceBase>;
|
|
7
|
+
formItemInstance: import("vue").Ref<import("../..").FormItemInstanceBase, import("../..").FormItemInstanceBase>;
|
|
8
|
+
formListInstance: import("vue").Ref<import("../..").FormListInstanceBase, import("../..").FormListInstanceBase>;
|
|
9
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { toValue, watch } from "vue";
|
|
2
|
+
import { useRegisterFormItem } from "./register.FormItem.js";
|
|
3
|
+
import { useFormList } from "../useFormList.js";
|
|
4
|
+
import { useEffect } from "../useEffect.js";
|
|
5
|
+
const useRegisterFormList = (options)=>{
|
|
6
|
+
const { ruleInstance, formItemInstance, form, newName, parentItem } = useRegisterFormItem(options);
|
|
7
|
+
const formListInstance = useFormList();
|
|
8
|
+
watch(()=>[
|
|
9
|
+
toValue(newName)
|
|
10
|
+
], ()=>{
|
|
11
|
+
formListInstance.value.ctor(toValue(newName));
|
|
12
|
+
}, {
|
|
13
|
+
immediate: true
|
|
14
|
+
});
|
|
15
|
+
watch(()=>[
|
|
16
|
+
toValue(form),
|
|
17
|
+
formItemInstance,
|
|
18
|
+
ruleInstance
|
|
19
|
+
], ()=>{
|
|
20
|
+
formListInstance.value.instance = form;
|
|
21
|
+
formListInstance.value.formItemInstance = formItemInstance;
|
|
22
|
+
formListInstance.value.rule = ruleInstance;
|
|
23
|
+
}, {
|
|
24
|
+
immediate: true
|
|
25
|
+
});
|
|
26
|
+
watch(()=>[
|
|
27
|
+
options.sort,
|
|
28
|
+
toValue(parentItem)
|
|
29
|
+
], ()=>{
|
|
30
|
+
formListInstance.value.sort = toValue(parentItem.value.sort);
|
|
31
|
+
formListInstance.value.parentDataField = toValue(parentItem.value.name);
|
|
32
|
+
}, {
|
|
33
|
+
immediate: true
|
|
34
|
+
});
|
|
35
|
+
useEffect(()=>form.value.registerFormList(newName.value, formListInstance));
|
|
36
|
+
return {
|
|
37
|
+
ruleInstance,
|
|
38
|
+
formItemInstance,
|
|
39
|
+
formListInstance
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
export { useRegisterFormList };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { FormInstanceBase } from '../../instance/formInstance';
|
|
2
|
+
import { Ref } from 'vue';
|
|
3
|
+
/**注册表单实例到多表单收集实例中*/
|
|
4
|
+
export declare const useRegisterForm: (form: Ref<FormInstanceBase>, name?: string) => Ref<import("../..").MultipleInstanceBase, import("../..").MultipleInstanceBase>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { useMultipleFormInject } from "../useMultipleForm.js";
|
|
2
|
+
import { useEffect } from "../useEffect.js";
|
|
3
|
+
const useRegisterForm = (form, name)=>{
|
|
4
|
+
const multipleForm = useMultipleFormInject();
|
|
5
|
+
useEffect(()=>{
|
|
6
|
+
if (name) return multipleForm.value.ctor(name, form);
|
|
7
|
+
});
|
|
8
|
+
return multipleForm;
|
|
9
|
+
};
|
|
10
|
+
export { useRegisterForm };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**公共属性*/
|
|
2
|
+
import { StyleValue, ComputedRef } from 'vue';
|
|
3
|
+
import { ComputedRefBase } from '../interface';
|
|
4
|
+
export type AttrsOptions = {
|
|
5
|
+
/**列数据*/
|
|
6
|
+
colCount?: ComputedRefBase<number | undefined>;
|
|
7
|
+
/**规则校验失败错误提示位置*/
|
|
8
|
+
errorLayout?: ComputedRefBase<'left-bottom' | 'right-bottom' | 'top-right' | 'top-left' | undefined>;
|
|
9
|
+
/**label显示模式*/
|
|
10
|
+
labelMode?: ComputedRefBase<'left' | 'top' | 'hide' | undefined>;
|
|
11
|
+
/**是否显示label后的冒号*/
|
|
12
|
+
showColon?: ComputedRefBase<boolean | undefined>;
|
|
13
|
+
/**表单项 className*/
|
|
14
|
+
formItemClass?: ComputedRefBase<string | undefined>;
|
|
15
|
+
/**表单项 style*/
|
|
16
|
+
formItemStyle?: ComputedRefBase<StyleValue | undefined>;
|
|
17
|
+
/**表单项 label className*/
|
|
18
|
+
formItemLabelClass?: ComputedRefBase<string | undefined>;
|
|
19
|
+
/**表单项 label style*/
|
|
20
|
+
formItemLabelStyle?: ComputedRefBase<StyleValue | undefined>;
|
|
21
|
+
};
|
|
22
|
+
/**公共属性 Context */
|
|
23
|
+
export declare function useAttrsProvide(options: AttrsOptions): void;
|
|
24
|
+
/**子项中获取公共属性*/
|
|
25
|
+
export declare function useAttrsInject(): ComputedRef<AttrsOptions>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { computed, inject, provide, reactive, ref } from "vue";
|
|
2
|
+
const attrsProvideSymbol = Symbol('carefrees-attrs');
|
|
3
|
+
function useAttrsProvide(options) {
|
|
4
|
+
const { colCount = ref(4), errorLayout = ref('left-bottom'), labelMode = ref('top'), showColon = ref(true), formItemClass, formItemStyle, formItemLabelClass, formItemLabelStyle } = options;
|
|
5
|
+
const data = computed(()=>({
|
|
6
|
+
colCount,
|
|
7
|
+
errorLayout,
|
|
8
|
+
labelMode,
|
|
9
|
+
showColon,
|
|
10
|
+
formItemClass,
|
|
11
|
+
formItemStyle,
|
|
12
|
+
formItemLabelClass,
|
|
13
|
+
formItemLabelStyle
|
|
14
|
+
}));
|
|
15
|
+
provide(attrsProvideSymbol, reactive(data));
|
|
16
|
+
}
|
|
17
|
+
function useAttrsInject() {
|
|
18
|
+
const attrs = inject(attrsProvideSymbol, computed(()=>({
|
|
19
|
+
colCount: ref(4),
|
|
20
|
+
errorLayout: ref('left-bottom'),
|
|
21
|
+
labelMode: ref('top'),
|
|
22
|
+
showColon: ref(true)
|
|
23
|
+
})));
|
|
24
|
+
return attrs;
|
|
25
|
+
}
|
|
26
|
+
export { useAttrsInject, useAttrsProvide };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { onMounted, onUnmounted, ref } from "vue";
|
|
2
|
+
const useEffect = (fun)=>{
|
|
3
|
+
const unRegisterRef = ref();
|
|
4
|
+
onMounted(()=>{
|
|
5
|
+
unRegisterRef.value = fun();
|
|
6
|
+
});
|
|
7
|
+
onUnmounted(()=>{
|
|
8
|
+
'function' == typeof unRegisterRef.value && unRegisterRef.value();
|
|
9
|
+
unRegisterRef.value = void 0;
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
export { useEffect };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { FormInstanceBase } from '../instance/formInstance';
|
|
2
|
+
import { Ref } from 'vue';
|
|
3
|
+
/**表单实例 Context */
|
|
4
|
+
export declare function useFormProvide<T = any>(form: Ref<FormInstanceBase<T>> | FormInstanceBase<T>): void;
|
|
5
|
+
/**子项中获取表单实例*/
|
|
6
|
+
export declare function useFormInject<T = any>(): Ref<FormInstanceBase<T>, FormInstanceBase<T>>;
|
|
7
|
+
/**初始化表单实例*/
|
|
8
|
+
export declare function useForm<T = any>(form?: Ref<FormInstanceBase<T>> | FormInstanceBase<T>): Ref<FormInstanceBase<T>>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { FormInstanceBase } from "../instance/formInstance.js";
|
|
2
|
+
import { inject, provide, ref, toValue } from "vue";
|
|
3
|
+
const formProvideSymbol = Symbol('carefrees-form');
|
|
4
|
+
function useFormProvide(form) {
|
|
5
|
+
const newForm = useForm(form);
|
|
6
|
+
provide(formProvideSymbol, newForm);
|
|
7
|
+
}
|
|
8
|
+
function useFormInject() {
|
|
9
|
+
const form = inject(formProvideSymbol, ref(new FormInstanceBase()));
|
|
10
|
+
return form;
|
|
11
|
+
}
|
|
12
|
+
function useForm(form) {
|
|
13
|
+
const refForm = ref();
|
|
14
|
+
if (!refForm.value) {
|
|
15
|
+
const value = toValue(form);
|
|
16
|
+
if (value) refForm.value = value;
|
|
17
|
+
else refForm.value = new FormInstanceBase();
|
|
18
|
+
}
|
|
19
|
+
return refForm;
|
|
20
|
+
}
|
|
21
|
+
export { useForm, useFormInject, useFormProvide };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { FormItemInstanceBase } from '../instance/formItemInstance';
|
|
2
|
+
import { Ref } from 'vue';
|
|
3
|
+
/**表单项实例 Context */
|
|
4
|
+
export declare function useFormItemProvide(formItem: Ref<FormItemInstanceBase> | FormItemInstanceBase): void;
|
|
5
|
+
/**子项中获取表单项实例*/
|
|
6
|
+
export declare function useFormItemInject(): Ref<FormItemInstanceBase, FormItemInstanceBase>;
|
|
7
|
+
/** 初始化 表单项实例*/
|
|
8
|
+
export declare function useFormItem(formItem?: Ref<FormItemInstanceBase> | FormItemInstanceBase): Ref<FormItemInstanceBase>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { FormItemInstanceBase } from "../instance/formItemInstance.js";
|
|
2
|
+
import { inject, provide, ref, toValue } from "vue";
|
|
3
|
+
const formItemProvideSymbol = Symbol('carefrees-form-item');
|
|
4
|
+
function useFormItemProvide(formItem) {
|
|
5
|
+
const newFormItem = useFormItem(formItem);
|
|
6
|
+
provide(formItemProvideSymbol, newFormItem);
|
|
7
|
+
}
|
|
8
|
+
function useFormItemInject() {
|
|
9
|
+
const formItem = inject(formItemProvideSymbol, ref(new FormItemInstanceBase()));
|
|
10
|
+
return formItem;
|
|
11
|
+
}
|
|
12
|
+
function useFormItem(formItem) {
|
|
13
|
+
const refForm = ref();
|
|
14
|
+
if (!refForm.value) {
|
|
15
|
+
const value = toValue(formItem);
|
|
16
|
+
if (value) refForm.value = value;
|
|
17
|
+
else refForm.value = new FormItemInstanceBase();
|
|
18
|
+
}
|
|
19
|
+
return refForm;
|
|
20
|
+
}
|
|
21
|
+
export { useFormItem, useFormItemInject, useFormItemProvide };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ComputedRef } from 'vue';
|
|
2
|
+
import { PartialComputedRefs } from '../interface';
|
|
3
|
+
export interface FormItemParentNamOptions {
|
|
4
|
+
/**字段*/
|
|
5
|
+
name: string;
|
|
6
|
+
/**排序*/
|
|
7
|
+
sort?: string;
|
|
8
|
+
/**是否拼接父级字段*/
|
|
9
|
+
isJoinParentField?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export type FormItemParentNameProviderProps = PartialComputedRefs<Omit<FormItemParentNamOptions, 'isJoinParentField'>>;
|
|
12
|
+
export declare const useFormItemParentNameProvide: (props: FormItemParentNameProviderProps) => void;
|
|
13
|
+
/**表单项获取父级字段*/
|
|
14
|
+
export declare const useFormItemParentNameInject: (options: FormItemParentNamOptions) => {
|
|
15
|
+
newName: ComputedRef<string>;
|
|
16
|
+
newSort: ComputedRef<string>;
|
|
17
|
+
parentItem: ComputedRef<PartialComputedRefs<Omit<FormItemParentNamOptions, "isJoinParentField">>>;
|
|
18
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { computed, inject, provide, ref, toValue } from "vue";
|
|
2
|
+
const parentNameProvideSymbol = Symbol('carefrees-parent-name');
|
|
3
|
+
const useFormItemParentNameProvide = (props)=>{
|
|
4
|
+
const { name, sort } = props;
|
|
5
|
+
provide(parentNameProvideSymbol, computed(()=>({
|
|
6
|
+
name,
|
|
7
|
+
sort
|
|
8
|
+
})));
|
|
9
|
+
};
|
|
10
|
+
const useFormItemParentNameInject = (options)=>{
|
|
11
|
+
const { isJoinParentField = true, sort, name } = options;
|
|
12
|
+
const parentItem = inject(parentNameProvideSymbol, computed(()=>({
|
|
13
|
+
name: ref(''),
|
|
14
|
+
sort: ref('')
|
|
15
|
+
})));
|
|
16
|
+
const newName = computed(()=>{
|
|
17
|
+
const _name = toValue(parentItem.value.name);
|
|
18
|
+
if (_name && isJoinParentField) {
|
|
19
|
+
if (/^\./.test(`${name}`)) ;
|
|
20
|
+
else if (name) return [
|
|
21
|
+
_name,
|
|
22
|
+
'.',
|
|
23
|
+
name
|
|
24
|
+
].filter(Boolean).join('');
|
|
25
|
+
return [
|
|
26
|
+
_name,
|
|
27
|
+
name
|
|
28
|
+
].filter(Boolean).join('');
|
|
29
|
+
}
|
|
30
|
+
return [
|
|
31
|
+
name
|
|
32
|
+
].filter(Boolean).join('');
|
|
33
|
+
});
|
|
34
|
+
const newSort = computed(()=>{
|
|
35
|
+
const _sort = toValue(parentItem.value.sort);
|
|
36
|
+
return [
|
|
37
|
+
isJoinParentField ? _sort : '',
|
|
38
|
+
sort
|
|
39
|
+
].filter(Boolean).join('-');
|
|
40
|
+
});
|
|
41
|
+
return {
|
|
42
|
+
newName,
|
|
43
|
+
newSort,
|
|
44
|
+
parentItem
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
export { useFormItemParentNameInject, useFormItemParentNameProvide };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { FormListInstanceBase } from '../instance/formListInstance';
|
|
2
|
+
import { Ref } from 'vue';
|
|
3
|
+
/**表单List实例 Context */
|
|
4
|
+
export declare function useFormListProvide(formList: Ref<FormListInstanceBase> | FormListInstanceBase): void;
|
|
5
|
+
/**子项中获取表单List实例*/
|
|
6
|
+
export declare function useFormListInject(): Ref<FormListInstanceBase, FormListInstanceBase>;
|
|
7
|
+
/**初始化 表单List实例*/
|
|
8
|
+
export declare function useFormList(formList?: Ref<FormListInstanceBase> | FormListInstanceBase): Ref<FormListInstanceBase>;
|