SColorPicker2.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. const n = `<template>
  2. <div class="s-color-picker-common">
  3. <a-popover trigger="click" placement="bottomLeft" v-model:open="visible">
  4. <template #content>
  5. <div class="picker-panel">
  6. <div class="palette">
  7. <span
  8. class="swatch"
  9. v-for="c in palette"
  10. :key="c"
  11. :style="{ backgroundColor: c }"
  12. :class="{ active: c.toLowerCase() === normalized }"
  13. @click="pick(c)"
  14. />
  15. </div>
  16. <div class="native-row">
  17. <input type="color" class="native-color" :value="normalized" @input="onNative" />
  18. <span class="native-tip">{{ $l('自定义颜色') }}</span>
  19. </div>
  20. </div>
  21. </template>
  22. <a-input
  23. class="color-input"
  24. :value="value"
  25. :style="{ width: width + 'px' }"
  26. placeholder=""
  27. @update:value="onInput"
  28. >
  29. <template #prefix>
  30. <span class="color-block" :style="[{ backgroundColor: normalized }, blockStyle]" />
  31. </template>
  32. </a-input>
  33. </a-popover>
  34. </div>
  35. </template>
  36. <script setup>
  37. import { computed, ref } from 'vue'
  38. const props = defineProps({
  39. // 颜色值(hex),支持 v-model:value
  40. value: {
  41. type: String,
  42. default: ''
  43. },
  44. // 色块宽度,单位 px
  45. width: {
  46. type: Number,
  47. default: 40
  48. },
  49. // 透传给色块的样式
  50. blockStyle: {
  51. type: Object,
  52. default() {
  53. return {}
  54. }
  55. }
  56. })
  57. const emits = defineEmits(['update:value', 'change'])
  58. const visible = ref(false)
  59. const palette = [
  60. '#1677ff',
  61. '#1167ff',
  62. '#722ed1',
  63. '#eb2f96',
  64. '#f5222d',
  65. '#fa541c',
  66. '#fa8c16',
  67. '#faad14',
  68. '#13c2c2',
  69. '#52c41a',
  70. '#1afa29',
  71. '#44d7b6'
  72. ]
  73. const normalized = computed(() =>
  74. /^#([0-9a-fA-F]{6})$/.test(props.value) ? props.value.toLowerCase() : '#1677ff'
  75. )
  76. const emitValue = (val) => {
  77. emits('update:value', val)
  78. emits('change', val)
  79. }
  80. const pick = (c) => {
  81. emitValue(c)
  82. visible.value = false
  83. }
  84. const onNative = (e) => {
  85. emitValue(e.target.value)
  86. }
  87. const onInput = (val) => {
  88. emitValue(val)
  89. }
  90. <\/script>
  91. <style lang="less" scoped>
  92. .s-color-picker-common {
  93. margin-left: 10px;
  94. display: inline-block;
  95. }
  96. .color-block {
  97. display: inline-block;
  98. width: 16px;
  99. height: 16px;
  100. border-radius: 3px;
  101. border: 1px solid var(--s-border-color-base);
  102. box-sizing: border-box;
  103. cursor: pointer;
  104. }
  105. .picker-panel {
  106. width: 220px;
  107. .palette {
  108. display: flex;
  109. flex-wrap: wrap;
  110. justify-content: flex-start;
  111. gap: 8px;
  112. }
  113. .swatch {
  114. width: 24px;
  115. height: 24px;
  116. border-radius: 4px;
  117. cursor: pointer;
  118. border: 1px solid transparent;
  119. box-sizing: border-box;
  120. }
  121. .swatch.active {
  122. border-color: var(--s-text-color);
  123. }
  124. .native-row {
  125. display: flex;
  126. align-items: center;
  127. margin-top: 12px;
  128. padding-top: 12px;
  129. border-top: 1px solid var(--s-border-color-split);
  130. .native-color {
  131. width: 32px;
  132. height: 28px;
  133. padding: 0;
  134. border: 1px solid var(--s-border-color-base);
  135. border-radius: 4px;
  136. background: none;
  137. cursor: pointer;
  138. }
  139. .native-tip {
  140. margin-left: 8px;
  141. color: var(--s-text-color-secondary);
  142. font-size: 13px;
  143. }
  144. }
  145. }
  146. .color-input {
  147. :deep(.ant-input-prefix) {
  148. margin-inline-end: 2px;
  149. }
  150. :deep(input.ant-input) {
  151. padding-left: 2px;
  152. padding-right: 2px;
  153. }
  154. }
  155. </style>
  156. `;
  157. export {
  158. n as default
  159. };