Skip to content

Commit

Permalink
feat(@uform/antd/next): improve form step (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Nov 25, 2019
1 parent 476d4cb commit b446c02
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
16 changes: 12 additions & 4 deletions packages/antd/src/components/FormStep.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useMemo, useRef, Fragment } from 'react'
import React, { useMemo, useRef, Fragment } from 'react'
import {
createControllerBox,
ISchemaVirtualFieldComponentProps,
Expand Down Expand Up @@ -30,8 +30,13 @@ type StepComponentExtendsProps = StateMap
export const FormStep: React.FC<IFormStep> &
StepComponentExtendsProps = createControllerBox<IFormStep>(
'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)
Expand All @@ -40,8 +45,11 @@ export const FormStep: React.FC<IFormStep> &
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) => {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export interface IFieldState<FieldProps = any> {
mounted: boolean
unmounted: boolean
props: FieldProps
[key: string]: any
}
export type FieldStateDirtyMap = StateDirtyMap<IFieldState>

Expand Down Expand Up @@ -209,6 +210,7 @@ export interface IFormState<FormProps = any> {
mounted: boolean
unmounted: boolean
props: FormProps
[key: string]: any
}

export type FormStateDirtyMap = StateDirtyMap<IFormState>
Expand Down Expand Up @@ -239,6 +241,7 @@ export interface IVirtualFieldState<FieldProps = any> {
mounted: boolean
unmounted: boolean
props: FieldProps
[key: string]: any
}
export type VirtualFieldStateDirtyMap = StateDirtyMap<IFieldState>

Expand Down
18 changes: 13 additions & 5 deletions packages/next/src/components/FormStep.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useMemo, useRef, Fragment } from 'react'
import React, { useMemo, useRef, Fragment } from 'react'
import {
createControllerBox,
ISchemaVirtualFieldComponentProps,
Expand Down Expand Up @@ -30,8 +30,13 @@ type StepComponentExtendsProps = StateMap
export const FormStep: React.FC<IFormStep> &
StepComponentExtendsProps = createControllerBox<IFormStep>(
'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)
Expand All @@ -40,8 +45,11 @@ export const FormStep: React.FC<IFormStep> &
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) => {
Expand All @@ -52,7 +60,7 @@ export const FormStep: React.FC<IFormStep> &
items.forEach(({ name }, index) => {
if (!name)
throw new Error('FormStep dataSource must include `name` property')

setFieldState(name, (state: any) => {
state.display = index === value
})
Expand Down

0 comments on commit b446c02

Please sign in to comment.