@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.
Files changed (152) hide show
  1. package/README.md +7 -0
  2. package/esm/hooks/attr/attr.FormItem.d.ts +94 -0
  3. package/esm/hooks/attr/attr.FormItem.js +104 -0
  4. package/esm/hooks/index.d.ts +13 -0
  5. package/esm/hooks/index.js +13 -0
  6. package/esm/hooks/register/register.FormHideItem.d.ts +9 -0
  7. package/esm/hooks/register/register.FormHideItem.js +26 -0
  8. package/esm/hooks/register/register.FormItem.d.ts +24 -0
  9. package/esm/hooks/register/register.FormItem.js +67 -0
  10. package/esm/hooks/register/register.FormList.d.ts +9 -0
  11. package/esm/hooks/register/register.FormList.js +42 -0
  12. package/esm/hooks/register/register.form.d.ts +4 -0
  13. package/esm/hooks/register/register.form.js +10 -0
  14. package/esm/hooks/useAttrs.d.ts +25 -0
  15. package/esm/hooks/useAttrs.js +26 -0
  16. package/esm/hooks/useEffect.d.ts +2 -0
  17. package/esm/hooks/useEffect.js +12 -0
  18. package/esm/hooks/useForm.d.ts +8 -0
  19. package/esm/hooks/useForm.js +21 -0
  20. package/esm/hooks/useFormItem.d.ts +8 -0
  21. package/esm/hooks/useFormItem.js +21 -0
  22. package/esm/hooks/useFormItemParentName.d.ts +18 -0
  23. package/esm/hooks/useFormItemParentName.js +47 -0
  24. package/esm/hooks/useFormList.d.ts +8 -0
  25. package/esm/hooks/useFormList.js +21 -0
  26. package/esm/hooks/useHtmlFor.d.ts +2 -0
  27. package/esm/hooks/useHtmlFor.js +7 -0
  28. package/esm/hooks/useMultipleForm.d.ts +8 -0
  29. package/esm/hooks/useMultipleForm.js +21 -0
  30. package/esm/hooks/useRule.d.ts +4 -0
  31. package/esm/hooks/useRule.js +12 -0
  32. package/esm/index.d.ts +4 -0
  33. package/esm/index.js +4 -0
  34. package/esm/instance/formInstance.d.ts +58 -0
  35. package/esm/instance/formInstance.js +132 -0
  36. package/esm/instance/formItemBaseInstance.d.ts +23 -0
  37. package/esm/instance/formItemBaseInstance.js +6 -0
  38. package/esm/instance/formItemInstance.d.ts +18 -0
  39. package/esm/instance/formItemInstance.js +11 -0
  40. package/esm/instance/formListInstance.d.ts +43 -0
  41. package/esm/instance/formListInstance.js +95 -0
  42. package/esm/instance/index.d.ts +5 -0
  43. package/esm/instance/index.js +5 -0
  44. package/esm/instance/multipleInstance.d.ts +32 -0
  45. package/esm/instance/multipleInstance.js +77 -0
  46. package/esm/instance/ruleIntsnace.d.ts +45 -0
  47. package/esm/instance/ruleIntsnace.js +52 -0
  48. package/esm/interface/index.d.ts +31 -0
  49. package/esm/interface/index.js +0 -0
  50. package/esm/utils/cloneByNamePathList.d.ts +3 -0
  51. package/esm/utils/cloneByNamePathList.js +11 -0
  52. package/esm/utils/get.d.ts +26 -0
  53. package/esm/utils/get.js +13 -0
  54. package/esm/utils/index.d.ts +4 -0
  55. package/esm/utils/index.js +4 -0
  56. package/esm/utils/interface.d.ts +3 -0
  57. package/esm/utils/interface.js +0 -0
  58. package/esm/utils/set.d.ts +32 -0
  59. package/esm/utils/set.js +29 -0
  60. package/esm/utils/utils.d.ts +172 -0
  61. package/esm/utils/utils.js +131 -0
  62. package/lib/hooks/attr/attr.FormItem.d.ts +94 -0
  63. package/lib/hooks/attr/attr.FormItem.js +138 -0
  64. package/lib/hooks/index.d.ts +13 -0
  65. package/lib/hooks/index.js +168 -0
  66. package/lib/hooks/register/register.FormHideItem.d.ts +9 -0
  67. package/lib/hooks/register/register.FormHideItem.js +60 -0
  68. package/lib/hooks/register/register.FormItem.d.ts +24 -0
  69. package/lib/hooks/register/register.FormItem.js +101 -0
  70. package/lib/hooks/register/register.FormList.d.ts +9 -0
  71. package/lib/hooks/register/register.FormList.js +76 -0
  72. package/lib/hooks/register/register.form.d.ts +4 -0
  73. package/lib/hooks/register/register.form.js +44 -0
  74. package/lib/hooks/useAttrs.d.ts +25 -0
  75. package/lib/hooks/useAttrs.js +63 -0
  76. package/lib/hooks/useEffect.d.ts +2 -0
  77. package/lib/hooks/useEffect.js +46 -0
  78. package/lib/hooks/useForm.d.ts +8 -0
  79. package/lib/hooks/useForm.js +61 -0
  80. package/lib/hooks/useFormItem.d.ts +8 -0
  81. package/lib/hooks/useFormItem.js +61 -0
  82. package/lib/hooks/useFormItemParentName.d.ts +18 -0
  83. package/lib/hooks/useFormItemParentName.js +84 -0
  84. package/lib/hooks/useFormList.d.ts +8 -0
  85. package/lib/hooks/useFormList.js +61 -0
  86. package/lib/hooks/useHtmlFor.d.ts +2 -0
  87. package/lib/hooks/useHtmlFor.js +41 -0
  88. package/lib/hooks/useMultipleForm.d.ts +8 -0
  89. package/lib/hooks/useMultipleForm.js +61 -0
  90. package/lib/hooks/useRule.d.ts +4 -0
  91. package/lib/hooks/useRule.js +46 -0
  92. package/lib/index.d.ts +4 -0
  93. package/lib/index.js +87 -0
  94. package/lib/instance/formInstance.d.ts +58 -0
  95. package/lib/instance/formInstance.js +166 -0
  96. package/lib/instance/formItemBaseInstance.d.ts +23 -0
  97. package/lib/instance/formItemBaseInstance.js +40 -0
  98. package/lib/instance/formItemInstance.d.ts +18 -0
  99. package/lib/instance/formItemInstance.js +45 -0
  100. package/lib/instance/formListInstance.d.ts +43 -0
  101. package/lib/instance/formListInstance.js +129 -0
  102. package/lib/instance/index.d.ts +5 -0
  103. package/lib/instance/index.js +96 -0
  104. package/lib/instance/multipleInstance.d.ts +32 -0
  105. package/lib/instance/multipleInstance.js +111 -0
  106. package/lib/instance/ruleIntsnace.d.ts +45 -0
  107. package/lib/instance/ruleIntsnace.js +96 -0
  108. package/lib/interface/index.d.ts +31 -0
  109. package/lib/interface/index.js +18 -0
  110. package/lib/utils/cloneByNamePathList.d.ts +3 -0
  111. package/lib/utils/cloneByNamePathList.js +45 -0
  112. package/lib/utils/get.d.ts +26 -0
  113. package/lib/utils/get.js +47 -0
  114. package/lib/utils/index.d.ts +4 -0
  115. package/lib/utils/index.js +87 -0
  116. package/lib/utils/interface.d.ts +3 -0
  117. package/lib/utils/interface.js +18 -0
  118. package/lib/utils/set.d.ts +32 -0
  119. package/lib/utils/set.js +63 -0
  120. package/lib/utils/utils.d.ts +172 -0
  121. package/lib/utils/utils.js +198 -0
  122. package/package.json +30 -0
  123. package/src/hooks/attr/attr.FormItem.tsx +185 -0
  124. package/src/hooks/index.ts +13 -0
  125. package/src/hooks/register/register.FormHideItem.ts +28 -0
  126. package/src/hooks/register/register.FormItem.ts +93 -0
  127. package/src/hooks/register/register.FormList.ts +49 -0
  128. package/src/hooks/register/register.form.ts +13 -0
  129. package/src/hooks/useAttrs.ts +64 -0
  130. package/src/hooks/useEffect.ts +13 -0
  131. package/src/hooks/useForm.ts +34 -0
  132. package/src/hooks/useFormItem.ts +33 -0
  133. package/src/hooks/useFormItemParentName.ts +49 -0
  134. package/src/hooks/useFormList.ts +33 -0
  135. package/src/hooks/useHtmlFor.ts +9 -0
  136. package/src/hooks/useMultipleForm.ts +30 -0
  137. package/src/hooks/useRule.ts +16 -0
  138. package/src/index.ts +4 -0
  139. package/src/instance/formInstance.ts +220 -0
  140. package/src/instance/formItemBaseInstance.ts +23 -0
  141. package/src/instance/formItemInstance.ts +23 -0
  142. package/src/instance/formListInstance.ts +108 -0
  143. package/src/instance/index.ts +5 -0
  144. package/src/instance/multipleInstance.ts +109 -0
  145. package/src/instance/ruleIntsnace.ts +89 -0
  146. package/src/interface/index.ts +38 -0
  147. package/src/utils/cloneByNamePathList.ts +13 -0
  148. package/src/utils/get.ts +53 -0
  149. package/src/utils/index.ts +4 -0
  150. package/src/utils/interface.ts +4 -0
  151. package/src/utils/set.ts +85 -0
  152. package/src/utils/utils.ts +504 -0
@@ -0,0 +1,28 @@
1
+ import { ref, watch, toValue } from 'vue';
2
+ import { RegisterFormItemOptions } from './register.FormItem';
3
+ import { useFormItemParentNameInject } from '../useFormItemParentName';
4
+ import { useFormInject } from '../useForm';
5
+
6
+ interface RegisterFormHideItemOptions extends Omit<RegisterFormItemOptions, 'rules'> {}
7
+
8
+ /**注册表单隐藏表单项到表单实例中*/
9
+ export const useRegisterFormHideItem = (options: RegisterFormHideItemOptions) => {
10
+ const { name, sort, isJoinParentField = true } = options;
11
+ const form = useFormInject();
12
+ const { newName } = useFormItemParentNameInject({
13
+ name,
14
+ sort,
15
+ isJoinParentField,
16
+ });
17
+ const isHide = ref(form.value.getFieldHideValue(newName.value));
18
+
19
+ watch(
20
+ () => [toValue(form), toValue(newName)],
21
+ () => {
22
+ isHide.value = form.value.getFieldHideValue(toValue(newName));
23
+ },
24
+ { immediate: true },
25
+ );
26
+
27
+ return { form, isHide };
28
+ };
@@ -0,0 +1,93 @@
1
+ /**
2
+ * @description 注册组件
3
+ */
4
+
5
+ import { computed, 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 { useRules } from './../useRule';
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({
30
+ name,
31
+ sort,
32
+ isJoinParentField,
33
+ });
34
+ // 注册规则
35
+ // 注册单个实例
36
+ const formItemInstance = useFormItem();
37
+ const ruleInstance = useRules();
38
+ const ruleItems = ref(rules || []);
39
+
40
+ watch(
41
+ () => toValue(options.rules),
42
+ (newVal) => {
43
+ ruleItems.value = newVal || [];
44
+ },
45
+ );
46
+ watch(
47
+ () => [toValue(form)],
48
+ () => {
49
+ ruleInstance.value.instance = form;
50
+ formItemInstance.value.instance = form;
51
+ },
52
+ { immediate: true },
53
+ );
54
+
55
+ watch(
56
+ () => [toValue(newName), toValue(ruleInstance)],
57
+ () => {
58
+ ruleInstance.value.name = toValue(newName);
59
+ ruleInstance.value.rules = ruleItems;
60
+ formItemInstance.value.name = toValue(newName);
61
+ formItemInstance.value.rule = ruleInstance;
62
+ },
63
+ { immediate: true },
64
+ );
65
+
66
+ watch(
67
+ () => [toValue(newSort)],
68
+ () => {
69
+ ruleInstance.value.sort = toValue(newSort);
70
+ formItemInstance.value.sort = toValue(newSort);
71
+ },
72
+ { immediate: true },
73
+ );
74
+
75
+ watch(
76
+ () => [toValue(parentItem)],
77
+ () => {
78
+ formItemInstance.value.parentDataField = toValue(toValue(parentItem).name);
79
+ },
80
+ { immediate: true },
81
+ );
82
+ useEffect(() => form.value.registerFormItem(formItemInstance));
83
+
84
+ return {
85
+ ruleInstance,
86
+ formItemInstance,
87
+ form,
88
+ newName,
89
+ newSort,
90
+ parentItem,
91
+ deepRefData,
92
+ };
93
+ };
@@ -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
+ () => [toValue(newName)],
15
+ () => {
16
+ formListInstance.value.ctor(toValue(newName));
17
+ },
18
+ { immediate: true },
19
+ );
20
+
21
+ watch(
22
+ () => [toValue(form), formItemInstance, ruleInstance],
23
+ () => {
24
+ formListInstance.value.instance = form;
25
+ formListInstance.value.formItemInstance = formItemInstance;
26
+ formListInstance.value.rule = ruleInstance;
27
+ },
28
+ { immediate: true },
29
+ );
30
+
31
+ watch(
32
+ () => [options.sort, toValue(parentItem)],
33
+ () => {
34
+ formListInstance.value.sort = toValue(parentItem.value.sort);
35
+ formListInstance.value.parentDataField = toValue(parentItem.value.name);
36
+ },
37
+ { immediate: true },
38
+ );
39
+
40
+ useEffect(() => {
41
+ return form.value.registerFormList(newName.value, formListInstance);
42
+ });
43
+
44
+ return {
45
+ ruleInstance,
46
+ formItemInstance,
47
+ formListInstance,
48
+ };
49
+ };
@@ -0,0 +1,13 @@
1
+ import { useMultipleFormInject } from '../useMultipleForm';
2
+ import { FormInstanceBase } from '../../instance/formInstance';
3
+ import { useEffect } from '../useEffect';
4
+ import { Ref } from 'vue';
5
+
6
+ /**注册表单实例到多表单收集实例中*/
7
+ export const useRegisterForm = (form: Ref<FormInstanceBase>, name?: string) => {
8
+ const multipleForm = useMultipleFormInject();
9
+ useEffect(() => {
10
+ if (name) return multipleForm.value.ctor(name, form);
11
+ });
12
+ return multipleForm;
13
+ };
@@ -0,0 +1,64 @@
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
+ colCount: ref(4),
58
+ errorLayout: ref('left-bottom'),
59
+ labelMode: ref('top'),
60
+ showColon: ref(true),
61
+ })),
62
+ );
63
+ return attrs;
64
+ }
@@ -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,34 @@
1
+ import { FormInstanceBase } from '../instance/formInstance';
2
+
3
+ import { provide, inject, ref, Ref, toValue } from 'vue';
4
+
5
+ const formProvideSymbol = Symbol('carefrees-form');
6
+
7
+ /**表单实例 Context */
8
+ export function useFormProvide<T = any>(form: Ref<FormInstanceBase<T>> | FormInstanceBase<T>) {
9
+ const newForm = useForm(form);
10
+ provide(formProvideSymbol, newForm);
11
+ }
12
+
13
+ /**子项中获取表单实例*/
14
+ export function useFormInject<T = any>() {
15
+ const form = inject<Ref<FormInstanceBase<T>>>(
16
+ formProvideSymbol,
17
+ ref(new FormInstanceBase<T>()) as unknown as Ref<FormInstanceBase<T>>,
18
+ );
19
+ return form;
20
+ }
21
+
22
+ /**初始化表单实例*/
23
+ export function useForm<T = any>(form?: Ref<FormInstanceBase<T>> | FormInstanceBase<T>) {
24
+ const refForm = ref<FormInstanceBase<T>>();
25
+ if (!refForm.value) {
26
+ const value = toValue(form);
27
+ if (value) {
28
+ refForm.value = value;
29
+ } else {
30
+ refForm.value = new FormInstanceBase<T>();
31
+ }
32
+ }
33
+ return refForm as Ref<FormInstanceBase<T>>;
34
+ }
@@ -0,0 +1,33 @@
1
+ import { FormItemInstanceBase } from '../instance/formItemInstance';
2
+ import { provide, inject, ref, Ref, toValue } from 'vue';
3
+
4
+ const formItemProvideSymbol = Symbol('carefrees-form-item');
5
+
6
+ /**表单项实例 Context */
7
+ export function useFormItemProvide(formItem: Ref<FormItemInstanceBase> | FormItemInstanceBase) {
8
+ const newFormItem = useFormItem(formItem);
9
+ provide(formItemProvideSymbol, newFormItem);
10
+ }
11
+
12
+ /**子项中获取表单项实例*/
13
+ export function useFormItemInject() {
14
+ const formItem = inject<Ref<FormItemInstanceBase>>(
15
+ formItemProvideSymbol,
16
+ ref(new FormItemInstanceBase()) as Ref<FormItemInstanceBase>,
17
+ );
18
+ return formItem;
19
+ }
20
+
21
+ /** 初始化 表单项实例*/
22
+ export function useFormItem(formItem?: Ref<FormItemInstanceBase> | FormItemInstanceBase) {
23
+ const refForm = ref<FormItemInstanceBase>();
24
+ if (!refForm.value) {
25
+ const value = toValue(formItem);
26
+ if (value) {
27
+ refForm.value = value;
28
+ } else {
29
+ refForm.value = new FormItemInstanceBase();
30
+ }
31
+ }
32
+ return refForm as Ref<FormItemInstanceBase>;
33
+ }
@@ -0,0 +1,49 @@
1
+ import { provide, inject, computed, ComputedRef, ref, toValue } from 'vue';
2
+ import { PartialComputedRefs } from '../interface';
3
+
4
+ export interface FormItemParentNamOptions {
5
+ /**字段*/
6
+ name: string;
7
+ /**排序*/
8
+ sort?: string;
9
+ /**是否拼接父级字段*/
10
+ isJoinParentField?: boolean;
11
+ }
12
+
13
+ export type FormItemParentNameProviderProps = PartialComputedRefs<Omit<FormItemParentNamOptions, 'isJoinParentField'>>;
14
+
15
+ const parentNameProvideSymbol = Symbol('carefrees-parent-name');
16
+
17
+ export const useFormItemParentNameProvide = (props: FormItemParentNameProviderProps) => {
18
+ const { name, sort } = props;
19
+ provide(
20
+ parentNameProvideSymbol,
21
+ computed(() => ({ name, sort })),
22
+ );
23
+ };
24
+
25
+ /**表单项获取父级字段*/
26
+ export const useFormItemParentNameInject = (options: FormItemParentNamOptions) => {
27
+ const { isJoinParentField = true, sort, name } = options;
28
+ const parentItem = inject<ComputedRef<FormItemParentNameProviderProps>>(
29
+ parentNameProvideSymbol,
30
+ computed(() => ({ name: ref(''), sort: ref('') })),
31
+ );
32
+ const newName = computed(() => {
33
+ const _name = toValue(parentItem.value.name);
34
+ if (_name && isJoinParentField) {
35
+ if (/^\./.test(`${name}`)) {
36
+ return [_name, name].filter(Boolean).join('');
37
+ } else if (name) {
38
+ return [_name, '.', name].filter(Boolean).join('');
39
+ }
40
+ return [_name, name].filter(Boolean).join('');
41
+ }
42
+ return [name].filter(Boolean).join('');
43
+ });
44
+ const newSort = computed(() => {
45
+ const _sort = toValue(parentItem.value.sort);
46
+ return [isJoinParentField ? _sort : '', sort].filter(Boolean).join('-');
47
+ });
48
+ return { newName, newSort, parentItem };
49
+ };
@@ -0,0 +1,33 @@
1
+ import { FormListInstanceBase } from '../instance/formListInstance';
2
+ import { provide, inject, ref, Ref, toValue } from 'vue';
3
+
4
+ const formListProvideSymbol = Symbol('carefrees-form-list');
5
+
6
+ /**表单List实例 Context */
7
+ export function useFormListProvide(formList: Ref<FormListInstanceBase> | FormListInstanceBase) {
8
+ const newFormList = useFormList(formList);
9
+ provide(formListProvideSymbol, newFormList);
10
+ }
11
+
12
+ /**子项中获取表单List实例*/
13
+ export function useFormListInject() {
14
+ const formList = inject<Ref<FormListInstanceBase>>(
15
+ formListProvideSymbol,
16
+ ref(new FormListInstanceBase()) as Ref<FormListInstanceBase>,
17
+ );
18
+ return formList;
19
+ }
20
+
21
+ /**初始化 表单List实例*/
22
+ export function useFormList(formList?: Ref<FormListInstanceBase> | FormListInstanceBase) {
23
+ const refForm = ref<FormListInstanceBase>();
24
+ if (!refForm.value) {
25
+ const value = toValue(formList);
26
+ if (value) {
27
+ refForm.value = value;
28
+ } else {
29
+ refForm.value = new FormListInstanceBase();
30
+ }
31
+ }
32
+ return refForm as Ref<FormListInstanceBase>;
33
+ }
@@ -0,0 +1,9 @@
1
+ import { ref, computed, Ref } from 'vue';
2
+
3
+ let localId = 0;
4
+ export const useHtmlFor = (suffix: Ref<string>) => {
5
+ const count = ref(localId++);
6
+ return computed(() => {
7
+ return `carefree-vue-form-item_${count.value.toString(32)}_${suffix.value}`;
8
+ });
9
+ };
@@ -0,0 +1,30 @@
1
+ import { MultipleInstanceBase } from '../instance/multipleInstance';
2
+ import { provide, inject, ref, Ref, toValue } from 'vue';
3
+
4
+ const multipleFormProvideSymbol = Symbol('carefrees-multiple-form');
5
+
6
+ /**多表单收集 Context */
7
+ export function useMultipleFormProvide(multipleForm?: Ref<MultipleInstanceBase> | MultipleInstanceBase) {
8
+ const newMultipleForm = useMultipleForm(multipleForm);
9
+ provide(multipleFormProvideSymbol, newMultipleForm);
10
+ }
11
+
12
+ /**子项中获取 多表单收集 实例*/
13
+ export function useMultipleFormInject() {
14
+ const multipleForm = inject<Ref<MultipleInstanceBase>>(multipleFormProvideSymbol, ref(new MultipleInstanceBase()));
15
+ return multipleForm;
16
+ }
17
+
18
+ /**初始化 多表单收集 实例*/
19
+ export function useMultipleForm(multipleForm?: Ref<MultipleInstanceBase> | MultipleInstanceBase) {
20
+ const refForm = ref<MultipleInstanceBase>();
21
+ if (!refForm.value) {
22
+ const value = toValue(multipleForm);
23
+ if (value) {
24
+ refForm.value = value;
25
+ } else {
26
+ refForm.value = new MultipleInstanceBase();
27
+ }
28
+ }
29
+ return refForm;
30
+ }
@@ -0,0 +1,16 @@
1
+ import { RuleInstanceBase } from '../instance/ruleIntsnace';
2
+ import { ref, Ref, toValue } from 'vue';
3
+
4
+ /** 初始化 规则实例*/
5
+ export function useRules(rules?: Ref<RuleInstanceBase> | RuleInstanceBase) {
6
+ const refRules = ref<RuleInstanceBase>();
7
+ if (!refRules.value) {
8
+ const value = toValue(rules);
9
+ if (value) {
10
+ refRules.value = value;
11
+ } else {
12
+ refRules.value = new RuleInstanceBase();
13
+ }
14
+ }
15
+ return refRules as Ref<RuleInstanceBase>;
16
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './interface';
2
+ export * from './hooks/index';
3
+ export * from './utils';
4
+ export * from './instance';