winbox-ui-next
Version:
We Design Vue 2.0: A Vue.js 3 UI Library
123 lines (122 loc) • 3.33 kB
JavaScript
;
var vue = require("vue");
var seemly = require("seemly");
var ColorInputUnit = require("./ColorInputUnit.js");
var ColorInput = vue.defineComponent({
name: "ColorInput",
props: {
clsPrefix: {
type: String,
required: true
},
mode: {
type: String,
required: true
},
modes: {
type: Array,
required: true
},
showAlpha: {
type: Boolean,
required: true
},
value: {
type: String,
default: null
},
valueArr: {
type: Array,
default: null
},
onUpdateValue: {
type: Function,
required: true
},
onUpdateMode: {
type: Function,
required: true
}
},
setup(props) {
return {
handleUnitUpdateValue(index, value) {
const {
showAlpha
} = props;
if (props.mode === "hex") {
props.onUpdateValue((showAlpha ? seemly.toHexaString : seemly.toHexString)(value));
return;
}
let nextValueArr;
if (props.valueArr === null) {
nextValueArr = [0, 0, 0, 0];
} else {
nextValueArr = Array.from(props.valueArr);
}
switch (props.mode) {
case "hsv":
nextValueArr[index] = value;
props.onUpdateValue((showAlpha ? seemly.toHsvaString : seemly.toHsvString)(nextValueArr));
break;
case "rgb":
nextValueArr[index] = value;
props.onUpdateValue((showAlpha ? seemly.toRgbaString : seemly.toRgbString)(nextValueArr));
break;
case "hsl":
nextValueArr[index] = value;
props.onUpdateValue((showAlpha ? seemly.toHslaString : seemly.toHslString)(nextValueArr));
break;
}
}
};
},
render() {
const {
clsPrefix,
modes
} = this;
return vue.createVNode("div", {
"class": `${clsPrefix}-color-picker-input`
}, [vue.createVNode("div", {
"class": `${clsPrefix}-color-picker-input__mode`,
"onClick": this.onUpdateMode,
"style": {
cursor: modes.length === 1 ? "" : "pointer"
}
}, [this.mode.toUpperCase() + (this.showAlpha ? "A" : "")]), vue.createVNode(vue.resolveComponent("w-input-group"), {
"style": "width:100%;"
}, {
default: () => {
const {
mode,
valueArr,
showAlpha
} = this;
if (mode === "hex") {
let hexValue = null;
try {
hexValue = valueArr === null ? null : (showAlpha ? seemly.toHexaString : seemly.toHexString)(valueArr);
} catch {
}
return vue.createVNode(ColorInputUnit, {
"label": "HEX",
"showAlpha": showAlpha,
"value": hexValue,
"onUpdateValue": (unitValue) => {
this.handleUnitUpdateValue(0, unitValue);
}
}, null);
}
return (mode + (showAlpha ? "a" : "")).split("").map((v, i) => vue.createVNode(ColorInputUnit, {
"label": v.toUpperCase(),
"value": valueArr === null ? null : valueArr[i],
"onUpdateValue": (unitValue) => {
this.handleUnitUpdateValue(i, unitValue);
}
}, null));
}
})]);
}
});
module.exports = ColorInput;