11<template >
2- <input
3- :id =" props.id"
4- class =" field-color"
5- type =" color"
6- :name =" props.field.name"
7- :value =" currentModelValue"
8- :required =" isRequired"
9- @change =" onFieldValueChanged"
10- @blur =" onBlur"
11- >
2+ <div class =" field-color-wrapper" >
3+ <input
4+ v-if =" props.field.withInput"
5+ v-maska =" maskOptions"
6+ class =" field-color-input"
7+ type =" text"
8+ :value =" currentModelValue"
9+ placeholder =" #ffffff"
10+ @input =" onFieldValueChanged"
11+ @blur =" onBlur"
12+ >
13+ <input
14+ :id =" props.id"
15+ class =" field-color"
16+ type =" color"
17+ :name =" props.field.name"
18+ :value =" currentModelValue"
19+ :required =" isRequired"
20+ @input =" onFieldValueChanged"
21+ @blur =" onBlur"
22+ >
23+ </div >
1224</template >
1325
1426<script setup>
15- import { toRefs } from ' vue'
27+ import validators from ' @/validators'
28+ import { toRefs , onBeforeMount } from ' vue'
1629import {
1730 useFormModel ,
1831 useFieldAttributes ,
1932 useFieldValidate ,
2033 useFieldProps ,
2134 useFieldEmits
2235} from ' @/composables/index.ts'
36+ import { vMaska } from ' maska/vue'
2337
2438const emits = defineEmits (useFieldEmits ())
2539const props = defineProps (useFieldProps ())
2640
41+ const maskOptions = {
42+ mask: ' !#HHHHHH' ,
43+ tokens: {
44+ H : {
45+ pattern: / [A-Fa-f0-9 ] /
46+ }
47+ }
48+ }
49+
2750const { field , model } = toRefs (props)
2851
2952const { currentModelValue } = useFormModel (model .value , field .value )
@@ -38,7 +61,7 @@ const { errors, validate } = useFieldValidate(
3861)
3962
4063const onBlur = () => {
41- validate ().then ((validationErrors ) => {
64+ validate (currentModelValue . value ).then ((validationErrors ) => {
4265 emits (' validated' ,
4366 validationErrors .length === 0 ,
4467 validationErrors,
@@ -49,8 +72,26 @@ const onBlur = () => {
4972
5073const onFieldValueChanged = ({ target }) => {
5174 errors .value = []
52- emits (' onInput' , target .value )
75+ // Ensure a change doesn't emit twice, we need this because both inputs might trigger this function at once.
76+ if (target .value !== currentModelValue .value ) {
77+ emits (' onInput' , target .value )
78+ }
5379}
5480
55- defineExpose ({ isVisible })
81+ onBeforeMount (() => {
82+ if (field .value .withInput ) {
83+ const fieldValidators = []
84+ if (Array .isArray (field .value .validator )) {
85+ fieldValidators .push (... field .value .validator )
86+ } else if (field .value .validator !== undefined ) {
87+ fieldValidators .push (field .value .validator )
88+ }
89+ // Keep in mind that the native color picker only supports 6 digit hex codes,
90+ // so even though a value might technically be valid, it won't display the right color on the color picker input.
91+ fieldValidators .push (validators .hexColorValue )
92+ field .value .validator = fieldValidators
93+ }
94+ })
95+
96+ defineExpose ({ isVisible, errors })
5697 </script >
0 commit comments