Skip to content

ColorPicker 颜色选择框

规则

基础示例

js
const rule = {
    type: "ColorPicker",
    field: "color",
    title: "颜色",
    value: '#ff7271',
}

Props 配置示例

显示颜色预览

js
const rule = {
    type: "ColorPicker",
    field: "color",
    title: "主题颜色",
    value: '#ff7271',
    props: {
        showPreview: true,
        showAlpha: true,
    }
}

自定义色板

js
const rule = {
    type: "ColorPicker",
    field: "color",
    title: "颜色",
    value: '#ff7271',
    props: {
        swatches: [
            '#FF6B6B',
            '#4ECDC4',
            '#45B7D1',
            '#FFA07A',
            '#98D8C8',
        ],
    }
}

禁用透明度

js
const rule = {
    type: "ColorPicker",
    field: "color",
    title: "颜色",
    value: '#ff7271',
    props: {
        showAlpha: false,
    }
}

不同尺寸

js
const rule = {
    type: "ColorPicker",
    field: "color",
    title: "颜色",
    value: '#ff7271',
    props: {
        size: "large",
    }
}

Events 事件示例

监听颜色变化

js
const rule = {
    type: "ColorPicker",
    field: "color",
    title: "颜色",
    value: '#ff7271',
    props: {
        showPreview: true,
    },
    on: {
        'update:value': (value) => {
            console.log('颜色改变:', value);
        },
        complete: (value) => {
            console.log('颜色选择完成:', value);
        },
    },
}

颜色选择后联动更新

js
const rule = [
    {
        type: "ColorPicker",
        field: "primaryColor",
        title: "主题色",
        value: '#ff7271',
        props: {
            showPreview: true,
        },
        inject: true,
        on: {
            complete: ($inject, value) => {
                // 根据主题色自动生成辅助色
                const secondaryColor = lightenColor(value, 0.2);
                $inject.api.setValue('secondaryColor', secondaryColor);
            },
        },
    },
    {
        type: "ColorPicker",
        field: "secondaryColor",
        title: "辅助色",
        value: '#ff9999',
        props: {
            showPreview: true,
            disabled: true,
        },
    },
]

完整配置项:naive-ui_ColorPicker

value :String

Props

名称类型默认值说明版本
default-showbooleanundefined默认是否展示弹出层
default-valuestring | null和第一个 mode 对应的黑色值默认的颜色值
modesArray<'rgb' | 'hex' | 'hsl' | 'hsv'>['rgb', 'hex', 'hsl']颜色选择器支持颜色的格式,注意一旦你在某个模式下选择了值,颜色选择器值的格式将跟随这个格式
placement'top-start' | 'top' | 'top-end' | 'right-start' | 'right' | 'right-end' | 'bottom-start' | 'bottom' | 'bottom-end' | 'left-start' | 'left' | 'left-end' | 'bottom-start'面板的弹出位置2.25.0
render-label(color: string | null) => VNodeChildundefined触发器的内容2.24.0
showbooleanundefined是否展示面板
show-alphabooleantrue是否可调节 alpha 通道
show-previewbooleanfalse是否展示颜色预览块
size'small' | 'medium' | 'large''medium'颜色选择器的尺寸
disabledbooleanfalse是否禁用2.24.5
swatchesstring[]undefined色板的值
tostring | HTMLElement'body'面板的卸载位置
valuestring | nullundefined颜色选择器的值
on-complete(value: string) => voidundefined颜色完成改变后的回调(在鼠标移动时候不会调用)
on-update:show(value: boolean) => voidundefined面板可见状态改变的回调
on-update:value(value: string) => voidundefined颜色改变时的回调
actionsArray<'confirm'> | nullnull显示按钮

FormCreate 是一个开源项目,基于 MIT 许可证发布,欢迎个人和企业用户免费使用