Skip to content

Commit

Permalink
feat: 添加 type
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyair committed Apr 16, 2020
1 parent 6909059 commit 2e627ef
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ nav:
order: 5
---

## 0.5.6

`2020-04-16`

- `onCancel` 添加 `type` 参数

## 0.5.5

`2020-04-14`
Expand Down
9 changes: 8 additions & 1 deletion docs/examples/demo/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ const layout = { labelCol: { span: 4 }, wrapperCol: { span: 20 } };
YForm.Config({
getScene: {
search: {
form: ({ formProps }) => ({ ...formProps, required: false }),
form: ({ formProps }) => ({
formProps: {
...formProps,
required: false,
// 搜索成功后不重置表单
onCancel: () => {},
},
}),
items: ({ itemsProps }) => {
return {
itemsProps: { noStyle: true, ...itemsProps },
Expand Down
2 changes: 1 addition & 1 deletion packages/yform/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "father-doc-yform",
"version": "0.5.5",
"version": "0.5.6",
"description": "自定义表单组件",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
33 changes: 18 additions & 15 deletions packages/yform/src/YForm/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export interface YFormProps extends FormProps, YFormConfig {
children?: YFormItemProps['children'];
onSave?: (values: { [key: string]: any }) => void;
submitComponentProps?: YFormSubmitProps;
onCancel?: () => void;
onCancel?: (p: { type: 'onSave' | 'onSubmit' | 'onCancel' }) => void;
params?: ParamsType;
scene?: string;
}
Expand Down Expand Up @@ -124,18 +124,21 @@ const InternalForm = (props: YFormProps) => {
window.history.back();
};

const handleReset = useCallback(() => {
if (typeof onCancel === 'function') {
onCancel();
} else {
resetFields();
if (create) {
goBack();
} else if (edit || view) {
setDisabled(true);
const handleReset: YFormProps['onCancel'] = useCallback(
({ type }) => {
if (typeof onCancel === 'function') {
onCancel({ type });
} else {
resetFields();
if (create) {
goBack();
} else if (edit || view) {
setDisabled(true);
}
}
}
}, [create, edit, onCancel, resetFields, view]);
},
[create, edit, onCancel, resetFields, view],
);
const _itemsTypeAll = {
...baseItemsType,
...itemsType,
Expand All @@ -153,7 +156,7 @@ const InternalForm = (props: YFormProps) => {
timeOut.current = window.setTimeout(
() => {
setSubmitLoading(false);
handleReset();
handleReset({ type: 'onSubmit' });
},
// loading 时间不到 0.5s 的加载 0.5s,超过的立刻结束。
end - begin > 500 ? 0 : 500,
Expand Down Expand Up @@ -187,8 +190,8 @@ const InternalForm = (props: YFormProps) => {
// form submit 触发后设置 loading = true
showSubmit: { loading: submitLoading },
showEdit: { onClick: handleOnEdit },
showCancel: { onClick: handleReset },
showSave: { onLoaded: handleReset },
showCancel: { onClick: () => handleReset({ type: 'onCancel' }) },
showSave: { onLoaded: () => handleReset({ type: 'onSave' }) },
showBack: { onClick: goBack },
},
},
Expand Down

1 comment on commit 2e627ef

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for website ready!

Built with commit 2e627ef

https://father-doc-yform-56x0ijjbf.now.sh

Please sign in to comment.