|
| 1 | +<template> |
| 2 | + <div :class="wrapperClass"> |
| 3 | + <slot name="label"> |
| 4 | + <label |
| 5 | + v-if="label" |
| 6 | + :for="$attrs.id || name" |
| 7 | + > |
| 8 | + {{ label }} |
| 9 | + </label> |
| 10 | + </slot> |
| 11 | + |
| 12 | + <input |
| 13 | + :id="$attrs.id || name" |
| 14 | + :ref="name" |
| 15 | + v-maska="mask" |
| 16 | + :class="computedClass" |
| 17 | + :name="name" |
| 18 | + :placeholder="placeholder" |
| 19 | + :value="inputValue" |
| 20 | + data-testid="lc-mask-input" |
| 21 | + v-bind="$attrs" |
| 22 | + @blur="onBlur" |
| 23 | + @maska="onChange" |
| 24 | + > |
| 25 | + <error-message :name="name" as="span" class="lc-form--error" /> |
| 26 | + </div> |
| 27 | +</template> |
| 28 | + |
| 29 | +<script lang="ts"> |
| 30 | +import { maska } from 'maska' |
| 31 | +import { defineComponent, computed } from 'vue' |
| 32 | +import { ErrorMessage, useField } from 'vee-validate' |
| 33 | +
|
| 34 | +export default defineComponent({ |
| 35 | + name: 'LcInput', |
| 36 | + directives: { maska }, |
| 37 | + components: { |
| 38 | + ErrorMessage, |
| 39 | + }, |
| 40 | + inheritAttrs: false, |
| 41 | + props: { |
| 42 | + disabled: { |
| 43 | + type: Boolean, |
| 44 | + default: false, |
| 45 | + }, |
| 46 | + label: { |
| 47 | + type: String, |
| 48 | + default: '', |
| 49 | + }, |
| 50 | + mask: { |
| 51 | + type: [String, Array], |
| 52 | + default: '', |
| 53 | + }, |
| 54 | + modelValue: { |
| 55 | + type: [String, Number], |
| 56 | + default: '', |
| 57 | + }, |
| 58 | + name: { |
| 59 | + type: String, |
| 60 | + required: true, |
| 61 | + }, |
| 62 | + noBorder: { |
| 63 | + type: Boolean, |
| 64 | + default: false, |
| 65 | + }, |
| 66 | + placeholder: { |
| 67 | + type: String, |
| 68 | + default: '', |
| 69 | + }, |
| 70 | + rules: { |
| 71 | + type: [String, Object], |
| 72 | + default: '', |
| 73 | + }, |
| 74 | + wrapperClass: { |
| 75 | + type: String, |
| 76 | + default: 'w-full mb-4', |
| 77 | + }, |
| 78 | + }, |
| 79 | + emits: ['update:modelValue', 'blur', 'raw-value'], |
| 80 | + setup(props, { emit }) { |
| 81 | + const { |
| 82 | + value: inputValue, |
| 83 | + handleBlur, |
| 84 | + handleChange, |
| 85 | + errors, |
| 86 | + } = useField<string|number>(props.name, props.rules, { |
| 87 | + initialValue: props.modelValue, |
| 88 | + }) |
| 89 | +
|
| 90 | + const isError = computed(() => Boolean(errors.value.length)) |
| 91 | +
|
| 92 | + function onBlur(): void { |
| 93 | + handleBlur() |
| 94 | + emit('blur') |
| 95 | + } |
| 96 | +
|
| 97 | + function onChange(event: Event & { target: HTMLInputElement }): void { |
| 98 | + handleChange(event.target.value) |
| 99 | + emit('raw-value', event.target.dataset.maskRawValue) |
| 100 | + } |
| 101 | +
|
| 102 | + return { |
| 103 | + inputValue, |
| 104 | + handleChange, |
| 105 | + isError, |
| 106 | + onBlur, |
| 107 | + onChange, |
| 108 | + } |
| 109 | + }, |
| 110 | + computed: { |
| 111 | + computedClass(): any[] { |
| 112 | + return [ |
| 113 | + 'lc-mask-input', |
| 114 | + { 'lc-mask-input--hasBorder': !this.noBorder }, |
| 115 | + { 'lc-mask-input--disabled': this.disabled }, |
| 116 | + { 'lc-mask-input--hasError-hasBorder': this.isError && !this.noBorder }, |
| 117 | + { 'lc-mask-input--hasntError-hasBorder': !this.isError && !this.noBorder }, |
| 118 | + ] |
| 119 | + }, |
| 120 | + }, |
| 121 | +}) |
| 122 | +</script> |
| 123 | + |
| 124 | +<style scoped> |
| 125 | +.lc-mask-input { |
| 126 | + height: 51px; |
| 127 | + transition: border-color .15s ease-in-out, box-shadow .15s ease-in-out; |
| 128 | + @apply block bg-clip-padding w-full text-gray-700 font-normal text-base leading-normal py-1.5 px-4 bg-white focus:text-gray-700 focus:bg-white focus:border-primary-focus focus:shadow-focus focus:outline-none; |
| 129 | +} |
| 130 | +.lc-mask-input--hasBorder { |
| 131 | + @apply border rounded-sm; |
| 132 | +} |
| 133 | +.lc-mask-input--disabled { |
| 134 | + @apply bg-gray-300 pointer-events-none text-gray-500; |
| 135 | +} |
| 136 | +.lc-mask-input--hasError-hasBorder { |
| 137 | + @apply border-error; |
| 138 | +} |
| 139 | +.lc-mask-input--hasntError-hasBorder { |
| 140 | + @apply border-gray-400; |
| 141 | +} |
| 142 | +.lc-mask-input[disabled=disabled] { |
| 143 | + -webkit-text-fill-color: #aaaaaa; |
| 144 | +} |
| 145 | +</style> |
0 commit comments