Skip to content

Commit

Permalink
refactor(Form): 删除了Form.Block功能
Browse files Browse the repository at this point in the history
  • Loading branch information
milobluebell committed Sep 10, 2021
1 parent 83fb389 commit 38f849c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 85 deletions.
2 changes: 1 addition & 1 deletion packages/mlz-admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"dependencies": {
"@babel/runtime": "^7.10.2",
"antd": "^4.16.8",
"antd": "^4.17.0",
"lodash": "^4.17.21",
"mytils": "^1.3.3"
},
Expand Down
86 changes: 2 additions & 84 deletions packages/mlz-admin/src/form/index.tsx
Original file line number Diff line number Diff line change
@@ -1,84 +1,2 @@
import React, { useState, useEffect, useMemo } from 'react';
import { Form as AntdForm, Spin, Button, Row } from 'antd';
import { NamePath } from 'rc-field-form/lib/interface';
import { IFormProps, IFormColumnType, IDependencyItem, CompoundedForm } from './index.type';

const Form = AntdForm as CompoundedForm;
Form.Block = (props: IFormProps) => {
const { loading, initialValues, rows, style, onFinish } = props;
const [form] = AntdForm.useForm();
const [forceRefresh, toggleForceRefresh] = useState(false);
// 所有表单项的所有依赖内容
const allRelyOnKeys = useMemo(() => {
return Array.from(
new Set(
rows?.reduce((prev: unknown[], curr: IFormColumnType) => {
const currRelyOnKeys = curr?.relyOn?.reduce((prv: NamePath[], cur: IDependencyItem) => prv.concat([cur?.name] || []), []);
return prev.concat(currRelyOnKeys || []);
}, []),
),
);
}, [rows]);

const hasSettled = initialValues && Object.keys(initialValues).length;
useEffect(() => {
if (hasSettled) {
form.setFieldsValue(initialValues);
toggleForceRefresh(!forceRefresh);
}
}, [initialValues]);

return (
<Spin spinning={Boolean(loading)}>
<AntdForm
onFieldsChange={(changedValues) => {
// ⚠️ DO NOT RETURN, JUST void
if (changedValues.length && allRelyOnKeys.includes(changedValues[0]?.name?.[0])) {
toggleForceRefresh(!forceRefresh);
}
}}
{...{ form, style, props }}>
{rows.map((item: IFormColumnType) => {
const { relyOn, name, label, render } = item;
const showItem = relyOn ? relyOn?.some((relyOnItem: IDependencyItem) => relyOnItem.toContain?.includes(form.getFieldValue(relyOnItem.name))) : true;
const component = typeof render === 'function' ? render() : render;
return showItem ? (
<AntdForm.Item key={name.toString()} name={name} label={label} labelAlign="left" preserve={false} {...item.itemProps}>
{component}
</AntdForm.Item>
) : null;
})}
<Row justify="end" {...props.submitterProps}>
{props.resetText ? (
<Button
htmlType="reset"
className="form-block-reset-btn"
style={{ marginRight: 8 }}
onClick={() => {
if (hasSettled) {
const resettedValues = {};
Object.keys(initialValues || {}).forEach((key) => {
resettedValues[key] = '';
});
form.setFieldsValue(resettedValues);
} else {
form.resetFields();
}
toggleForceRefresh(!forceRefresh);
props.onReset?.(initialValues || {});
}}>
重置
</Button>
) : null}
{props.confirmText ? (
<Button type="primary" htmlType="submit" className="form-block-submit-btn">
提交
</Button>
) : null}
</Row>
</AntdForm>
</Spin>
);
};

export default Form;
import React from 'react';
export { Form as default } from 'antd';

0 comments on commit 38f849c

Please sign in to comment.