@carefrees/form-utils-vue 0.0.12 → 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 (71) hide show
  1. package/README.md +54 -6
  2. package/esm/form/form.js +12 -7
  3. package/esm/formItem/form.Item.base.js +1 -6
  4. package/esm/formItem/form.Item.base.vue.d.ts +1 -0
  5. package/esm/formItem/form.hide.item.js +1 -5
  6. package/esm/formItem/form.hide.item.vue.d.ts +1 -0
  7. package/esm/formItem/formItem.js +0 -4
  8. package/esm/formItem/index.d.ts +2 -0
  9. package/esm/index.d.ts +1 -1
  10. package/esm/index.js +1 -1
  11. package/esm/interface/index.d.ts +4 -11
  12. package/esm/interface/layout.d.ts +1 -1
  13. package/esm/interface/layout.formItem.d.ts +1 -1
  14. package/esm/layout/layout.formItem.js +1 -1
  15. package/esm/layout/layout.js +1 -1
  16. package/package.json +3 -2
  17. package/src/form/form.vue +13 -6
  18. package/src/formItem/form.Item.base.vue +5 -5
  19. package/src/formItem/form.hide.item.vue +3 -1
  20. package/src/formItem/formItem.vue +2 -1
  21. package/src/index.ts +1 -1
  22. package/src/interface/index.ts +4 -13
  23. package/src/interface/layout.formItem.ts +1 -2
  24. package/src/interface/layout.ts +2 -1
  25. package/src/layout/layout.formItem.vue +2 -1
  26. package/src/layout/layout.vue +2 -1
  27. package/esm/hooks/attr/attr.FormItem.d.ts +0 -97
  28. package/esm/hooks/attr/attr.FormItem.js +0 -97
  29. package/esm/hooks/index.d.ts +0 -13
  30. package/esm/hooks/index.js +0 -13
  31. package/esm/hooks/register/register.FormHideItem.d.ts +0 -9
  32. package/esm/hooks/register/register.FormHideItem.js +0 -42
  33. package/esm/hooks/register/register.FormItem.d.ts +0 -26
  34. package/esm/hooks/register/register.FormItem.js +0 -67
  35. package/esm/hooks/register/register.FormList.d.ts +0 -9
  36. package/esm/hooks/register/register.FormList.js +0 -42
  37. package/esm/hooks/register/register.form.d.ts +0 -3
  38. package/esm/hooks/register/register.form.js +0 -10
  39. package/esm/hooks/useAttrs.d.ts +0 -25
  40. package/esm/hooks/useAttrs.js +0 -26
  41. package/esm/hooks/useEffect.d.ts +0 -2
  42. package/esm/hooks/useEffect.js +0 -12
  43. package/esm/hooks/useForm.d.ts +0 -8
  44. package/esm/hooks/useForm.js +0 -17
  45. package/esm/hooks/useFormItem.d.ts +0 -7
  46. package/esm/hooks/useFormItem.js +0 -17
  47. package/esm/hooks/useFormItemParentName.d.ts +0 -18
  48. package/esm/hooks/useFormItemParentName.js +0 -47
  49. package/esm/hooks/useFormList.d.ts +0 -7
  50. package/esm/hooks/useFormList.js +0 -17
  51. package/esm/hooks/useHtmlFor.d.ts +0 -2
  52. package/esm/hooks/useHtmlFor.js +0 -7
  53. package/esm/hooks/useMultipleForm.d.ts +0 -7
  54. package/esm/hooks/useMultipleForm.js +0 -18
  55. package/esm/instance/ruleIntsnace.d.ts +0 -49
  56. package/esm/instance/ruleIntsnace.js +0 -65
  57. package/src/hooks/attr/attr.FormItem.tsx +0 -174
  58. package/src/hooks/index.ts +0 -13
  59. package/src/hooks/register/register.FormHideItem.ts +0 -45
  60. package/src/hooks/register/register.FormItem.ts +0 -80
  61. package/src/hooks/register/register.FormList.ts +0 -49
  62. package/src/hooks/register/register.form.ts +0 -12
  63. package/src/hooks/useAttrs.ts +0 -64
  64. package/src/hooks/useEffect.ts +0 -13
  65. package/src/hooks/useForm.ts +0 -28
  66. package/src/hooks/useFormItem.ts +0 -28
  67. package/src/hooks/useFormItemParentName.ts +0 -49
  68. package/src/hooks/useFormList.ts +0 -28
  69. package/src/hooks/useHtmlFor.ts +0 -9
  70. package/src/hooks/useMultipleForm.ts +0 -29
  71. package/src/instance/ruleIntsnace.ts +0 -105
@@ -1,65 +0,0 @@
1
- import async_validator from "async-validator";
2
- import { ref, toValue } from "vue";
3
- class RuleInstanceBase2 {
4
- sort;
5
- instance;
6
- name = '';
7
- rules = ref([]);
8
- messages = ref([]);
9
- updated;
10
- isRequired = ()=>{
11
- if (this.instance?.getFieldHideRulesValue?.(this.name)) return false;
12
- const findItem = (toValue(this.rules) || []).find((item)=>item?.required);
13
- return !!findItem;
14
- };
15
- ctor = (name, rules)=>{
16
- this.name = name;
17
- this.rules.value = rules || [];
18
- return this;
19
- };
20
- isValidate = ()=>{
21
- if (this.instance?.getFieldHideRulesValue?.(this.name)) return false;
22
- const _rules = toValue(this.rules);
23
- return !!(Array.isArray(_rules) && _rules.length);
24
- };
25
- updatedMessages = (messages)=>{
26
- this.messages.value = messages || [];
27
- this.updated?.({});
28
- };
29
- updatedRules = (rules)=>{
30
- this.rules.value = rules;
31
- this.updatedMessages?.([]);
32
- };
33
- validate = (isOnly = false)=>new Promise((resolve, reject)=>{
34
- const value = this.instance?.getFieldValue?.(this.name);
35
- if (this.instance?.getFieldHideRulesValue?.(this.name)) {
36
- this.updatedMessages([]);
37
- resolve({
38
- [this.name]: value
39
- });
40
- }
41
- new async_validator({
42
- [this.name]: toValue(this.rules) || []
43
- }).validate({
44
- [this.name]: value
45
- }).then((values)=>{
46
- if (!isOnly) this.updatedMessages([]);
47
- resolve(values);
48
- }).catch(({ errors })=>{
49
- if (Array.isArray(errors)) {
50
- if (!isOnly) this.updatedMessages(errors);
51
- reject(errors);
52
- } else reject();
53
- });
54
- });
55
- getValidateResult = ()=>{
56
- const _messages = toValue(this.messages);
57
- const tip = Array.isArray(_messages) ? _messages.map((it)=>it.message) : '';
58
- const isInvalid = Array.isArray(tip) ? !!tip.length : !!tip;
59
- return {
60
- tip,
61
- isInvalid
62
- };
63
- };
64
- }
65
- export { RuleInstanceBase2 };
@@ -1,174 +0,0 @@
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, 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?: (
20
- ruleInstance: Ref<RuleInstanceBase2>,
21
- form: FormInstanceBase,
22
- formItemInstance: FormItemInstanceBase,
23
- ) => void;
24
- /**输入框属性重写*/
25
- useAttrs?: (attrs: any, form: FormInstanceBase, formItemInstance: FormItemInstanceBase) => any;
26
- /**输入框属性*/
27
- inputAttrs?: Object;
28
- /**传递组件字段*/
29
- valuePropName?: string;
30
- /**取值字段(默认和valuePropName值相同)*/
31
- getValuePath?: string;
32
- /**自定义获取值*/
33
- getValueFromEvent?: (event: any, form: FormInstanceBase, formItemInstance: FormItemInstanceBase) => any;
34
- /**值格式化*/
35
- formatValue?: (value: any, form: FormInstanceBase, formItemInstance: FormItemInstanceBase, event: any) => any;
36
- /**触发数据更新之后触发(用于数据联动之类的)*/
37
- onAfterUpdate?: (value: any, form: FormInstanceBase, formItemInstance: FormItemInstanceBase, event: any) => void;
38
- /**事件名称*/
39
- trigger?: string;
40
- }
41
-
42
- /**表单项参数*/
43
- export const useFormItemAttr = (options: FormItemAttrOptions) => {
44
- const {
45
- trigger = 'onChange',
46
- dependencies,
47
- noticeOnlyRuleDataField,
48
- isNoticeParentField,
49
- noticeWatchField,
50
- preserve,
51
- valuePropName = 'value',
52
- getValuePath = valuePropName,
53
- getValueFromEvent,
54
- formatValue,
55
- onAfterUpdate,
56
- useAttrs,
57
- useRules,
58
- inputAttrs,
59
- ...rest
60
- } = options;
61
- const { formItemInstance, form, ruleInstance, newName, newSort, deepRefData } = useRegisterFormItem({ ...rest });
62
- formItemInstance.dependencies = dependencies;
63
- formItemInstance.noticeOnlyRuleDataField = noticeOnlyRuleDataField;
64
- formItemInstance.isNoticeParentField = isNoticeParentField;
65
- formItemInstance.onAfterUpdate = onAfterUpdate;
66
- formItemInstance.noticeWatchField = noticeWatchField;
67
- formItemInstance.preserve = preserve;
68
- /**获取值*/
69
- const oldValue = ref(get(form.formData, toValue(newName)));
70
-
71
- watch(
72
- () => [get(form.formData, toValue(newName)), toValue(newName)],
73
- () => {
74
- oldValue.value = form.getFieldValue(toValue(newName));
75
- },
76
- { immediate: true },
77
- );
78
-
79
- const onValueChange = (event: any) => {
80
- try {
81
- let value = event;
82
- if (typeof getValueFromEvent === 'function') {
83
- value = getValueFromEvent(event, form, formItemInstance);
84
- } else if (event && event.target && typeof event.target === 'object' && getValuePath in event.target) {
85
- value = get(event.target, getValuePath);
86
- }
87
- if (typeof formatValue === 'function') {
88
- value = formatValue(value, form, formItemInstance, event);
89
- }
90
- if (oldValue.value !== value) {
91
- form.updatedFieldValue(toValue(newName), value, 'validate');
92
- formItemInstance.onAfterUpdate?.(value, form, formItemInstance, event);
93
- if (Array.isArray(formItemInstance.noticeWatchField) && formItemInstance.noticeWatchField.length) {
94
- form.noticeWatch(formItemInstance.noticeWatchField);
95
- }
96
- if (
97
- Array.isArray(formItemInstance.noticeOnlyRuleDataField) &&
98
- formItemInstance.noticeOnlyRuleDataField.length
99
- ) {
100
- form.onlyValidate(formItemInstance.noticeOnlyRuleDataField);
101
- }
102
- if (formItemInstance.isNoticeParentField && formItemInstance.parentDataField) {
103
- form.notice(formItemInstance.parentDataField);
104
- }
105
- }
106
- } catch (error) {
107
- console.log(error);
108
- }
109
- };
110
-
111
- formItemInstance.onChange = onValueChange;
112
- const htmlFor = useHtmlFor(newName);
113
-
114
- watch(
115
- () => htmlFor,
116
- () => {
117
- formItemInstance.htmlFor = htmlFor.value;
118
- },
119
- { immediate: true },
120
- );
121
-
122
- /**属性处理*/
123
- const newAttrs = computed(() => {
124
- const _attr = inputAttrs || {};
125
- return {
126
- ..._attr,
127
- [trigger]: onValueChange,
128
- name: toValue(newName),
129
- id: toValue(htmlFor),
130
- [valuePropName]: toValue(oldValue),
131
- };
132
- });
133
-
134
- const attrsLastData = ref(toValue(newAttrs));
135
-
136
- watch(
137
- () => [toValue(newAttrs), toValue(deepRefData), toValue(oldValue)],
138
- () => {
139
- attrsLastData.value = useAttrs?.(toValue(newAttrs), form, formItemInstance) || toValue(newAttrs);
140
- },
141
- { immediate: true },
142
- );
143
-
144
- /**规则处理**/
145
- const validateResult = ref(toValue(ruleInstance).getValidateResult());
146
-
147
- watch(
148
- () => [toValue(deepRefData), toValue(oldValue)],
149
- () => {
150
- useRules?.(ruleInstance as any, form, formItemInstance);
151
- },
152
- );
153
-
154
- watch(
155
- () => [toValue(ruleInstance).messages, toValue(ruleInstance).rules, toValue(oldValue)],
156
- () => {
157
- validateResult.value = toValue(ruleInstance).getValidateResult();
158
- },
159
- );
160
-
161
- return {
162
- valuePropName,
163
- htmlFor,
164
- onChange: onValueChange,
165
- formItemInstance,
166
- form,
167
- ruleInstance,
168
- newAttrs,
169
- attrsLastData,
170
- validateResult,
171
- newSort,
172
- newName,
173
- };
174
- };
@@ -1,13 +0,0 @@
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';
@@ -1,45 +0,0 @@
1
- import { ref, toValue, 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
- () => [toValue(isHide)],
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, toValue(newSort)],
33
- () => {
34
- hideItemInstance.value.instance = form;
35
- hideItemInstance.value.sort = toValue(newSort);
36
- },
37
- { immediate: true },
38
- );
39
-
40
- useEffect(() => {
41
- return form.registerFormHideItem(toValue(hideItemInstance) as FormHideItemInstanceBase);
42
- });
43
-
44
- return { form, isHide };
45
- };
@@ -1,80 +0,0 @@
1
- /**
2
- * @description 注册组件
3
- */
4
-
5
- import { Ref, 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<RuleInstanceBase2> = ref<any>(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
- () => [toValue(newName)],
46
- () => {
47
- ruleInstance.value.ctor(toValue(newName), rules || []);
48
- formItemInstance.ctor(toValue(newName), toValue(ruleInstance) as any);
49
- },
50
- { immediate: true },
51
- );
52
-
53
- watch(
54
- () => [toValue(newSort)],
55
- () => {
56
- ruleInstance.value.sort = toValue(newSort);
57
- formItemInstance.sort = toValue(newSort);
58
- },
59
- { immediate: true },
60
- );
61
-
62
- watch(
63
- () => [toValue(parentItem)],
64
- () => {
65
- formItemInstance.parentDataField = toValue(toValue(parentItem).name);
66
- },
67
- { immediate: true },
68
- );
69
-
70
- const updated = () => {
71
- deepRefData.value = { __temp: 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
- };
@@ -1,49 +0,0 @@
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.ctor(toValue(newName));
17
- },
18
- { immediate: true },
19
- );
20
-
21
- watch(
22
- () => [form, formItemInstance, toValue(ruleInstance)],
23
- () => {
24
- formListInstance.instance = form;
25
- formListInstance.formItemInstance = formItemInstance;
26
- formListInstance.rule = toValue(ruleInstance) as any;
27
- },
28
- { immediate: true },
29
- );
30
-
31
- watch(
32
- () => [options.sort, toValue(parentItem)],
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
- };
@@ -1,12 +0,0 @@
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
- };
@@ -1,64 +0,0 @@
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
- }
@@ -1,13 +0,0 @@
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
- };
@@ -1,28 +0,0 @@
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
- }
@@ -1,28 +0,0 @@
1
- import { FormItemInstanceBase } from '@carefrees/form-utils';
2
- import { provide, inject, ref } from 'vue';
3
-
4
- const formItemProvideSymbol = Symbol('carefrees-form-item');
5
-
6
- /**表单项实例 Context */
7
- export function useFormItemProvide(formItem: FormItemInstanceBase) {
8
- provide(formItemProvideSymbol, formItem);
9
- }
10
-
11
- /**子项中获取表单项实例*/
12
- export function useFormItemInject() {
13
- const formItem = inject<FormItemInstanceBase>(formItemProvideSymbol, new FormItemInstanceBase());
14
- return formItem;
15
- }
16
-
17
- /**s初始化 表单项实例*/
18
- export function useFormItem(formItem?: FormItemInstanceBase) {
19
- const refForm = ref<FormItemInstanceBase>();
20
- if (!refForm.value) {
21
- if (formItem) {
22
- refForm.value = formItem;
23
- } else {
24
- refForm.value = new FormItemInstanceBase();
25
- }
26
- }
27
- return refForm.value;
28
- }
@@ -1,49 +0,0 @@
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
- };
@@ -1,28 +0,0 @@
1
- import { FormListInstanceBase } from '@carefrees/form-utils';
2
- import { provide, inject, ref } from 'vue';
3
-
4
- const formListProvideSymbol = Symbol('carefrees-form-list');
5
-
6
- /**表单List实例 Context */
7
- export function useFormListProvide(formList: FormListInstanceBase) {
8
- provide(formListProvideSymbol, formList);
9
- }
10
-
11
- /**子项中获取表单List实例*/
12
- export function useFormListInject() {
13
- const formList = inject<FormListInstanceBase>(formListProvideSymbol, new FormListInstanceBase());
14
- return formList;
15
- }
16
-
17
- /**初始化 表单List实例*/
18
- export function useFormList(formList?: FormListInstanceBase) {
19
- const refForm = ref<FormListInstanceBase>();
20
- if (!refForm.value) {
21
- if (formList) {
22
- refForm.value = formList;
23
- } else {
24
- refForm.value = new FormListInstanceBase();
25
- }
26
- }
27
- return refForm.value;
28
- }
@@ -1,9 +0,0 @@
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
- };