Skip to content

Commit

Permalink
fix(json-schema): fix is null (#2560)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Dec 2, 2021
1 parent f5a1d1b commit 5f2db00
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/json-schema/src/transformer.ts
Expand Up @@ -95,7 +95,7 @@ const setSchemaFieldState = (
options: IFieldStateSetterOptions,
demand = false
) => {
const { request, target, field, scope } = options || {}
const { request, target, runner, field, scope } = options || {}
if (!request) return
if (target) {
if (request.state) {
Expand All @@ -119,6 +119,14 @@ const setSchemaFieldState = (
)
)
}
if (isStr(runner) && runner) {
field.form.setFieldState(target, (state) => {
shallowCompile(`{{function(){${runner}}}}`, {
...scope,
$target: state,
})()
})
}
} else {
if (request.state) {
field.setState((state) => patchCompile(state, request.state, scope))
Expand All @@ -128,6 +136,9 @@ const setSchemaFieldState = (
patchSchemaCompile(state, request.schema, scope, demand)
)
}
if (isStr(runner) && runner) {
shallowCompile(`{{function(){${runner}}}}`, scope)()
}
}
}

Expand Down Expand Up @@ -195,11 +206,9 @@ const getUserReactions =
field,
target,
request,
runner,
scope,
})
if (isStr(runner)) {
shallowCompile(`{{function(){${runner}}}}`, scope)()
}
}

if (target) {
Expand Down
1 change: 1 addition & 0 deletions packages/json-schema/src/types.ts
Expand Up @@ -130,6 +130,7 @@ export interface IFieldStateSetterOptions {
field: GeneralField
target?: FormPathPattern
request: ISchemaFieldUpdateRequest
runner?: string
scope?: any
}

Expand Down
48 changes: 48 additions & 0 deletions packages/react/src/__tests__/schema.markup.spec.tsx
Expand Up @@ -868,3 +868,51 @@ test('void field children', async () => {
expect(queryByTestId('btn').textContent).toBe('placeholder')
})
})

test('x-reactions runner for target', async () => {
const form = createForm()
const getTarget = jest.fn()
const SchemaField = createSchemaField({
components: {
Input: () => <div></div>,
Button: (props) => (
<button
data-testid="btn"
onClick={(e) => {
e.preventDefault()
props.onChange('123')
}}
>
Click {props.value}
</button>
),
},
scope: {
getTarget,
},
})

const { getByTestId } = render(
<FormProvider form={form}>
<SchemaField>
<SchemaField.String name="target" default="333" x-component="Input" />
<SchemaField.String
x-component="Button"
x-reactions={{
target: 'target',
effects: ['onFieldInputValueChange'],
fulfill: {
run: 'getTarget($target.value)',
},
}}
/>
</SchemaField>
</FormProvider>
)
fireEvent.click(getByTestId('btn'))
await waitFor(() => {
expect(getByTestId('btn').textContent).toBe('Click 123')
expect(getTarget).toBeCalledWith('333')
expect(getTarget).toBeCalledTimes(1)
})
})

0 comments on commit 5f2db00

Please sign in to comment.