Skip to content

Commit

Permalink
fix: empty string can be default value
Browse files Browse the repository at this point in the history
  • Loading branch information
monkindey committed Jul 10, 2019
1 parent 9a0025a commit 2446cf3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/core/src/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ export class Field implements IField {

public initialize(options: IFieldOptions) {
const rules = this.getRulesFromProps(options.props)
this.value = !isEmpty(options.value) ? clone(options.value) : this.value
this.value = clone(options.value)

this.name = !isEmpty(options.name) ? options.name : this.name || ''
this.namePath = resolveFieldPath(this.name)

Expand Down
34 changes: 34 additions & 0 deletions packages/react/src/__tests__/value.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react'
import { render } from '@testing-library/react'
import SchemaForm, { Field, registerFormField, connect } from '../index'

beforeEach(() => {
registerFormField(
'string',
connect()(props => <div data-testid="value">{typeof props.value}</div>)
)
})

test('default value', async () => {
const Component = () => (
<SchemaForm defaultValue={{ foo: '' }}>
<Field name="foo" type="string" />
</SchemaForm>
)

const { getByTestId } = render(<Component />)
await sleep(33)
expect(getByTestId('value').textContent).toEqual('string')
})

test('initialValues', async () => {
const Component = () => (
<SchemaForm initialValues={{ foo: '' }}>
<Field name="foo" type="string" />
</SchemaForm>
)

const { getByTestId } = render(<Component />)
await sleep(33)
expect(getByTestId('value').textContent).toEqual('string')
})

0 comments on commit 2446cf3

Please sign in to comment.