FormPasswordField.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <script setup>
  2. import { useIdGenerator, useValidationErrorIdGenerator } from '@/composables/helpers'
  3. defineOptions({
  4. inheritAttrs: true
  5. })
  6. const props = defineProps({
  7. modelValue: [String],
  8. label: {
  9. type: String,
  10. default: ''
  11. },
  12. fieldName: {
  13. type: String,
  14. default: '',
  15. required: true
  16. },
  17. fieldError: [String],
  18. inputType: {
  19. type: String,
  20. default: 'password'
  21. },
  22. placeholder: {
  23. type: String,
  24. default: ''
  25. },
  26. help: {
  27. type: String,
  28. default: ''
  29. },
  30. hasOffset: {
  31. type: Boolean,
  32. default: false
  33. },
  34. isDisabled: {
  35. type: Boolean,
  36. default: false
  37. },
  38. showRules: {
  39. type: Boolean,
  40. default: false
  41. },
  42. idSuffix: {
  43. type: String,
  44. default: ''
  45. },
  46. isLocked: Boolean,
  47. })
  48. const { inputId } = useIdGenerator(props.inputType, props.fieldName + props.idSuffix)
  49. const { valErrorId } = useValidationErrorIdGenerator(props.fieldName)
  50. const legendId = useIdGenerator('legend', props.fieldName).inputId
  51. const currentType = ref(props.inputType)
  52. const hasCapsLockOn = ref(false)
  53. const hasLowerCase = computed(() => {
  54. return /[a-z]/.test(props.modelValue)
  55. })
  56. const hasUpperCase = computed(() => {
  57. return /[A-Z]/.test(props.modelValue)
  58. })
  59. const hasNumber = computed(() => {
  60. return /[0-9]/.test(props.modelValue)
  61. })
  62. const hasSpecialChar = computed(() => {
  63. return /[^A-Za-z0-9]/.test(props.modelValue)
  64. })
  65. const IsLongEnough = computed(() => {
  66. return props.modelValue.length >= 8
  67. })
  68. function checkCapsLock(event) {
  69. if (typeof event.getModifierState === 'function') {
  70. hasCapsLockOn.value = event.getModifierState('CapsLock') ? true : false
  71. }
  72. }
  73. function setFieldType(event) {
  74. if (currentType.value != event) {
  75. currentType.value = event
  76. }
  77. }
  78. </script>
  79. <template>
  80. <div class="field" :class="{ 'pt-3' : hasOffset }">
  81. <label :for="inputId" class="label">
  82. {{ $t(label) }}<FontAwesomeIcon v-if="isLocked" :icon="['fas', 'lock']" class="ml-2" size="xs" />
  83. </label>
  84. <div class="control has-icons-right">
  85. <input
  86. :disabled="isDisabled || isLocked"
  87. :id="inputId"
  88. :type="currentType"
  89. class="input"
  90. :value="modelValue"
  91. :placeholder="placeholder"
  92. v-bind="$attrs"
  93. v-on:input="$emit('update:modelValue', $event.target.value)"
  94. v-on:keyup="checkCapsLock"
  95. :aria-describedby="help ? legendId : undefined"
  96. :aria-invalid="fieldError != undefined"
  97. :aria-errormessage="fieldError != undefined ? valErrorId : undefined"
  98. />
  99. <span v-if="currentType == 'password'" role="button" id="btnTogglePassword" tabindex="0" class="icon is-small is-right is-clickable" @keyup.enter="setFieldType('text')" @click="setFieldType('text')" :title="$t('auth.forms.reveal_password')">
  100. <font-awesome-icon :icon="['fas', 'eye-slash']" />
  101. </span>
  102. <span v-else role="button" id="btnTogglePassword" tabindex="0" class="icon is-small is-right is-clickable" @keyup.enter="setFieldType('password')" @click="setFieldType('password')" :title="$t('auth.forms.hide_password')">
  103. <font-awesome-icon :icon="['fas', 'eye']" />
  104. </span>
  105. </div>
  106. <p class="help is-warning" v-if="hasCapsLockOn" v-html="$t('auth.forms.caps_lock_is_on')" />
  107. <FieldError v-if="fieldError != undefined" :error="fieldError" :field="fieldName" />
  108. <p class="help" v-html="$t(help)" v-if="help" />
  109. <div v-if="showRules" :id="legendId" class="columns is-mobile is-size-7 mt-0">
  110. <div class="column is-one-third">
  111. <span class="has-text-weight-semibold">{{ $t("auth.forms.mandatory_rules") }}</span><br />
  112. <span class="is-underscored" id="valPwdIsLongEnough" :class="{'is-dot' : IsLongEnough}"></span>{{ $t('auth.forms.is_long_enough') }}<br/>
  113. </div>
  114. <div class="column">
  115. <span class="has-text-weight-semibold">{{ $t("auth.forms.optional_rules_you_should_follow") }}</span><br />
  116. <span class="is-underscored" id="valPwdHasLowerCase" :class="{'is-dot' : hasLowerCase}"></span>{{ $t('auth.forms.has_lower_case') }}<br/>
  117. <span class="is-underscored" id="valPwdHasUpperCase" :class="{'is-dot' : hasUpperCase}"></span>{{ $t('auth.forms.has_upper_case') }}<br/>
  118. <span class="is-underscored" id="valPwdHasSpecialChar" :class="{'is-dot' : hasSpecialChar}"></span>{{ $t('auth.forms.has_special_char') }}<br/>
  119. <span class="is-underscored" id="valPwdHasNumber" :class="{'is-dot' : hasNumber}"></span>{{ $t('auth.forms.has_number') }}
  120. </div>
  121. </div>
  122. </div>
  123. </template>