FormLockField.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <script setup>
  2. import { useIdGenerator, useValidationErrorIdGenerator } from '@/composables/helpers'
  3. import { UseColorMode } from '@vueuse/components'
  4. defineOptions({
  5. inheritAttrs: false
  6. })
  7. const props = defineProps({
  8. modelValue: String,
  9. modelModifiers: { default: () => ({}) },
  10. isEditMode: {
  11. type: Boolean,
  12. default: false
  13. },
  14. label: {
  15. type: String,
  16. default: ''
  17. },
  18. fieldName: {
  19. type: String,
  20. default: '',
  21. required: true
  22. },
  23. fieldError: [String],
  24. inputType: {
  25. type: String,
  26. default: 'text'
  27. },
  28. placeholder: {
  29. type: String,
  30. default: ''
  31. },
  32. help: {
  33. type: String,
  34. default: ''
  35. },
  36. hasOffset: {
  37. type: Boolean,
  38. default: false
  39. },
  40. isDisabled: {
  41. type: Boolean,
  42. default: false
  43. },
  44. isExpanded: {
  45. type: Boolean,
  46. default: true
  47. },
  48. maxLength: {
  49. type: Number,
  50. default: null
  51. },
  52. idSuffix: {
  53. type: String,
  54. default: ''
  55. },
  56. isLocked: Boolean,
  57. })
  58. const { inputId } = useIdGenerator(props.inputType, props.fieldName + props.idSuffix)
  59. const { valErrorId } = useValidationErrorIdGenerator(props.fieldName)
  60. const legendId = useIdGenerator('legend', props.fieldName).inputId
  61. const fieldIsLocked = ref(props.isDisabled || props.isEditMode || props.isLocked)
  62. const hasBeenTrimmed = ref(false)
  63. const componentKey = ref(0);
  64. const emit = defineEmits(['update:modelValue'])
  65. /**
  66. * Removes spaces from the input string
  67. */
  68. function emitValue(e) {
  69. let value = e.target.value
  70. if (props.modelModifiers.trimAll) {
  71. value = value.replace(/\s+/g, '')
  72. }
  73. emit('update:modelValue', value)
  74. }
  75. function alertOnSpace(e) {
  76. let value = e.target.value
  77. hasBeenTrimmed.value = value.includes(' ')
  78. emit('update:modelValue', value)
  79. }
  80. function forceRefresh(e) {
  81. hasBeenTrimmed.value = e.target.value.includes(' ')
  82. componentKey.value += 1
  83. }
  84. </script>
  85. <template>
  86. <label :for="inputId" class="label">
  87. {{ $t(label) }}<FontAwesomeIcon v-if="isLocked" :icon="['fas', 'lock']" class="ml-2" size="xs" />
  88. </label>
  89. <div class="field has-addons mb-0" :class="{ 'pt-3' : hasOffset }">
  90. <div class="control" :class="{ 'is-expanded': isExpanded }">
  91. <input
  92. :key="componentKey"
  93. :disabled="fieldIsLocked"
  94. :id="inputId"
  95. :type="inputType"
  96. class="input"
  97. :value="modelValue"
  98. :placeholder="placeholder"
  99. v-bind="$attrs"
  100. v-on:input="alertOnSpace"
  101. v-on:change="emitValue"
  102. v-on:blur="forceRefresh"
  103. :maxlength="maxLength"
  104. :aria-describedby="help ? legendId : undefined"
  105. :aria-invalid="fieldError != undefined"
  106. :aria-errormessage="fieldError != undefined ? valErrorId : undefined"
  107. />
  108. </div>
  109. <UseColorMode v-slot="{ mode }" v-if="isEditMode">
  110. <div class="control" v-if="fieldIsLocked">
  111. <button type="button" class="button field-lock" :class="{'is-dark' : mode == 'dark'}" @click.stop="fieldIsLocked = false" :title="$t('twofaccounts.forms.unlock.title')">
  112. <span class="icon">
  113. <FontAwesomeIcon :icon="['fas', 'lock']" />
  114. </span>
  115. </button>
  116. </div>
  117. <div class="control" v-else>
  118. <button type="button" class="button field-unlock" :class="{'is-dark' : mode == 'dark'}" @click.stop="fieldIsLocked = true" :title="$t('twofaccounts.forms.lock.title')">
  119. <span class="icon has-text-danger">
  120. <FontAwesomeIcon :icon="['fas', 'lock-open']" />
  121. </span>
  122. </button>
  123. </div>
  124. </UseColorMode>
  125. </div>
  126. <FieldError v-if="hasBeenTrimmed" :error="$t('twofaccounts.forms.spaces_are_ignored')" :field="'spaces'" :alertType="'is-warning'" />
  127. <FieldError v-if="fieldError != undefined" :error="fieldError" :field="fieldName" />
  128. <p :id="legendId" class="help" v-html="$t(help)" v-if="help"></p>
  129. </template>