ARTICLE DETAIL

资讯详情

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

wp-calypso 插件操作组件 PluginAction 深度指南:从开关切换到禁用态提示

wp-calypso 插件操作组件 PluginAction 深度指南:从开关切换到禁用态提示 前端CMS【免费下载链接】wp-calypsoThe JavaScript and API powered WordPress.com项目地址https://gitcode.com/gh_mirrors/wp/wp-calypso点击查看免费下载PluginAction 是 WordPress.com 客户端 wp-calypso 中用于展示插件操作插件激活/停用、自动更新开关、移除插件等的通用组件默认渲染一个开关Toggle也支持通过子元素完全自定义操作控件如断开 Jetpack按钮。读完本文你将掌握该组件的全部 Props、默认渲染与子元素覆盖两种用法以及它在插件激活开关、自动更新开关、移除按钮中的真实落地方式。组件定位与核心职责在 client/my-sites/plugins/plugin-action/README.md 中组件的定位被描述为This component is used to display a plugin action in the form of a toggle or a disconnect Jetpack button.即用开关toggle或断开 Jetpack按钮的形式展示一个可执行的插件操作。它把操作展示 进度状态 禁用原因解释封装成一个可复用的 UI 单元供插件管理页面的多个操作组件复用。组件源码位于 client/my-sites/plugins/plugin-action/plugin-action.jsx配套资源包括文件作用plugin-action.jsx组件主实现React Class Componentstyle.scss组件样式开关、标签、禁用提示弹层的布局test/index.jsxJest Testing Library 单元测试README.md使用文档与 Props 说明一个需要留意的细节README 提到默认会渲染FormToggle而当前源码plugin-action.jsx实际引入的是wordpress/components的ToggleControl。组件本身是一个无状态由父组件驱动的展示型组件真正的数据流插件状态、Redux 派发由外层操作组件负责。快速上手默认渲染开关Toggle不传任何子元素时PluginAction 会自动渲染一个开关控件。README 给出的最小用法如下import PluginAction from calypso/my-sites/plugins/plugin-action/plugin-action; function render() { return ( div classNameplugin-actions PluginAction label{ this.props.translate( Active, { context: plugin status } ) } status{ this.isActive() } action{ this.toggleActivation } inProgress{ this.props.activateInProgress } htmlForhtml-for-attribute-on-label / /div ); }对照源码 renderToggle()这套 Props 会被映射为ToggleControl onChange{ this.props.action } // 用户拨动开关时触发 checked{ this.props.status } // 开关的当前状态 disabled{ this.props.inProgress || this.props.disabled || !! this.props.disabledInfo } id{ this.props.htmlFor } // label 的 for 属性指向 label{ this.renderLabel() } aria-label{ this.props.label } __nextHasNoMarginBottom /其中disabled的判定非常关键只要满足「操作进行中inProgress」「显式禁用disabled」「存在禁用原因disabledInfo」三者之一开关就会被置灰且不可交互。toggleExtraContent会在开关下方追加额外内容例如自动更新开关旁的解释文案。覆盖默认行为通过子元素自定义操作控件当开关形态不满足需求例如要渲染一个跳转按钮、断开 Jetpack连接链接可以通过传入 children 覆盖默认渲染。README 的示例import { Button } from automattic/components; import PluginAction from calypso/my-sites/plugins/plugin-action/plugin-action; function render() { return ( PluginAction label{ this.props.translate( Active and Connected, { context: plugin status } ) } Button href/plugins/jetpack / /PluginAction ); }源码中的分流逻辑位于 renderInner()renderInner() { if ( 0 Children.count( this.props.children ) ) { return this.renderChildren(); } return this.renderToggle(); }只要检测到存在 children就进入 renderChildren()span classNameplugin-action__children { this.props.children } { this.renderLabel() } /span即自定义操作控件与操作标签plugin-action__label会被并排包裹在.plugin-action__children容器中。测试 test/index.jsx 明确验证了这一点存在 children 时绝不渲染 toggletoggle-control不出现且 children 的父节点必须带有plugin-action__childrenclass。Props 全解析README 列出了 7 个核心 Props结合源码还可以补充hideLabel、toggleExtraContent、className三个实际支持的属性。完整对照如下Props类型说明源码位置labelstring/ReactNode描述该操作的用户友好文案如 Active、AutoupdatesrenderLabel()statusbool进度指示器的状态映射为ToggleControl的checkedrenderToggle()actioncallback用户触发操作时要执行的函数onChange/ 标签点击inProgressbool操作是否正在执行中为 true 时开关被禁用renderToggle()htmlForstring用于生成 label 的for属性绑定开关idrenderToggle()disabledInfostring/ReactNode禁用原因文本渲染为一个 InfoPopover 解释弹层renderDisabledInfo()disabledbool是否禁用置灰且不可交互根节点 class 与 ToggleControlhideLabelbool隐藏标签文本通过.hideclass 实现renderLabel()toggleExtraContentReactNode追加在开关下方的额外内容renderToggle()classNamestring追加到根节点.plugin-action的额外 classrender()注意status与action是受控组件约定父组件负责维护开关状态并传入回调PluginAction 自身不持有状态因此状态更新、Redux 派发、埋点上报都由父组件完成。点击分流逻辑为什么禁用时会弹出解释PluginAction 最有价值的设计在点击处理 handleAction()handleAction ( event ) { if ( ! this.props.disabledInfo ) { this.props.action(); } else { this.infoPopover.handleClick( event ); } };没有disabledInfo正常触发action回调有disabledInfo不执行操作改为弹出 InfoPopover向用户解释为什么这个操作不可用。禁用的原因展示由 renderDisabledInfo() 实现使用calypso/components/info-popover的InfoPopover定位在bottom leftGA 事件类别为Plugins并通过ignoreContext让标签点击不会误触发关闭。标签区.plugin-action__label同样绑定了handleAction因此点击文案也能拨动开关或弹出解释。根节点还会根据禁用状态追加样式 classrender()const additionalClasses { is-disabled: this.props.disabled, has-disabled-info: !! this.props.disabledInfo, };样式层面style.scss.is-disabled下标签文字变为中性灰--color-neutral-20且cursor: default.has-disabled-info下标签改为cursor: default并调整 flex 方向为信息图标腾出位置.plugin-action__disabled-info负责禁用解释图标与弹层的 z-index、间距细节.is-warning变体可将标签染成错误色--color-error。实战案例三个真实使用场景PluginAction 不是孤立组件插件管理页面的三个操作组件都直接复用了它相关目录均在 client/my-sites/plugins 下。1. 激活/停用开关plugin-activate-toggleclient/my-sites/plugins/plugin-activate-toggle/index.jsx 是 README 示例的原型实现PluginAction disabled{ disabled || isJetpackPlugin } classNameplugin-activate-toggle label{ translate( Active, { context: plugin status } ) } inProgress{ inProgress } status{ plugin.active } action{ this.toggleActivation } htmlFor{ activate- plugin.slug - site.ID } hideLabel{ hideLabel } /status直接取plugin.active开关状态即插件激活状态toggleActivation内部调用 Redux actiontogglePluginActivation( site.ID, plugin )并派发removePluginStatuses清理旧状态同时上报 GA 与 Tracks 埋点inProgress来自 Redux selectorisPluginActionInProgress监听ACTIVATE_PLUGIN / DEACTIVATE_PLUGIN两个动作特殊分支非 Jetpack Cloud 环境下遇到jetpack插件时改用 children 形式传入Manage Connection齿轮图标 管理连接链接这正是 README 提到的 disconnect Jetpack 场景。2. 自动更新开关plugin-autoupdate-toggleclient/my-sites/plugins/plugin-autoupdate-toggle/index.jsx 展示了disabledInfo与toggleExtraContent的用法PluginAction disabled{ this.isAutoManaged() ? true : disabled } label{ label || defaultLabel } classNameplugin-autoupdate-toggle status{ this.isAutoManaged() ? true : plugin.autoupdate } action{ this.toggleAutoUpdates } inProgress{ inProgress } disabledInfo{ getDisabledInfo } htmlFor{ autoupdates- plugin.slug - site.ID } hideLabel{ hideLabel } toggleExtraContent{ toggleExtraContent } /getDisabledInfo()是disabledInfo的典型生产者当插件不在 WordPress.org 仓库、站点属于多网络multi-network安装、非主站main network site、或文件修改被禁用时返回对应的本地化解释文案多个原因时渲染为ul classNameplugin-action__disabled-info-list列表并附上How do I fix this?外部帮助链接。这些文案会原样进入 InfoPopover 展示。3. 移除按钮plugin-remove-buttonclient/my-sites/plugins/plugin-remove-button/index.jsx 展示了 children 覆盖与disabledInfo的组合PluginAction htmlFor{ remove-plugin- this.props.site.ID } action{ this.removeAction } disabled{ disabled } disabledInfo{ disabledInfo } classNameplugin-remove-button__remove-link Button onClick{ handleClick } classNameplugin-remove-button__remove-button Icon icon{ trash } classNameplugin-remove-button__remove-icon / { label } /Button /PluginAction这里传入自定义Button作为操作控件操作进行中标签会变为Removing…同时disabledInfo负责解释多网络、非主站、文件修改被禁用等导致无法移除的原因。注意handleClick在禁用时被置为null保证不可交互。行为验证测试用例导读test/index.jsx 以 Jest Testing Libraryjsdom 环境覆盖了核心契约默认渲染无 children 时根节点带plugin-actionclass并出现 checkboxToggleControl被 mock 为input typecheckboxchildren 覆盖有 children 时不渲染 togglechildren 与 label 均存在于.plugin-action__children容器中label 带plugin-action__label-textclass测试用jest.mock将InfoPopover替换为空组件、将ToggleControl替换为 checkbox聚焦于组件自身的渲染分支逻辑。小结PluginAction 是 wp-calypso 插件管理 UI 中操作入口的统一抽象默认渲染ToggleControl开关传入 children 即切换为自定义控件inProgress、disabled、disabledInfo三重判定统一管理可交互性禁用原因通过 InfoPopover 即时解释。理解这个组件也就理解了插件激活开关、自动更新开关、移除按钮等页面元素背后的共用逻辑与交互约定。若要在此基础上新增插件操作入口如启用/停用之外的动作直接复用 plugin-action.jsx 并参照上述三个实战组件组织状态与埋点即可。赞分享前端CMS【免费下载链接】wp-calypsoThe JavaScript and API powered WordPress.com项目地址https://gitcode.com/gh_mirrors/wp/wp-calypso点击查看免费下载相关推荐wp-calypso 组件体系深度指南从 UI 组件、子组件到样式与复用规范wp calypso 组件体系深度指南从 UI 组件、子组件到样式与复用规范 导读 本文基于 wp calypso 仓库中 client/components前端CMSwp-calypso LinkCard 组件使用指南从 Props 详解到插件市场实战wp calypso LinkCard 组件使用指南从 Props 详解到插件市场实战 LinkCard 是 wp calypsoWordPress.com前端CMSwp-calypso Plugin Autoupdate Toggle 组件指南为插件自动更新开关赋能wp calypso Plugin Autoupdate Toggle 组件指南为插件自动更新开关赋能 导读 PluginAutoupdateToggle 是前端CMS上一篇shadcn-vue Form 组件实战基于 VeeValidate 与 Zod 构建可访问、类型安全的表单下一篇Leela Zero终极网络权重转换指南轻松兼容主流深度学习框架创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表