@carefrees/form-utils-react-hooks 0.0.4
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 +9 -0
- package/esm/hooks/register/register.FormHideItem.d.ts +9 -0
- package/esm/hooks/register/register.FormHideItem.js +31 -0
- package/esm/hooks/register/register.FormItem.d.ts +23 -0
- package/esm/hooks/register/register.FormItem.js +42 -0
- package/esm/hooks/register/register.FormList.d.ts +9 -0
- package/esm/hooks/register/register.FormList.js +31 -0
- package/esm/hooks/register/register.form.d.ts +3 -0
- package/esm/hooks/register/register.form.js +17 -0
- package/esm/hooks/useAttrs.d.ts +30 -0
- package/esm/hooks/useAttrs.js +10 -0
- package/esm/hooks/useForm.d.ts +7 -0
- package/esm/hooks/useForm.js +13 -0
- package/esm/hooks/useFormItem.d.ts +7 -0
- package/esm/hooks/useFormItem.js +11 -0
- package/esm/hooks/useFormItemParentName.d.ts +30 -0
- package/esm/hooks/useFormItemParentName.js +61 -0
- package/esm/hooks/useFormList.d.ts +7 -0
- package/esm/hooks/useFormList.js +11 -0
- package/esm/hooks/useHtmlFor.d.ts +1 -0
- package/esm/hooks/useHtmlFor.js +10 -0
- package/esm/hooks/useMultipleForm.d.ts +13 -0
- package/esm/hooks/useMultipleForm.js +19 -0
- package/esm/hooks/useUpdate.d.ts +2 -0
- package/esm/hooks/useUpdate.js +10 -0
- package/esm/hooks/useWatch.d.ts +19 -0
- package/esm/hooks/useWatch.js +41 -0
- package/esm/index.d.ts +13 -0
- package/esm/index.js +13 -0
- package/lib/hooks/register/register.FormHideItem.d.ts +9 -0
- package/lib/hooks/register/register.FormHideItem.js +65 -0
- package/lib/hooks/register/register.FormItem.d.ts +23 -0
- package/lib/hooks/register/register.FormItem.js +76 -0
- package/lib/hooks/register/register.FormList.d.ts +9 -0
- package/lib/hooks/register/register.FormList.js +65 -0
- package/lib/hooks/register/register.form.d.ts +3 -0
- package/lib/hooks/register/register.form.js +51 -0
- package/lib/hooks/useAttrs.d.ts +30 -0
- package/lib/hooks/useAttrs.js +47 -0
- package/lib/hooks/useForm.d.ts +7 -0
- package/lib/hooks/useForm.js +53 -0
- package/lib/hooks/useFormItem.d.ts +7 -0
- package/lib/hooks/useFormItem.js +51 -0
- package/lib/hooks/useFormItemParentName.d.ts +30 -0
- package/lib/hooks/useFormItemParentName.js +101 -0
- package/lib/hooks/useFormList.d.ts +7 -0
- package/lib/hooks/useFormList.js +51 -0
- package/lib/hooks/useHtmlFor.d.ts +1 -0
- package/lib/hooks/useHtmlFor.js +44 -0
- package/lib/hooks/useMultipleForm.d.ts +13 -0
- package/lib/hooks/useMultipleForm.js +62 -0
- package/lib/hooks/useUpdate.d.ts +2 -0
- package/lib/hooks/useUpdate.js +44 -0
- package/lib/hooks/useWatch.d.ts +19 -0
- package/lib/hooks/useWatch.js +78 -0
- package/lib/index.d.ts +13 -0
- package/lib/index.js +168 -0
- package/package.json +30 -0
- package/src/hooks/register/register.FormHideItem.ts +29 -0
- package/src/hooks/register/register.FormItem.ts +52 -0
- package/src/hooks/register/register.FormList.ts +31 -0
- package/src/hooks/register/register.form.ts +18 -0
- package/src/hooks/useAttrs.ts +40 -0
- package/src/hooks/useForm.ts +23 -0
- package/src/hooks/useFormItem.ts +21 -0
- package/src/hooks/useFormItemParentName.ts +49 -0
- package/src/hooks/useFormList.ts +21 -0
- package/src/hooks/useHtmlFor.ts +9 -0
- package/src/hooks/useMultipleForm.ts +36 -0
- package/src/hooks/useUpdate.ts +12 -0
- package/src/hooks/useWatch.ts +61 -0
- package/src/index.ts +13 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { createContext, useContext, useMemo, createElement } from 'react';
|
|
2
|
+
|
|
3
|
+
export const FormItemParentNameContext = createContext({ name: '', sort: '' });
|
|
4
|
+
|
|
5
|
+
export interface FormItemParentNamOptions {
|
|
6
|
+
/**字段*/
|
|
7
|
+
name: string;
|
|
8
|
+
/**排序*/
|
|
9
|
+
sort?: string;
|
|
10
|
+
/**是否拼接父级字段*/
|
|
11
|
+
isJoinParentField?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface FormItemParentNameProviderProps extends Omit<FormItemParentNamOptions, 'isJoinParentField'> {
|
|
15
|
+
children?: React.ReactNode;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const FormItemParentNameProvider = (props: FormItemParentNameProviderProps) => {
|
|
19
|
+
const { name, sort, children } = props;
|
|
20
|
+
const value = useMemo(() => {
|
|
21
|
+
return { name, sort };
|
|
22
|
+
}, [name, sort]);
|
|
23
|
+
return createElement(FormItemParentNameContext.Provider, { value, children });
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**表单项获取父级字段*/
|
|
27
|
+
export const useFormItemParentName = (options: FormItemParentNamOptions) => {
|
|
28
|
+
const { isJoinParentField = true, sort, name } = options;
|
|
29
|
+
const parentItem = useContext(FormItemParentNameContext);
|
|
30
|
+
const parentName = parentItem.name;
|
|
31
|
+
const parentSort = parentItem.sort;
|
|
32
|
+
const newName = useMemo(() => {
|
|
33
|
+
if (parentName && isJoinParentField) {
|
|
34
|
+
if (/^\./.test(`${name}`)) {
|
|
35
|
+
return [parentName, name].filter(Boolean).join('');
|
|
36
|
+
} else if (name) {
|
|
37
|
+
return [parentName, '.', name].filter(Boolean).join('');
|
|
38
|
+
}
|
|
39
|
+
return [parentName, name].filter(Boolean).join('');
|
|
40
|
+
}
|
|
41
|
+
return [name].filter(Boolean).join('');
|
|
42
|
+
}, [isJoinParentField, name, parentName]);
|
|
43
|
+
const newSort = useMemo(
|
|
44
|
+
() => [isJoinParentField ? parentSort : '', sort].filter(Boolean).join('-'),
|
|
45
|
+
[isJoinParentField, parentSort, sort],
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
return { newName, newSort, parentItem, parentName: parentName };
|
|
49
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { FormListInstanceBase } from '@carefrees/form-utils';
|
|
2
|
+
import { createContext, useContext, useRef } from 'react';
|
|
3
|
+
|
|
4
|
+
/**表单List实例 Context */
|
|
5
|
+
export const FormListInstanceContext = createContext(new FormListInstanceBase());
|
|
6
|
+
|
|
7
|
+
/**子项中获取表单List实例*/
|
|
8
|
+
export const useFormListInstance = () => useContext(FormListInstanceContext);
|
|
9
|
+
|
|
10
|
+
/**初始化 表单List实例*/
|
|
11
|
+
export const useFormList = (formList?: FormListInstanceBase) => {
|
|
12
|
+
const ref = useRef<FormListInstanceBase>();
|
|
13
|
+
if (!ref.current) {
|
|
14
|
+
if (formList) {
|
|
15
|
+
ref.current = formList;
|
|
16
|
+
} else {
|
|
17
|
+
ref.current = new FormListInstanceBase();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return ref.current;
|
|
21
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useMemo, useRef } from 'react';
|
|
2
|
+
|
|
3
|
+
let localId = 0;
|
|
4
|
+
export const useHtmlFor = (suffix: string) => {
|
|
5
|
+
const count = useRef(localId++);
|
|
6
|
+
return useMemo(() => {
|
|
7
|
+
return `carefree-form-item_${count.current.toString(32)}_${suffix}`;
|
|
8
|
+
}, [count.current, suffix]);
|
|
9
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { MultipleInstanceBase } from '@carefrees/form-utils';
|
|
2
|
+
import { createContext, useContext, useRef, createElement } from 'react';
|
|
3
|
+
|
|
4
|
+
/**多表单收集 Context */
|
|
5
|
+
export const MultipleFormInstanceContext = createContext(new MultipleInstanceBase());
|
|
6
|
+
|
|
7
|
+
/**子项中获取 多表单收集 实例*/
|
|
8
|
+
export const useMultipleFormInstance = () => useContext(MultipleFormInstanceContext);
|
|
9
|
+
|
|
10
|
+
/**初始化 多表单收集 实例*/
|
|
11
|
+
export const useMultipleForm = (multipleForm?: MultipleInstanceBase) => {
|
|
12
|
+
const ref = useRef<MultipleInstanceBase>();
|
|
13
|
+
if (!ref.current) {
|
|
14
|
+
if (multipleForm) {
|
|
15
|
+
ref.current = multipleForm;
|
|
16
|
+
} else {
|
|
17
|
+
ref.current = new MultipleInstanceBase();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return ref.current;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export interface MultipleFormProviderProps {
|
|
24
|
+
children: React.ReactNode;
|
|
25
|
+
multipleForm?: MultipleInstanceBase;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**多表单收集 Provider */
|
|
29
|
+
export const MultipleFormProvider = (props: MultipleFormProviderProps) => {
|
|
30
|
+
const { children, multipleForm } = props;
|
|
31
|
+
const multipleFormInstance = useMultipleForm(multipleForm);
|
|
32
|
+
return createElement(MultipleFormInstanceContext.Provider, {
|
|
33
|
+
value: multipleFormInstance,
|
|
34
|
+
children,
|
|
35
|
+
});
|
|
36
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useState, useRef } from 'react';
|
|
2
|
+
|
|
3
|
+
/**更新页面状态*/
|
|
4
|
+
export const useUpdate = () => {
|
|
5
|
+
const [, _update] = useState({});
|
|
6
|
+
/**为了防止 hooks 闭包问题*/
|
|
7
|
+
const refUpdate = useRef<Function>(() => void 0);
|
|
8
|
+
refUpdate.current = () => {
|
|
9
|
+
_update({});
|
|
10
|
+
};
|
|
11
|
+
return refUpdate;
|
|
12
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import { FormInstanceBase } from '@carefrees/form-utils';
|
|
3
|
+
import { useFormInstance } from './useForm';
|
|
4
|
+
import { useFormItem } from './useFormItem';
|
|
5
|
+
|
|
6
|
+
export class WatchInstanceBase {
|
|
7
|
+
/**监听字段*/
|
|
8
|
+
name: string;
|
|
9
|
+
/**表单实例*/
|
|
10
|
+
form: FormInstanceBase;
|
|
11
|
+
/**老值*/
|
|
12
|
+
oldValue: any;
|
|
13
|
+
/**更新值*/
|
|
14
|
+
dispatch: (value: any) => void;
|
|
15
|
+
/**回调*/
|
|
16
|
+
callBack?: (value: any, form: FormInstanceBase) => void;
|
|
17
|
+
/**更新*/
|
|
18
|
+
updated = () => {
|
|
19
|
+
// 如果上一次和当前相等,则不进行更新
|
|
20
|
+
const newValue = this.form.getFieldValue(this.name);
|
|
21
|
+
if (this.oldValue === newValue) {
|
|
22
|
+
/**相同不进行更新*/
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (this.callBack) {
|
|
26
|
+
this.callBack(newValue, this.form);
|
|
27
|
+
} else {
|
|
28
|
+
this.dispatch(newValue);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 字段监听
|
|
35
|
+
*/
|
|
36
|
+
export const useWatch = (
|
|
37
|
+
name: string,
|
|
38
|
+
form: FormInstanceBase,
|
|
39
|
+
callBack?: (value: any, form: FormInstanceBase) => void,
|
|
40
|
+
) => {
|
|
41
|
+
const formInstance = form || useFormInstance();
|
|
42
|
+
const [oldValue, setValue] = useState(formInstance.getFieldValue(name));
|
|
43
|
+
const watch = useRef(new WatchInstanceBase()).current;
|
|
44
|
+
watch.name = name;
|
|
45
|
+
watch.oldValue = oldValue;
|
|
46
|
+
watch.dispatch = setValue;
|
|
47
|
+
watch.callBack = callBack;
|
|
48
|
+
watch.form = formInstance;
|
|
49
|
+
|
|
50
|
+
const formItemInstance = useFormItem();
|
|
51
|
+
useMemo(() => formItemInstance.ctor(name), []);
|
|
52
|
+
formItemInstance.instance = formInstance;
|
|
53
|
+
formItemInstance.isWatch = true;
|
|
54
|
+
formItemInstance.updated = watch.updated;
|
|
55
|
+
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
const unMount = formInstance.registerFormItem(formItemInstance);
|
|
58
|
+
return () => unMount();
|
|
59
|
+
}, []);
|
|
60
|
+
return [oldValue, formInstance, watch] as [any, FormInstanceBase, WatchInstanceBase];
|
|
61
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from './hooks/register/register.FormHideItem';
|
|
2
|
+
export * from './hooks/register/register.FormItem';
|
|
3
|
+
export * from './hooks/register/register.FormList';
|
|
4
|
+
export * from './hooks/register/register.form';
|
|
5
|
+
export * from './hooks/useAttrs';
|
|
6
|
+
export * from './hooks/useForm';
|
|
7
|
+
export * from './hooks/useFormItem';
|
|
8
|
+
export * from './hooks/useFormItemParentName';
|
|
9
|
+
export * from './hooks/useFormList';
|
|
10
|
+
export * from './hooks/useHtmlFor';
|
|
11
|
+
export * from './hooks/useMultipleForm';
|
|
12
|
+
export * from './hooks/useUpdate';
|
|
13
|
+
export * from './hooks/useWatch';
|