Skip to content

Commit

Permalink
fix(core): fix unmount onChange trigger and x-linkages array merge (#742
Browse files Browse the repository at this point in the history
)

* fix(core): fix unmount onChange trigger

* fix(schema-renderer): fix array merge
  • Loading branch information
janryWang authored Mar 23, 2020
1 parent c61d595 commit b928dc3
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 9 deletions.
81 changes: 80 additions & 1 deletion docs/zh-cn/jsx-develop/faq.md
Original file line number Diff line number Diff line change
@@ -1 +1,80 @@
# FAQ
# FAQ

```jsx
import React, { useEffect, useState } from "react";
import ReactDOM from "react-dom";
import { SchemaForm } from "@formily/antd"; // 或者 @formily/next
import { Input, Select, Radio } from "@formily/antd-components";
import Printer from "@formily/printer";
import "antd/dist/antd.css";

const mockSchema = {
type: "object",
properties: {
aa: {
type: "boolean",
enum: [
{ label: "设置bb的枚举列表", value: true },
{ label: "还原bb的枚举列表", value: false }
],
default: false,
title: "AA",
"x-component": "RadioGroup",
"x-linkages": [
{
type: "value:schema",
target: "bb",
condition: "{{!!$value}}",
schema: {
enum: ["xx1", "xx2", "xx3"]
},
otherwise: {
enum: ["zz"]
}
}
]
},
bb: {
type: "string",
title: "BB",
"x-component": "Input",
enum: ["yy"]
},
cc: {
type: "string",
title: "{{customCCTitle}}",
"x-component": "Input"
}
}
};

const App = () => {
const [schema, setSchema] = useState({
type: "object"
});
useEffect(() => {
setTimeout(() => {
setSchema(mockSchema);
}, 1000);
}, []);

return (
<Printer>
<SchemaForm
schema={schema}
components={{ Input, Select, RadioGroup: Radio.Group }}
onSubmit={values => {
console.log(values);
}}
expressionScope={{
customTitle: "this is custom title",
customCCTitle: "CC"
}}
/>
</Printer>
);
};

ReactDOM.render(<App />, document.getElementById("root"));

```
9 changes: 5 additions & 4 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ export function createForm<FieldProps, VirtualFieldProps>(
}

function notifyFormValuesChange() {
if (isFn(options.onChange)) {
if (isFn(options.onChange) && !state.state.unmounted) {
clearTimeout(env.onChangeTimer)
env.onChangeTimer = setTimeout(() => {
if (state.state.unmounted) return
options.onChange(clone(getFormValuesIn('')))
})
}
Expand Down Expand Up @@ -986,7 +987,7 @@ export function createForm<FieldProps, VirtualFieldProps>(
})
})
})
if (isFn(options.onReset)) {
if (isFn(options.onReset) && !state.state.unmounted) {
options.onReset()
}
heart.publish(LifeCycleTypes.ON_FORM_RESET, state)
Expand Down Expand Up @@ -1028,7 +1029,7 @@ export function createForm<FieldProps, VirtualFieldProps>(
// 增加onFormSubmitValidateFailed来明确结束submit的类型
heart.publish(LifeCycleTypes.ON_FORM_SUBMIT_VALIDATE_FAILED, state)
heart.publish(LifeCycleTypes.ON_FORM_SUBMIT_END, state)
if (isFn(options.onValidateFailed)) {
if (isFn(options.onValidateFailed) && !state.state.unmounted) {
options.onValidateFailed(validated)
}

Expand All @@ -1042,7 +1043,7 @@ export function createForm<FieldProps, VirtualFieldProps>(

let payload,
values = state.getState(state => clone(state.values))
if (isFn(onSubmit)) {
if (isFn(onSubmit) && !state.state.unmounted) {
try {
payload = await Promise.resolve(onSubmit(values))
heart.publish(LifeCycleTypes.ON_FORM_ON_SUBMIT_SUCCESS, payload)
Expand Down
6 changes: 4 additions & 2 deletions packages/react-schema-renderer/src/linkages/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export const useValueSchemaLinkageEffect = (scope?: any) =>
$target: innerState
}),
{
assign: true
assign: true,
arrayMerge:(target,source)=>source
}
)
})
Expand All @@ -25,7 +26,8 @@ export const useValueSchemaLinkageEffect = (scope?: any) =>
$target: innerState
}),
{
assign: true
assign: true,
arrayMerge:(target,source)=>source
}
)
})
Expand Down
6 changes: 4 additions & 2 deletions packages/react-schema-renderer/src/linkages/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export const useValueStateLinkageEffect = (scope?: any) =>
$target: innerState
}),
{
assign: true
assign: true,
arrayMerge: (target, source) => source
}
)
})
Expand All @@ -25,7 +26,8 @@ export const useValueStateLinkageEffect = (scope?: any) =>
$target: innerState
}),
{
assign: true
assign: true,
arrayMerge: (target, source) => source
}
)
})
Expand Down

0 comments on commit b928dc3

Please sign in to comment.