Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@uform/antd/next): improve form step #431

Merged
merged 1 commit into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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