diff --git a/packages/antd/src/components/FormStep.tsx b/packages/antd/src/components/FormStep.tsx index 2a960efe34a..e38fe19a5f2 100644 --- a/packages/antd/src/components/FormStep.tsx +++ b/packages/antd/src/components/FormStep.tsx @@ -1,4 +1,4 @@ -import React, { useState, useMemo, useRef, Fragment } from 'react' +import React, { useMemo, useRef, Fragment } from 'react' import { createControllerBox, ISchemaVirtualFieldComponentProps, @@ -30,8 +30,13 @@ type StepComponentExtendsProps = StateMap export const FormStep: React.FC & StepComponentExtendsProps = createControllerBox( 'step', - ({ form, schema, children }: ISchemaVirtualFieldComponentProps) => { - const [current, setCurrent] = useState(0) + ({ + form, + path, + schema, + current, + children + }: ISchemaVirtualFieldComponentProps) => { const ref = useRef(current) const { dataSource, ...stepProps } = schema.getExtendsComponentProps() const items = toArr(dataSource) @@ -40,8 +45,11 @@ export const FormStep: React.FC & value: cur, preValue: current }) - setCurrent(cur) + form.setFieldState(path, state => { + state.current = cur + }) } + current = current || 0 useFormEffects(({ setFieldState }) => { items.forEach(({ name }, index) => { setFieldState(name, (state: any) => { diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index ec8ba9692d1..f8fefb2a445 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -146,6 +146,7 @@ export interface IFieldState { mounted: boolean unmounted: boolean props: FieldProps + [key: string]: any } export type FieldStateDirtyMap = StateDirtyMap @@ -209,6 +210,7 @@ export interface IFormState { mounted: boolean unmounted: boolean props: FormProps + [key: string]: any } export type FormStateDirtyMap = StateDirtyMap @@ -239,6 +241,7 @@ export interface IVirtualFieldState { mounted: boolean unmounted: boolean props: FieldProps + [key: string]: any } export type VirtualFieldStateDirtyMap = StateDirtyMap diff --git a/packages/next/src/components/FormStep.tsx b/packages/next/src/components/FormStep.tsx index d40175409e1..b98926f068a 100644 --- a/packages/next/src/components/FormStep.tsx +++ b/packages/next/src/components/FormStep.tsx @@ -1,4 +1,4 @@ -import React, { useState, useMemo, useRef, Fragment } from 'react' +import React, { useMemo, useRef, Fragment } from 'react' import { createControllerBox, ISchemaVirtualFieldComponentProps, @@ -30,8 +30,13 @@ type StepComponentExtendsProps = StateMap export const FormStep: React.FC & StepComponentExtendsProps = createControllerBox( 'step', - ({ props, form, children }: ISchemaVirtualFieldComponentProps) => { - const [current, setCurrent] = useState(0) + ({ + props, + form, + path, + current, + children + }: ISchemaVirtualFieldComponentProps) => { const ref = useRef(current) const { dataSource, ...stepProps } = props['x-component-props'] || {} const items = toArr(dataSource) @@ -40,8 +45,11 @@ export const FormStep: React.FC & value: cur, preValue: current }) - setCurrent(cur) + form.setFieldState(path, state => { + state.current = cur + }) } + current = current || 0 useFormEffects(({ setFieldState }) => { items.forEach(({ name }, index) => { setFieldState(name, (state: any) => { @@ -52,7 +60,7 @@ export const FormStep: React.FC & items.forEach(({ name }, index) => { if (!name) throw new Error('FormStep dataSource must include `name` property') - + setFieldState(name, (state: any) => { state.display = index === value })