Skip to content

Commit

Permalink
fix(@uform/react): invariant initialValues will not be changed when f…
Browse files Browse the repository at this point in the history
…orm rerender (#214)
  • Loading branch information
monkindey authored and janryWang committed Aug 2, 2019
1 parent 05ced41 commit b9efa4c
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 27 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ node_js:
- "11"

before_script:
- lerna clean --yes
- lerna bootstrap --no-ci

script:
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"lint": "eslint --ext .ts,.tsx,.js \"packages/*/src/**.@(ts|tsx|js)\" \"scripts/**/*.js\" --fix",
"postinstall": "opencollective-postinstall"
},
"resolutions": {
"@types/react": "16.8.23"
},
"devDependencies": {
"@alifd/next": "^1.14.2",
"@babel/cli": "^7.2.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/builder-next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"scripts": {
"build": "tsc"
},
"resolutions": {
"@types/react": "16.8.23"
},
"peerDependencies": {
"@alifd/next": "^1.13.1",
"@babel/runtime": "^7.4.4",
Expand Down
15 changes: 5 additions & 10 deletions packages/builder-next/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
"compilerOptions": {
"outDir": "./lib",
"declaration": false,
"allowJs": true
"allowJs": true,
"skipLibCheck": true
},
"include": [
"./src/**/*.js",
"./src/**/*.ts",
"./src/**/*.tsx"
],
"exclude": [
"./src/__tests__/*"
]
}
"include": ["./src/**/*.js", "./src/**/*.ts", "./src/**/*.tsx"],
"exclude": ["./src/__tests__/*"]
}
3 changes: 3 additions & 0 deletions packages/builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"react": ">=16.8.0",
"react-dom": ">=16.8.0"
},
"resolutions": {
"@types/react": "16.8.23"
},
"dependencies": {
"@uform/react": "^0.3.7",
"@uform/utils": "^0.3.7",
Expand Down
13 changes: 5 additions & 8 deletions packages/builder/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
"compilerOptions": {
"outDir": "./lib",
"declaration": false,
"allowJs": true
"allowJs": true,
"skipLibCheck": true
},
"include": [
"./src/**/*.js"
],
"exclude": [
"./src/__tests__/*"
]
}
"include": ["./src/**/*.js"],
"exclude": ["./src/__tests__/*"]
}
17 changes: 14 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import {
} from '@uform/validator'

import { Form } from './form'
import { caculateSchemaInitialValues, isFn, each, isEmpty } from './utils'
import {
caculateSchemaInitialValues,
isFn,
each,
isEmpty,
clone
} from './utils'

export * from './path'

Expand All @@ -27,7 +33,7 @@ export const createForm = ({
let fields = []
let calculatedValues = caculateSchemaInitialValues(
schema,
isEmpty(values) ? initialValues : values,
isEmpty(values) ? clone(initialValues) : clone(values),
({ name, path, schemaPath }, schema: ISchema, value: any) => {
fields.push({ name, path, schemaPath, schema, value })
}
Expand Down Expand Up @@ -78,6 +84,11 @@ export const createForm = ({
return form
}

export { setValidationLocale, setValidationLanguage, Form }
export {
setValidationLocale,
setValidationLanguage,
Form,
caculateSchemaInitialValues
}

export default createForm
65 changes: 62 additions & 3 deletions packages/react/src/__tests__/value.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ registerFormField(
))
)

registerFormField(
'name-string',
connect()(props => (
<input
data-testid={`test-input-${props.name}`}
{...props}
value={props.value || ''}
/>
))
)

test('default value', async () => {
const Component = () => (
<SchemaForm defaultValue={{ foo: '' }}>
Expand Down Expand Up @@ -260,26 +271,74 @@ test('controlled with hooks by dynamic value', async () => {
expect(onChangeHandler).toHaveBeenCalledTimes(3)
})

test('invariant initialValues will not be changed when form rerender', async () => {
const Component = () => {
const [, setDisabled] = useState(false)

return (
<div>
<SchemaForm
initialValues={{ a1: 'a1' }}
onSubmit={() => {
act(setDisabled)
}}
>
<Field type="name-string" x-props={{ name: 'a1' }} name="a1" />
<Field
type="name-string"
name="a2"
x-props={{ name: 'a2' }}
default={'a2'}
/>
<Field type="name-string" name="a3" x-props={{ name: 'a3' }} />
<button type="submit">Click</button>
</SchemaForm>
</div>
)
}

const { queryByTestId, queryByText } = render(<Component />)
expect(queryByTestId('test-input-a1').value).toEqual('a1')
expect(queryByTestId('test-input-a2').value).toEqual('a2')
expect(queryByTestId('test-input-a3').value).toEqual('')

fireEvent.click(queryByText('Click'))
await sleep(33)

expect(queryByTestId('test-input-a1').value).toEqual('a1')
expect(queryByTestId('test-input-a2').value).toEqual('a2')
expect(queryByTestId('test-input-a3').value).toEqual('')

// 重新设置 SchemaForm Rerender
fireEvent.click(queryByText('Click'))
await sleep(33)

expect(queryByTestId('test-input-a1').value).toEqual('a1')
expect(queryByTestId('test-input-a2').value).toEqual('a2')
expect(queryByTestId('test-input-a3').value).toEqual('')
})

test('submit with number name', async () => {
const onSubmitHandler = jest.fn()
const Component = () => {
return (
<SchemaForm onSubmit={onSubmitHandler}>
<Field type="object" name="aaa">
<Field name="123" type="string" />
<Field name="bbb" type="string" />
</Field>
<button type="submit">Click</button>
</SchemaForm>
)
}

const { queryByTestId, baseElement, queryByText } = render(<Component />)
const { queryByTestId, queryByText } = render(<Component />)
fireEvent.change(queryByTestId('test-input'), { target: { value: '333' } })
fireEvent.click(queryByText('Click'))

await sleep(33)
expect(onSubmitHandler).toHaveBeenCalledWith({
aaa: {
'123': '333'
bbb: '333'
}
})
})
5 changes: 4 additions & 1 deletion packages/react/src/state/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,10 @@ export const StateForm = createHOC((options, Form) => {
!isEmpty(initialValues) &&
!isEqual(initialValues, prevProps.initialValues)
) {
this.form.initialize({ values: initialValues, initialValues })
this.form.initialize({
values: initialValues,
initialValues
})
}
if (!isEmpty(editable) && !isEqual(editable, prevProps.editable)) {
this.form.changeEditable(editable)
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const schemaTraverse = (
export const caculateSchemaInitialValues = (
schema: ISchema,
initialValues: any,
callback: (pathInfo: IPathInfo, schema: ISchema, value: any) => void
callback?: (pathInfo: IPathInfo, schema: ISchema, value: any) => void
) => {
initialValues = initialValues || schema.default || {}
schemaTraverse(schema, (subSchema, $path, parentPath) => {
Expand All @@ -121,7 +121,7 @@ export const caculateSchemaInitialValues = (
if (!isEmpty(value)) {
setIn(initialValues, name, value)
}
if (isFn(callback)) {
if (callback && isFn(callback)) {
const newPath = {
name,
path,
Expand Down

0 comments on commit b9efa4c

Please sign in to comment.