|
1 | | -import {FormMeta} from "@/types"; |
2 | 1 | import React from "react"; |
3 | | -import {FormContextScope} from "@/context"; |
4 | 2 | import {FormItemFactory} from "@/factory"; |
| 3 | +import {useFormContext} from "@/hooks"; |
5 | 4 |
|
6 | | -interface FormSubViewProps{ |
7 | | - meta:FormMeta; |
| 5 | +interface FormSubViewProps { |
| 6 | + formCode: string; |
8 | 7 | Form: React.ComponentType<any>; |
9 | | - context:FormContextScope; |
10 | | - review:boolean; |
| 8 | + review: boolean; |
11 | 9 | } |
12 | 10 |
|
13 | | - |
14 | | -export const FormSubView:React.FC<FormSubViewProps> = (props)=>{ |
| 11 | +export const FormSubView: React.FC<FormSubViewProps> = (props) => { |
15 | 12 | const Form = props.Form; |
16 | | - const formCode = props.meta.code; |
17 | | - const fields = props.meta.fields; |
| 13 | + const formCode = props.formCode; |
18 | 14 | const review = props.review; |
19 | | - const context = props.context; |
20 | 15 |
|
21 | | - const formControl = context.getFormControl(formCode); |
| 16 | + const {state, context} = useFormContext(); |
| 17 | + |
| 18 | + const fields = React.useMemo(() => { |
| 19 | + const formList = []; |
| 20 | + formList.push(state); |
| 21 | + for (const subForm of state.subForms || []) { |
| 22 | + formList.push(subForm); |
| 23 | + } |
| 24 | + for (const subForm of formList) { |
| 25 | + if (subForm.code === formCode) { |
| 26 | + return subForm.fields; |
| 27 | + } |
| 28 | + } |
| 29 | + return []; |
| 30 | + }, [state]); |
22 | 31 |
|
23 | | - const formTarget = formControl?.getProxyTarget(); |
| 32 | + const formControl = context.getFormControl(formCode); |
24 | 33 |
|
| 34 | + const formTarget = formControl?.getProxyTarget(); |
25 | 35 |
|
26 | | - React.useEffect(()=>{ |
27 | | - const events = context.getEventContext().getLoadEvents(); |
| 36 | + React.useEffect(() => { |
| 37 | + const events = context.getEventContext().getLoadEvents(); |
28 | 38 | if (events && events.length > 0) { |
29 | 39 | for (const event of events) { |
30 | 40 | event.event(); |
31 | 41 | } |
32 | 42 | } |
33 | | - },[]); |
34 | | - |
| 43 | + }, []); |
35 | 44 |
|
36 | 45 | return ( |
37 | 46 | <Form |
38 | 47 | form={formTarget} |
39 | 48 | > |
40 | 49 | {fields.map(field => { |
41 | | - return FormItemFactory.getInstance().render(formCode,field,review,context); |
| 50 | + return FormItemFactory.getInstance().render(formCode, field, review, context); |
42 | 51 | })} |
43 | 52 | </Form> |
44 | 53 | ) |
|
0 commit comments