Skip to content

Button 按钮 ​

ewColorPickerButton 渲染「清空」与「确定」两个按钮。清空会还原默认色并关闭面板,确定会提交当前色并关闭面板。

项值
包@ew-color-picker/button
pluginNameewColorPickerButton
applyOrderPost
挂载点panelContainer → .ew-color-picker-bottom-row
访问实例core.plugins.ewColorPickerButton

配置项 ​

配置类型默认值说明
showButtonbooleantruefalse 时不渲染任何按钮
hasClearbooleantruefalse 时隐藏清空按钮
hasSurebooleantruefalse 时隐藏确定按钮
clearTextstring'清空'清空按钮文案
sureTextstring'确定'确定按钮文案
disabledbooleanfalse禁用两个按钮

hasClear / hasSure 在构造期生效;运行时切换请用 enableClear / enableSure / enableButtons。

实例方法 ​

ts
const button = picker.plugins.ewColorPickerButton
方法 / 属性签名说明
enableClear(enable = true) => void显示/隐藏清空按钮
enableSure(enable = true) => void显示/隐藏确定按钮
enableButtons(enable = true) => void同时控制两个按钮
hasClear / hasSureboolean(getter/setter)setter 会重新渲染
setClearText(text: string) => void修改清空文案
setSureText(text: string) => void修改确定文案
setDisabled(disabled: boolean) => void设置按钮 disabled 属性
updateOptions() => void重新读取配置并重绘(延后一帧以避开 ColorMode)
onClearColor() => void执行清空逻辑(防抖 100ms)
onSureColor() => void执行确定逻辑(防抖 100ms)
clearButton / sureButtonHTMLButtonElement | null按钮 DOM

点击行为 ​

清空(onClearColor) ​

  1. 读取 defaultColor:有值则还原为该颜色,否则置空;
  2. 同步 hsvaColor(无默认色时重置为 {h:0,s:100,v:100,a:1});
  3. core.setColor(resetColor);
  4. core.syncAllPlugins(resetColor) 同步全部插件 UI;
  5. core.hidePanel() 关闭面板;
  6. 调用 options.clear?.(picker);
  7. core.trigger('clear')。

确定(onSureColor) ​

  1. 调用 options.sure?.(color, picker);
  2. core.trigger('sure', color, picker);
  3. core.hidePanel() 关闭面板。

事件与回调 ​

来源签名触发时机
options.clear(picker) => void点击清空按钮
options.sure(color, picker) => void点击确定按钮
事件 clear()点击清空按钮
事件 sure(color, picker)点击确定按钮
ts
picker.on('sure', (color, instance) => console.log('确定:', color))
picker.on('clear', () => console.log('已清空'))

DOM 结构 ​

html
<div class="ew-color-picker-bottom-row">
  <div class="ew-color-picker-drop-btn-group">
    <button class="ew-color-picker-clear-btn ew-color-picker-drop-btn">清空</button>
    <button class="ew-color-picker-sure-btn ew-color-picker-drop-btn">确定</button>
  </div>
</div>
类名说明
.ew-color-picker-drop-btn-group按钮组
.ew-color-picker-drop-btn-group-multi / -single与 ColorMode 输入组并排时的布局调整
.ew-color-picker-clear-btn清空按钮
.ew-color-picker-sure-btn确定按钮
.ew-color-picker-drop-btn通用按钮样式

完整示例 ​

ts
const picker = new ewColorPicker({
  el: '#app',
  defaultColor: '#8b5cf6',
  clearText: '重置',
  sureText: '用这个颜色',
  clear: (instance) => {
    console.log('清空后颜色:', instance.getColor())
  },
  sure: (color, instance) => {
    console.log('确定颜色:', color)
    instance.destroy()
  },
})

相关 ​