Commit 0aefe4aa091b88db6c36febde4b5d301fbb74b42

Authored by 梁灏
1 parent e7893a68

update ColorPicker

examples/routers/color-picker.vue
... ... @@ -4,11 +4,12 @@
4 4 <!--<Input placeholder="请输入..." size="large" style="width: 50px;"></Input>-->
5 5 <!--<color-picker placement="bottom-start" size="large"></color-picker>-->
6 6 <!--<Date-picker type="date" placeholder="选择日期" size="large" style="width: 200px"></Date-picker>-->
7   - <color-picker v-model="color" alpha :recommend="true" placement="bottom" size="default"></color-picker>
  7 + <color-picker ref="xxx" v-model="color" alpha :recommend="true" placement="bottom" size="default"></color-picker>
8 8 <color-picker v-model="color" :alpha="false" :recommend="false" placement="bottom" size="default"></color-picker>
9 9 <!--<Date-picker type="date" placeholder="选择日期" style="width: 200px"></Date-picker>-->
10 10 <!--<color-picker placement="bottom-start" size="small"></color-picker>-->
11 11 <!--<Date-picker type="date" placeholder="选择日期" size="small" style="width: 200px"></Date-picker>-->
  12 + <Button @click="do2">do2</Button>
12 13 </div>
13 14 </template>
14 15 <script>
... ... @@ -16,31 +17,14 @@
16 17 props: {},
17 18 data () {
18 19 return {
19   - color: {
20   - hex: '#194d33',
21   - hsl: {
22   - h: 150,
23   - s: 0.5,
24   - l: 0.2,
25   - a: 1
26   - },
27   - hsv: {
28   - h: 150,
29   - s: 0.66,
30   - v: 0.30,
31   - a: 1
32   - },
33   - rgba: {
34   - r: 25,
35   - g: 77,
36   - b: 51,
37   - a: 1
38   - },
39   - a: 1
40   - }
  20 + color: '#ff4290'
41 21 };
42 22 },
43 23 computed: {},
44   - methods: {}
  24 + methods: {
  25 + do2 () {
  26 + this.$refs.xxx.do2();
  27 + }
  28 + }
45 29 };
46 30 </script>
47 31 \ No newline at end of file
... ...
src/components/color-picker/color-picker.vue
1 1 <template>
2   - <Dropdown trigger="click" :transfer="transfer" :placement="placement">
  2 + <Dropdown trigger="click" :transfer="transfer" :placement="placement" @on-visible-change="handleToggleVisible">
3 3 <div :class="wrapClasses">
4 4 <i class="ivu-icon ivu-icon-arrow-down-b ivu-input-icon ivu-input-icon-normal"></i>
5 5 <div :class="inputClasses">
6   - <div :class="[prefixCls + '-color']" style="background-color: rgb(32, 160, 255);"></div>
  6 + <div :class="[prefixCls + '-color']">
  7 + <div :style="{backgroundColor: displayedColor}"></div>
  8 + </div>
7 9 </div>
8 10 </div>
9 11 <Dropdown-menu slot="list">
... ... @@ -19,7 +21,7 @@
19 21 </div>
20 22 <recommend-colors v-if="colors.length" :list="colors" :class="[prefixCls + '-picker-colors']"></recommend-colors>
21 23 <recommend-colors v-if="!colors.length && recommend" :list="recommendedColor" :class="[prefixCls + '-picker-colors']"></recommend-colors>
22   - <Confirm></Confirm>
  24 + <Confirm @on-pick-success="handleSuccess" @on-pick-clear="handleClear"></Confirm>
23 25 </div>
24 26 </Dropdown-menu>
25 27 </Dropdown>
... ... @@ -94,7 +96,7 @@
94 96 components: { Dropdown, DropdownMenu, Confirm, RecommendColors, Saturation, Hue, Alpha },
95 97 props: {
96 98 value: {
97   - type: Object
  99 + type: String
98 100 },
99 101 alpha: {
100 102 type: Boolean,
... ... @@ -139,6 +141,7 @@
139 141 return {
140 142 val: _colorChange(this.value),
141 143 prefixCls: prefixCls,
  144 + visible: false,
142 145 recommendedColor: [
143 146 '#2d8cf0',
144 147 '#19be6b',
... ... @@ -170,7 +173,6 @@
170 173 },
171 174 set (newVal) {
172 175 this.val = newVal;
173   - this.$emit('input', newVal);
174 176 }
175 177 },
176 178 wrapClasses () {
... ... @@ -190,14 +192,70 @@
190 192 [`${inputPrefixCls}-disabled`]: this.disabled
191 193 }
192 194 ];
  195 + },
  196 + displayedColor () {
  197 + let color;
  198 + if (this.visible) {
  199 + const rgba = this.saturationColors.rgba;
  200 + color = {
  201 + r: rgba.r,
  202 + g: rgba.g,
  203 + b: rgba.b,
  204 + a: rgba.a
  205 + };
  206 + } else {
  207 + color = tinycolor(this.value).toRgb();
  208 + }
  209 + return `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a})`;
193 210 }
194 211 },
195 212 watch: {
196 213 value (newVal) {
197 214 this.val = _colorChange(newVal);
  215 + },
  216 + visible () {
  217 + this.val = _colorChange(this.value);
198 218 }
199 219 },
200 220 methods: {
  221 + formatColor () {
  222 + const defaultColor = {
  223 + hex: '#ff0000',
  224 + hsl: {
  225 + h: 0,
  226 + s: 1,
  227 + l: 0.5,
  228 + a: 1
  229 + },
  230 + hsv: {
  231 + h: 0,
  232 + s: 1,
  233 + v: 1,
  234 + a: 1
  235 + },
  236 + rgba: {
  237 + r: 255,
  238 + g: 0,
  239 + b: 0,
  240 + a: 1
  241 + },
  242 + a: 1
  243 + };
  244 + if (this.value) {
  245 + const color = tinycolor(this.value);
  246 + const hex = color.toHex();
  247 + const hsl = color.toHsl();
  248 + const hsv = color.toHsv();
  249 + const rgba = color.toRgb();
  250 +
  251 + defaultColor.hex = hex;
  252 + defaultColor.hsl = hsl;
  253 + defaultColor.hsv = hsv;
  254 + defaultColor.rgba = rgba;
  255 + defaultColor.a = rgba.a;
  256 + }
  257 + return defaultColor;
  258 + },
201 259 childChange (data) {
202 260 this.colorChange(data);
203 261 },
... ... @@ -226,6 +284,16 @@
226 284 if (checked === passed) {
227 285 return data;
228 286 }
  287 + },
  288 + handleToggleVisible (visible) {
  289 + this.visible = visible;
  290 + },
  291 + handleSuccess () {
  292 + this.$emit('input', this.val);
  293 + },
  294 + handleClear () {
  295 + this.$emit('input', '');
  296 + // todo
229 297 }
230 298 }
231 299 };
... ...
src/styles/components/color-picker.less
... ... @@ -7,10 +7,16 @@
7 7 &-color{
8 8 width: 18px;
9 9 height: 18px;
10   - box-shadow: inset 0 0 0 1px rgba(0,0,0,.15);
  10 + background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAMCAIAAADZF8uwAAAAGUlEQVQYV2M4gwH+YwCGIasIUwhT25BVBADtzYNYrHvv4gAAAABJRU5ErkJggg==);
11 11 border-radius: 2px;
12 12 position: relative;
13 13 top: 2px;
  14 + div{
  15 + width: 100%;
  16 + height: 100%;
  17 + box-shadow: inset 0 0 0 1px rgba(0,0,0,.15);
  18 + border-radius: 2px;
  19 + }
14 20 }
15 21 &-large &-color{
16 22 width: 20px;
... ...