Skip to content

Commit

Permalink
fix(@uform/core): fix bug
Browse files Browse the repository at this point in the history
- fix bug that async schema default property is not work
- fix bug that visible property is not work by setFieldState when FormInit
  • Loading branch information
janryWang committed Mar 8, 2019
1 parent 334523e commit 8864ba9
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 15 deletions.
30 changes: 26 additions & 4 deletions packages/core/src/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class Field {
this.dirty = false
this.pristine = true
this.valid = true
this.removed = false
this.invalid = false
this.visible = true
this.editable = true
Expand All @@ -37,31 +38,51 @@ export class Field {
}

initialize(options) {
const editable = this.getEditableFromProps(options.props)
const rules = this.getRulesFromProps(options.props)
this.value = !isEmpty(options.value) ? clone(options.value) : this.value
this.initialValue = !isEmpty(options.initialValue)
? options.initialValue
: this.initialValue
: !isEmpty(this.initialValue)
? this.initialValue
: this.getInitialValueFromProps(options.props)
this.name = !isEmpty(options.name) ? options.name : this.name || ''
this.namePath = resolveFieldPath(this.name)
const editable = this.getEditableFromProps(options.props)
this.editable = !isEmpty(editable) ? editable : this.editable
this.path = resolveFieldPath(
!isEmpty(options.path) ? options.path : this.path || []
)
const rules = this.getRulesFromProps(options.props)
this.rules = !isEmpty(rules) ? rules : this.rules
this.required = hasRequired(this.rules)
this.props = !isEmpty(options.props)
? !isEmpty(this.props)
? { ...this.props, ...clone(options.props) }
: clone(options.props)
: this.props
this.visible = true
if (this.removed) {
this.removed = false
this.visible = true
}
if (!this.initialized) {
if (isEmpty(this.value) && !isEmpty(this.initialValue)) {
this.value = clone(this.initialValue)
this.context.setIn(this.name, this.value)
this.context.setInitialValueIn(this.name, this.initialValue)
}
}
if (isFn(options.onChange)) {
this.onChange(options.onChange)
}
}

getInitialValueFromProps(props) {
if (props) {
if (!isEmpty(props['default'])) {
return props['default']
}
}
}

getEditableFromProps(props) {
if (props) {
if (!isEmpty(props.editable)) {
Expand Down Expand Up @@ -155,6 +176,7 @@ export class Field {
remove() {
this.value = undefined
this.visible = false
this.removed = true
if (!this.context) return
this.context.deleteIn(this.name)
if (typeof this.value === 'object') {
Expand Down
46 changes: 44 additions & 2 deletions packages/react/src/__tests__/dynamic.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Fragment } from 'react'
import React, { Fragment, useEffect, useState } from 'react'
import SchemaForm, {
Field,
registerFormField,
Expand All @@ -8,7 +8,7 @@ import SchemaForm, {
createVirtualBox
} from '../index'
import { toArr } from '@uform/utils'
import { render, fireEvent } from 'react-testing-library'
import { render, fireEvent, act } from 'react-testing-library'

let FormCard

Expand Down Expand Up @@ -378,3 +378,45 @@ test('dynamic default value', async () => {
expect(queryAllByTestId('item').length).toBe(1)
expect(queryAllByTestId('input').length).toBe(2)
})

test('dynamic async default value', async () => {
const TestComponent = () => {
const [schema, setSchema] = useState()
useEffect(() => {
setTimeout(() => {
act(() => {
setSchema({
type: 'object',
properties: {
container: {
type: 'array',
default: [{}],
'x-component': 'container',
items: {
type: 'object',
properties: {
aa: {
type: 'string'
},
bb: {
type: 'string'
}
}
}
}
}
})
})
}, 33)
}, [])
return (
<SchemaForm schema={schema}>
<button type='submit'>Submit</button>
</SchemaForm>
)
}
const { queryAllByTestId } = render(<TestComponent />)
await sleep(100)
expect(queryAllByTestId('item').length).toBe(1)
expect(queryAllByTestId('input').length).toBe(2)
})
30 changes: 30 additions & 0 deletions packages/react/src/__tests__/visible.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react'
import SchemaForm, { Field, registerFormField, connect } from '../index'
import { render } from 'react-testing-library'

beforeEach(() => {
registerFormField('string', connect()(props => <div>{props.value}</div>))
})

test('set visible by setFieldState', async () => {
const TestComponent = () => {
return (
<SchemaForm
effects={($, { setFieldState }) => {
$('onFormInit').subscribe(() => {
setFieldState('aa', state => {
state.visible = false
})
})
}}
>
<Field type='string' name='aa' default='123321' />
</SchemaForm>
)
}

const { queryByText } = render(<TestComponent />)

await sleep(33)
expect(queryByText('123321')).toBeNull()
})
28 changes: 19 additions & 9 deletions packages/react/src/state/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,21 @@ export const StateForm = createHOC((options, Form) => {
type: 'submitting',
state: this.state
})
promise.then(() => {
this.notify({
type: 'submitted',
state: this.state
})
})
promise.then(
() => {
this.notify({
type: 'submitted',
state: this.state
})
},
error => {
this.notify({
type: 'submitted',
state: this.state
})
throw error
}
)
}
}
}
Expand All @@ -147,10 +156,11 @@ export const StateForm = createHOC((options, Form) => {
this.form.changeValues(this.props.value)
}
if (
this.props.initialValues &&
!isEqual(this.props.initialValues, prevProps.initialValues)
(this.props.initialValues &&
!isEqual(this.props.initialValues, prevProps.initialValues)) ||
(this.props.schema && !isEqual(this.props.schema, prevProps.schema))
) {
this.form.initialize(this.props.initialValues)
this.form.initialize(this.props.initialValues, this.props.schema)
}
}

Expand Down

0 comments on commit 8864ba9

Please sign in to comment.