index.vue 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <template>
  2. <el-input v-model="valueRef" v-bind="$attrs">
  3. <template #append>
  4. <el-color-picker v-model="colorRef" :predefine="PREDEFINE_COLORS" />
  5. </template>
  6. </el-input>
  7. </template>
  8. <script lang="ts" setup>
  9. import { propTypes } from '@/utils/propTypes'
  10. import { PREDEFINE_COLORS } from '@/utils/color'
  11. /**
  12. * 带颜色选择器输入框
  13. */
  14. defineOptions({ name: 'InputWithColor' })
  15. const props = defineProps({
  16. modelValue: propTypes.string.def('').isRequired,
  17. color: propTypes.string.def('').isRequired
  18. })
  19. watch(
  20. () => props.modelValue,
  21. (val: string) => {
  22. if (val === unref(valueRef)) return
  23. valueRef.value = val
  24. }
  25. )
  26. const emit = defineEmits(['update:modelValue', 'update:color'])
  27. // 输入框的值
  28. const valueRef = ref(props.modelValue)
  29. watch(
  30. () => valueRef.value,
  31. (val: string) => {
  32. emit('update:modelValue', val)
  33. }
  34. )
  35. // 颜色
  36. const colorRef = ref(props.color)
  37. watch(
  38. () => colorRef.value,
  39. (val: string) => {
  40. emit('update:color', val)
  41. }
  42. )
  43. </script>
  44. <style scoped lang="scss">
  45. :deep(.el-input-group__append) {
  46. padding: 0;
  47. .el-color-picker__trigger {
  48. padding: 0;
  49. border-left: none;
  50. border-radius: 0 var(--el-input-border-radius) var(--el-input-border-radius) 0;
  51. }
  52. }
  53. </style>