-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(table): add new api actionRef.saveEditable #6081
Conversation
🎊 PR Preview has been successfully built and deployed to https://pro-components-preview-pr-6081.surge.sh |
Codecov ReportBase: 98.55% // Head: 98.80% // Increases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## master #6081 +/- ##
==========================================
+ Coverage 98.55% 98.80% +0.25%
==========================================
Files 320 268 -52
Lines 9731 7896 -1835
Branches 3282 2724 -558
==========================================
- Hits 9590 7802 -1788
+ Misses 134 94 -40
+ Partials 7 0 -7
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
This pull request fixes 1 alert when merging 0aafa3c into 7a38415 - view on LGTM.com fixed alerts:
|
This pull request fixes 1 alert when merging 99f8a78 into 7a38415 - view on LGTM.com fixed alerts:
|
没看懂你的新用法 |
@chenshuai2144 可能文字描述不直观,我加了一段示例代码,手动保存编辑行,比调用 import type { FC, RefObject } from 'react';
import { useRef } from 'react';
import { Button } from 'antd';
import { EditableProTable } from '@ant-design/pro-table';
import type { EditableFormInstance } from '@ant-design/pro-table/es/components/EditableTable';
import type { SaveEditableActionRef } from '@ant-design/pro-utils/es/useEditableArray';
const AdvancedProperties: FC = () => {
const editableRef = useRef<EditableFormInstance>();
const saveActionRef = useRef<RefObject<SaveEditableActionRef>>();
return (
<div>
<Button
onClick={async () => {
// validate editing row and save
await editableRef.current.validateFields();
await saveActionRef.current.current.save(); // NEW API: saveRef.save
/* then continue submitting the form data... */
}}
>
Save row and submit
</Button>
<EditableProTable
editableFormRef={editableRef}
editable={{
actionRender: (row, config, defaultDom) => {
saveActionRef.current = defaultDom.saveRef; // NEW API: saveRef
return [defaultDom.save, defaultDom.cancel];
},
}}
/>
</div>
);
}; |
这个写法也太离谱了 |
@chenshuai2144 这样设计API怎么样? import type { FC, Key } from 'react';
import { useState, useRef } from 'react';
import { Button } from 'antd';
import type { ActionType } from '@ant-design/pro-table';
import { EditableProTable } from '@ant-design/pro-table';
import type { EditableFormInstance } from '@ant-design/pro-table/es/components/EditableTable';
const AdvancedProperties: FC = () => {
const editableRef = useRef<EditableFormInstance>();
const [editableKeys, setEditableKeys] = useState<Key[]>([]);
const actionRef = useRef<ActionType>();
return (
<div>
<Button
onClick={async () => {
// validate editing row and save
await editableRef.current.validateFields();
await actionRef.current.saveRecord(editingKeys[0]); // NEW API: actionRef.saveRecord
/* then continue submitting the form data... */
}}
>
Save row and submit
</Button>
<EditableProTable
editableFormRef={editableRef}
actionRef={actionRef}
editable={{
editableKeys,
onChange(keys) {
setEditableKeys(keys);
},
}}
/>
</div>
);
}; |
重新实现了一下,补了一个单元测试 |
saveRecord 和 save 的区别是什么? |
第一个 基于 |
a7db9cf
to
9eff843
Compare
现在 actionRef 上开了什么东西出来,我记得好像有个差不多的 |
跟EditableTable相关的是这几个 @chenshuai2144 {
isEditable,
startEditable,
cancelEditable,
addEditRecord,
preEditableKeys,
onValuesChange
} |
9ec5dee
to
816df7b
Compare
我把方法名改成 @chenshuai2144 麻烦review下吧,看是否需要merge进去?
|
autoSaveEditable 这个有点hack了,让用户手动调用 saveEditable 吧 |
覆盖率降低了 提一下 |
done |
但是我发现使用这种方法触发不了onSave方法
|
[EditableTable]
editable
对象actionRender
方法的defaultDoms
参数对每个action按钮都扩展了一个ref对象,可以让调用者以编程的方式直接触发对应的action。相较于新的api,调用者只能调用config对象的
onSave/onDelete/onCancel
方法,但需要传递很多参数(尤其是onSave),需要了解很多内部知识,自己需要用form来获取整个编辑行的数据,门槛太高。而新的方法,内部获取所有信息,无需传参。应用场景:
如果页面上存在多个tab,或者编辑表格会动态显示/隐藏,如果编辑行没有保存就被隐藏了,那最后保存表单时这一行就会丢失掉。借助新方法,调用者可以在隐藏表格之前自动保存编辑行,避免数据丢失的问题。