Skip to content

Commit

Permalink
fix(schema): compat eva expression actions (#951)
Browse files Browse the repository at this point in the history
* fix: expression

* feat: add unitest
  • Loading branch information
JohnIsOnTheRoad committed Jul 6, 2020
1 parent 0162788 commit aed0369
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/react-schema-renderer/src/__tests__/actions.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,47 @@ test('createFormActions', async () => {
expect(queryByTestId('inputA').getAttribute('value')).toEqual(VALUE_A)
expect(queryByTestId('inputB').getAttribute('value')).toEqual(VALUE_B)
})

test('createFormActions', async () => {
const actionsSymbol = Symbol.for("__REVA_ACTIONS")
const actions = createFormActions()
const actions2 = createFormActions()
expect(actions[actionsSymbol]).toEqual(true)
expect(actions2[actionsSymbol]).toEqual(true)
const TestComponent = () => (
<SchemaForm
actions={actions}
effects={($, { setFieldState }) => {
$('onFormInit').subscribe(() => {
setFieldState('aaa', state => {
state.value = 'change value of aaa field onFormInit'
})
})
}}
components={{ SchemaForm }}
>
<Field name="nested"
x-component="SchemaForm"
x-component-props={{
actions: actions2
}}
/>
<Field
name="aaa"
type="string"
x-props={{
'data-testid': 'inputA'
}}
/>
</SchemaForm>
)

const { queryByTestId } = render(<TestComponent />)
expect(queryByTestId('inputA').getAttribute('value')).toEqual(
'change value of aaa field onFormInit'
)
actions.setFormState(state => (state.values = { aaa: 123 }))
await wait()
expect(actions[actionsSymbol]).toEqual(true)
expect(actions2[actionsSymbol]).toEqual(true)
})
4 changes: 4 additions & 0 deletions packages/react-schema-renderer/src/shared/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@formily/shared'

const ExpRE = /^\s*\{\{(.*)\}\}\s*$/
const actionsSymbol = Symbol.for("__REVA_ACTIONS")

export const complieExpression = <Source = any, Context = any>(
source: Source,
Expand All @@ -25,6 +26,9 @@ export const complieExpression = <Source = any, Context = any>(
} else if (isArr(source)) {
return source.map(value => complie(value))
} else if (isPlainObj(source)) {
if (source[actionsSymbol]) {
return source
}
if (source['_isAMomentObject']) {
return source
}
Expand Down

0 comments on commit aed0369

Please sign in to comment.