Skip to content

Commit fff60d3

Browse files
committed
fix: add hooks to call extra-data
1 parent 7508c15 commit fff60d3

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/v2/hooks/useExtra.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {
2+
useContext,
3+
} from "react";
4+
import { IForm } from "../logic/createForm";
5+
import { FormContext } from "../contexts/FormContext";
6+
import useSubscribeAndCompare from "./useSubscribeAndCompare";
7+
8+
export const useExtra = <T>(props: {
9+
defaultValue?: any;
10+
form?: any;
11+
name: string;
12+
// eslint-disable-next-line no-unused-vars
13+
log?: () => void;
14+
}) => {
15+
const { form: formContext } = useContext(FormContext);
16+
const { form = formContext, name } = props as { form: IForm<any>, name: string };
17+
18+
useSubscribeAndCompare({
19+
form,
20+
subject: "extras",
21+
getState() {
22+
return form.getExtra(name);
23+
},
24+
});
25+
26+
return {
27+
state: form.getExtra(name) as T,
28+
form,
29+
};
30+
};
31+
32+
export default useExtra;

src/v2/logic/createForm.ts

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export interface ISubject {
5151
fields: any[];
5252
containers: any[];
5353
supports: any[];
54+
extras: any[];
5455
}
5556

5657
export type IEventCallback = () => boolean
@@ -130,6 +131,7 @@ const createForm = <TSchema>(props: ICreateFormProps<TSchema>) => {
130131
fields: [],
131132
containers: [],
132133
supports: [],
134+
extras: [],
133135
};
134136

135137
const parse = (expression: IExpressionString, terms: Record<string, any> = {}, version: string = "v1") => {
@@ -143,6 +145,11 @@ const createForm = <TSchema>(props: ICreateFormProps<TSchema>) => {
143145
if (!key) return undefined;
144146
return get(_state.values, key);
145147
}
148+
// extras
149+
function getExtra(key?: string) {
150+
if (!key) return undefined;
151+
return get(_config.extraData, key);
152+
}
146153
function initValue(key: string, value: any) { set(_state.values, key, value); }
147154
// fieldState
148155
function getError(key?: string) {
@@ -717,6 +724,7 @@ const createForm = <TSchema>(props: ICreateFormProps<TSchema>) => {
717724
setContainerFormState,
718725
setSupportFormState,
719726
getValue,
727+
getExtra,
720728
setValue,
721729
getError,
722730
setError,

0 commit comments

Comments
 (0)