-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83fb389
commit 38f849c
Showing
2 changed files
with
3 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |