Skip to content

Commit

Permalink
fix(react): fix form render dirty check (#1056)
Browse files Browse the repository at this point in the history
* fix(react): fix form render dirty check

* fix(ci): fix ci
  • Loading branch information
janryWang authored Oct 26, 2020
1 parent 9db2eeb commit 5aeed55
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
18 changes: 16 additions & 2 deletions packages/antd/src/components/Form.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import React, { useRef, useMemo } from 'react'
import { Form as AntdForm } from 'antd'
import { InternalForm } from '@formily/react-schema-renderer'
import { normalizeCol, autoScrollInValidateFailed, isAntdV4,log } from '../shared'
import {
normalizeCol,
autoScrollInValidateFailed,
isAntdV4,
log,
cloneChlildren
} from '../shared'
import { FormItemDeepProvider } from '../context'
import { IAntdFormProps } from '../types'
import {
PreviewText,
PreviewTextConfigProps
} from '@formily/react-shared-components'
import { isFn } from '@formily/shared'

export const Form: React.FC<IAntdFormProps &
PreviewTextConfigProps> = props => {
Expand All @@ -26,6 +33,7 @@ export const Form: React.FC<IAntdFormProps &
previewPlaceholder,
editable,
validateFirst,
children,
...rest
} = props
const formRef = useRef<HTMLDivElement>()
Expand Down Expand Up @@ -70,7 +78,13 @@ export const Form: React.FC<IAntdFormProps &
labelCol={normalizeCol(props.labelCol)}
wrapperCol={normalizeCol(props.wrapperCol)}
layout={inline ? 'inline' : props.layout}
/>
>
{() => {
return isFn(children)
? children(form)
: cloneChlildren(children)
}}
</AntdForm>
</div>
</FormItemDeepProvider>
</PreviewText.ConfigProvider>
Expand Down
13 changes: 12 additions & 1 deletion packages/antd/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@ import {
getRegistry
} from '@formily/react-schema-renderer'
import { version } from 'antd'
import { each } from '@formily/shared'
import { each, isArr } from '@formily/shared'
export * from '@formily/shared'

export const isAntdV4 = /^4\./.test(version)

export const cloneChlildren = (children: any, props?: any) => {
return React.isValidElement(children)
? React.cloneElement(children, {
...props,
children: cloneChlildren(children.props['children'])
})
: isArr(children)
? children.map((child, key) => cloneChlildren(child, { key }))
: children
}

export const autoScrollInValidateFailed = (formRef: any) => {
if (formRef.current) {
setTimeout(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/antd/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type ColSpanType = number | string

export type IAntdSchemaFormProps = Omit<
FormProps,
'onSubmit' | 'defaultValue' | 'labelCol' | 'wrapperCol'
'onSubmit' | 'defaultValue' | 'labelCol' | 'wrapperCol' | 'children'
> &
IFormItemTopProps &
PreviewTextConfigProps &
Expand All @@ -31,7 +31,7 @@ export type IAntdSchemaFieldProps = IMarkupSchemaFieldProps

export type IAntdFormProps = Omit<
FormProps,
'onSubmit' | 'defaultValue' | 'labelCol' | 'wrapperCol'
'onSubmit' | 'defaultValue' | 'labelCol' | 'wrapperCol' | 'children'
> &
IFormItemTopProps &
IFormProps<any, any>
Expand Down
17 changes: 15 additions & 2 deletions packages/next/src/components/Form.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import React, { useRef } from 'react'
import { Form as NextForm } from '@alifd/next'
import { InternalForm } from '@formily/react-schema-renderer'
import { normalizeCol, autoScrollInValidateFailed,log } from '../shared'
import {
normalizeCol,
autoScrollInValidateFailed,
log,
isFn,
cloneChlildren
} from '../shared'
import { FormItemDeepProvider } from '../context'
import { INextFormProps } from '../types'
import {
Expand All @@ -25,6 +31,7 @@ export const Form: React.FC<INextFormProps &
onValidateFailed,
previewPlaceholder,
validateFirst,
children,
...rest
} = props
const formRef = useRef<HTMLDivElement>()
Expand Down Expand Up @@ -57,7 +64,13 @@ export const Form: React.FC<INextFormProps &
onReset={() => {
form.reset({ validate: false, forceClear: false })
}}
/>
>
{() => {
return isFn(children)
? children(form)
: cloneChlildren(children)
}}
</NextForm>
</div>
</FormItemDeepProvider>
</PreviewText.ConfigProvider>
Expand Down
13 changes: 12 additions & 1 deletion packages/next/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ import {
IConnectProps,
getRegistry
} from '@formily/react-schema-renderer'
import { each } from '@formily/shared'
import { each, isArr } from '@formily/shared'
export * from '@formily/shared'

export const cloneChlildren = (children: any, props?: any) => {
return React.isValidElement(children)
? React.cloneElement(children, {
...props,
children: cloneChlildren(children.props['children'])
})
: isArr(children)
? children.map((child, key) => cloneChlildren(child, { key }))
: children
}

export const autoScrollInValidateFailed = (formRef: any) => {
if (formRef.current) {
setTimeout(() => {
Expand Down

0 comments on commit 5aeed55

Please sign in to comment.