|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <label class="font-semibold">{{ fieldName }}</label> |
| 4 | + <ValidationProvider v-slot="{ errors, touched }" :vid="id" :rules="rules"> |
| 5 | + <div class="relative flex items-center -mt-2"> |
| 6 | + <input |
| 7 | + v-if="fieldType != 'dropdown'" |
| 8 | + v-model="inputValue" |
| 9 | + :name="fieldName" |
| 10 | + :type="showText ? 'text' : fieldType" |
| 11 | + class="w-full h-10 px-2 my-3 drop-shadow-lg" |
| 12 | + /> |
| 13 | + <div |
| 14 | + v-if="fieldType == 'password'" |
| 15 | + class="absolute right-0 top-auto bottom-auto left-auto w-10" |
| 16 | + @mouseenter="toggleShowText" |
| 17 | + @mouseleave="toggleShowText" |
| 18 | + > |
| 19 | + <font-awesome-icon |
| 20 | + :icon="['fas', 'fa-eye']" |
| 21 | + class="w-6 mx-2" |
| 22 | + :class="showText ? 'text-blue2' : 'text-gray-400'" |
| 23 | + /> |
| 24 | + </div> |
| 25 | + <select |
| 26 | + v-if="fieldType == 'dropdown'" |
| 27 | + v-model="inputValue" |
| 28 | + :name="fieldName" |
| 29 | + class="w-full h-10 px-5 my-3 drop-shadow-lg" |
| 30 | + > |
| 31 | + <option selected disabled value="">Please Choose Grade...</option> |
| 32 | + <option |
| 33 | + v-for="(option, index) in fieldOptions" |
| 34 | + :key="index" |
| 35 | + :value="option" |
| 36 | + v-text="option" |
| 37 | + /> |
| 38 | + </select> |
| 39 | + </div> |
| 40 | + <span v-if="touched" class="text-red">{{ errors[0] }}</span> |
| 41 | + </ValidationProvider> |
| 42 | + </div> |
| 43 | +</template> |
| 44 | + |
| 45 | +<script> |
| 46 | +// Validation |
| 47 | +// --------------------------------------- |
| 48 | +import { ValidationProvider, extend } from 'vee-validate'; |
| 49 | +import { required, email, min } from 'vee-validate/dist/rules'; |
| 50 | +
|
| 51 | +extend('email', { |
| 52 | + ...email, |
| 53 | + message: 'Invalid email address', |
| 54 | +}); |
| 55 | +
|
| 56 | +extend('required', { |
| 57 | + ...required, |
| 58 | + message: 'This field is required', |
| 59 | +}); |
| 60 | +
|
| 61 | +extend('min', { |
| 62 | + ...min, |
| 63 | + message: '{_field_} must have at least {length} characters', |
| 64 | +}); |
| 65 | +
|
| 66 | +extend('password', { |
| 67 | + params: ['target'], |
| 68 | + validate(value, { target }) { |
| 69 | + return value === target; |
| 70 | + }, |
| 71 | + message: 'Password confirmation does not match', |
| 72 | +}); |
| 73 | +// --------------------------------------- |
| 74 | +
|
| 75 | +export default { |
| 76 | + name: 'InputField', |
| 77 | + components: { |
| 78 | + ValidationProvider, |
| 79 | + }, |
| 80 | + props: { |
| 81 | + fieldName: { |
| 82 | + type: String, |
| 83 | + default: 'Field', |
| 84 | + }, |
| 85 | + fieldType: { |
| 86 | + type: String, |
| 87 | + default: 'text', |
| 88 | + }, |
| 89 | + isPassword: Boolean, |
| 90 | + fieldOptions: { |
| 91 | + type: Array, |
| 92 | + default: undefined, |
| 93 | + }, |
| 94 | + id: { |
| 95 | + type: String, |
| 96 | + default: undefined, |
| 97 | + }, |
| 98 | + rules: { |
| 99 | + type: String, |
| 100 | + default: undefined, |
| 101 | + }, |
| 102 | + }, |
| 103 | + data: () => ({ |
| 104 | + showText: false, |
| 105 | + inputValue: '', |
| 106 | + }), |
| 107 | + methods: { |
| 108 | + toggleShowText: function () { |
| 109 | + this.showText = !this.showText; |
| 110 | + }, |
| 111 | + }, |
| 112 | +}; |
| 113 | +</script> |
0 commit comments