十年匠心定制 · 商业建站与技术教学双线并行 咨询热线:400-886-1026 service@lmnt.cn
ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Refine v5 中的 Ant Design `<AutoSaveIndicator />`:为管理后台表单构建可视化自动保存状态

Refine v5 中的 Ant Design `<AutoSaveIndicator />`:为管理后台表单构建可视化自动保存状态 Refine v5 中的 Ant DesignAutoSaveIndicator /为管理后台表单构建可视化自动保存状态【免费下载链接】refineA React Framework for building internal tools, admin panels, dashboards B2B apps with unmatched flexibility.项目地址: https://gitcode.com/GitHub_Trending/re/refine本指南围绕 Refine 的Ant Design 集成包refinedev/antd中的AutoSaveIndicator /组件展开讲解如何把它接入useForm的自动保存auto-save流程向用户直观呈现保存中 / 已保存 / 保存失败 / 等待修改四种状态。读完本文你将掌握该组件的接入方式、autoSaveProps数据契约、内置的 Ant Design 样式实现原理以及如何通过elements属性深度自定义各状态下的展示内容。组件定位Ant Design 风格的核心组件扩展AutoSaveIndicator /是 Refine 核心包refinedev/core中同名组件的扩展实现专为 Ant Design 生态打磨它复用了核心组件的状态判断逻辑但把默认展示元素替换为与 Ant Design 组件库和主题体系一致的元素packages/antd/src/components/autoSaveIndicator/index.tsx。从源码结构看Ant Design 版本的组件本身不包含任何自动保存触发逻辑它接收status、error、data以及可选的elements将其透传给核心组件完成渲染// packages/antd/src/components/autoSaveIndicator/index.tsx节选 return ( AutoSaveIndicatorCore status{status} elements{{ success, error, loading, idle, }} / );核心组件则根据status用switch分发渲染对应的元素packages/core/src/components/autoSaveIndicator/index.tsxswitch (status) { case success: return {success}/; case error: return {error}/; case pending: return {loading}/; case idle: return {idle}/; default: return {idle}/; }注意核心组件内部用status pending来匹配保存中状态而autoSaveProps对外暴露的状态注释为loading | error | idle | success二者语义对应接入时无需关心这一内部细节。快速接入从useForm拿到autoSaveProps接入方式非常简单从refinedev/antd的useForm返回对象中取出autoSaveProps通过展开运算符spread传给AutoSaveIndicator /即可import { AutoSaveIndicator, useForm } from refinedev/antd; const MyComponent () { const { autoSaveProps } useForm({ refineCoreProps: { autoSave: { enabled: true, }, }, }); console.log(autoSaveProps); /* { status: success, // loading | error | idle | success error: null, // HttpError | null data: { ... }, // UpdateResponse | undefined, } */ return AutoSaveIndicator {...autoSaveProps} /; };需要特别说明的是refinedev/antd的useForm与核心useForm在参数结构上的差异Ant Design 版useForm中自动保存配置需要放在refineCoreProps.autoSave下而核心包的useForm直接使用autoSave顶层选项参见 documentation/docs/core/components/auto-save-indicator/index.md。在refinedev/antd的useForm中开启autoSave.enabled: true后表单值发生变化时会自动触发自动保存而核心包的useForm并不会自动触发需要手动调用onFinishAutoSave。理解autoSaveProps的数据契约autoSaveProps的类型定义位于 packages/core/src/hooks/form/types.ts它实际上是对内部useUpdate变更mutation返回值的抽取export type AutoSaveReturnType TData extends BaseRecord BaseRecord, TError extends HttpError HttpError, TVariables {}, { autoSaveProps: Pick UseUpdateReturnTypeTData, TError, TVariables[mutation], data | error | status ; onFinishAutoSave: ( values: TVariables, ) PromiseUpdateResponseTData | void; };三个字段的语义如下字段类型含义statusloading \| error \| idle \| success当前自动保存请求的状态errorHttpError \| null自动保存请求失败时返回的错误对象成功时为nulldataUpdateResponse \| undefined自动保存更新请求成功后的响应数据未完成时为undefined正因为autoSaveProps是对useUpdatemutation 状态的直接映射AutoSaveIndicator /的对应 propsdata、error、status也复用了UseUpdateReturnType中的类型保证类型在端到端传递时始终一致packages/core/src/components/autoSaveIndicator/index.tsx#L9-L31。内置四态样式Ant Design 主题与图标体系Ant Design 版本为四种状态提供了开箱即用的默认元素每个元素都是一个封装好的Message组件由翻译文本 图标构成packages/antd/src/components/autoSaveIndicator/index.tsx#L15-L46状态默认文本translationKey / 默认值默认图标successautoSave.success/ savedCheckCircleOutlined成功对勾errorautoSave.error/ auto save failureExclamationCircleOutlined异常感叹号loadingautoSave.loading/ saving...SyncOutlined旋转同步图标idleautoSave.idle/ waiting for changesEllipsisOutlined省略号这些默认元素在样式上完全对齐 Ant Design 设计语言文本使用Typography.Text渲染颜色取自主题 tokencolorTextTertiary即通过theme.useToken()获取的次要文本色字号为0.8rem图标与文本之间保持0.2rem间距文本右侧保留5px外边距。// packages/antd/src/components/autoSaveIndicator/index.tsx节选 const { token } theme.useToken(); return ( Typography.Text style{{ marginRight: 5, color: token.colorTextTertiary, fontSize: .8rem, }} {translate(translationKey, defaultMessage)} span style{{ marginLeft: .2rem }}{icon}/span /Typography.Text );文本通过useTranslate()读取因此当你为应用配置了 i18n 资源时默认文案会随语言环境自动切换未命中翻译 key 时则回退到上述默认英文文案packages/antd/src/components/autoSaveIndicator/index.tsx#L61-L85。自定义各状态展示elements属性默认四态样式适合多数场景但如果你希望展示更贴合业务语义的文案、插入加载动画或自定义组件可以通过elements属性覆盖任意一个或多个状态import { AutoSaveIndicator, useForm } from refinedev/antd; const MyComponent () { const { autoSaveProps } useForm({ refineCoreProps: { autoSave: { enabled: true }, }, }); return ( AutoSaveIndicator {...autoSaveProps} elements{{ loading: span正在保存.../span, error: span自动保存失败请检查网络。/span, idle: span等待修改.../span, success: span已保存。/span, }} / ); };elements的类型为PartialRecordsuccess | error | loading | idle, ReactNodepackages/core/src/hooks/form/types.ts#L63-L65也就是说你只需覆盖需要变更的状态其余状态仍使用 Ant Design 默认元素——每个属性在解构时都带有默认值兜底。底层机制auto-save 配置与防抖原理要真正用好AutoSaveIndicator /理解它背后autoSave选项的行为同样重要。autoSave的配置类型定义在 packages/core/src/hooks/form/types.ts#L39-L47export type AutoSavePropsTVariables { autoSave?: { enabled: boolean; debounce?: number; onFinish?: (values: TVariables) TVariables; invalidateOnUnmount?: boolean; invalidateOnClose?: boolean; }; };配置项类型默认值作用enabledboolean—是否启用自动保存debouncenumber1000毫秒输入变化后延迟多少毫秒再触发保存onFinish(values) values—提交前的值转换/预处理钩子invalidateOnUnmountboolean—组件卸载时是否使相关查询失效invalidateOnCloseboolean—表单关闭时是否使相关查询失效从 packages/core/src/hooks/form/index.ts#L310-L319 可以看到自动保存触发函数onFinishAutoSave由asyncDebounce包装默认防抖时长为1000msconst onFinishAutoSave React.useMemo( () asyncDebounce( (values: TVariables) onFinishRef.current(values, { isAutosave: true }), props.autoSave?.debounce ?? 1000, Cancelled by debounce, ), [props.autoSave?.debounce], );这解释了组件展示上的一个体验细节当用户连续快速编辑时status会因防抖与请求周期在loading、success、idle之间切换AutoSaveIndicator /正是负责把这一过程实时、直观地呈现给用户。另外组件卸载时会调用onFinishAutoSave.cancel()取消尚未执行的防抖任务packages/core/src/hooks/form/index.ts#L321-L325。测试验证四态渲染由共享测试套件保障Refine 通过refinedev/ui-tests提供了跨 UI 集成包共享的组件测试AutoSaveIndicator /的四态渲染均有测试覆盖packages/ui-tests/src/tests/autoSaveIndicator.tsxstatussuccess时渲染文本 savedstatuserror时渲染文本 auto save failurestatusidle时渲染文本 waiting for changesstatuspending时渲染文本 saving...。Ant Design 版本的测试直接绑定这套共享套件packages/antd/src/components/autoSaveIndicator/index.spec.tsximport { autoSaveIndicatorTests } from refinedev/ui-tests; import { AutoSaveIndicator } from ./; describe(AutoSaveIndicator, () { autoSaveIndicatorTests.bind(this)(AutoSaveIndicator); });这意味着只要传入合法的status无论你使用的是核心组件还是 Ant Design 扩展组件四态渲染行为都有一致性保障。使用注意事项autoSave仅支持编辑edit场景核心useForm在非 edit action 下启用自动保存会输出警告[useForm]: autoSave is only allowed in edit actionpackages/core/src/hooks/form/index.ts#L364。因此在 create/clone 页面上不要期望自动保存生效。组件只负责展示AutoSaveIndicator /本身不包含任何触发保存的逻辑它纯粹根据autoSaveProps.status渲染反馈触发行为由useForm的 auto-save 机制onFinishAutoSave 防抖完成。需要 i18n 资源时自行补充默认文案通过useTranslate读取autoSave.success/error/loading/idle四个 key未配置对应语言包时回退到英文默认值。小结AutoSaveIndicator /Ant Design以极低的接入成本为 Refine 表单的自动保存能力补齐了用户可感知的最后一块拼图useForm负责在表单值变化后防抖自动保存并产出autoSaveProps组件负责把loading / success / error / idle四种状态映射为符合 Ant Design 设计语言的图标与文案。若需要进一步了解自动保存机制的完整设计可继续阅读核心包的 Auto Save 指南 与 核心版AutoSaveIndicator /文档。【免费下载链接】refineA React Framework for building internal tools, admin panels, dashboards B2B apps with unmatched flexibility.项目地址: https://gitcode.com/GitHub_Trending/re/refine创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表