ParameterInputOptions.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <div class="flex items-center">
  3. <label class="flex items-center">
  4. <ui-switch v-model="options.useMask" />
  5. <span class="ml-2"> Use input masking </span>
  6. </label>
  7. <v-remixicon
  8. v-tooltip="{ content: maskInfo, allowHTML: true }"
  9. name="riInformationLine"
  10. class="ml-1 text-gray-600 dark:text-gray-200"
  11. size="20"
  12. />
  13. <label v-if="false" class="flex items-center ml-4">
  14. <ui-switch v-model="options.unmaskValue" />
  15. <span class="ml-2">Return unmask value</span>
  16. </label>
  17. </div>
  18. <div v-if="options.useMask" class="mt-2">
  19. <p>Masks</p>
  20. <div class="space-y-2">
  21. <div
  22. v-for="(mask, index) in options.masks"
  23. :key="index"
  24. class="flex items-center"
  25. >
  26. <ui-input
  27. v-model="options.masks[index].mask"
  28. placeholder="aaa-aaa-aaa"
  29. />
  30. <ui-checkbox v-model="mask.isRegex" class="ml-4">
  31. Is RegEx
  32. </ui-checkbox>
  33. <div class="flex-grow" />
  34. <v-remixicon
  35. name="riDeleteBin7Line"
  36. class="cursor-pointer flex-shrink-0 ml-1"
  37. @click="options.masks.splice(index, 1)"
  38. />
  39. </div>
  40. </div>
  41. <template v-if="false">
  42. <p>Custom tokens</p>
  43. <div class="grid grid-cols-2 gap-4">
  44. <div
  45. v-for="(token, index) in options.customTokens"
  46. :key="index"
  47. class="flex items-center"
  48. >
  49. <ui-input
  50. v-model="token.symbol"
  51. placeholder="Symbol"
  52. style="width: 120px"
  53. />
  54. <ui-input
  55. v-model="token.regex"
  56. placeholder="RegEx"
  57. class="flex-1 ml-2"
  58. />
  59. <v-remixicon
  60. name="riDeleteBin7Line"
  61. class="cursor-pointer flex-shrink-0 ml-1"
  62. @click="options.customTokens.splice(index, 1)"
  63. />
  64. </div>
  65. </div>
  66. <ui-button class="mt-4" @click="addToken"> Add token </ui-button>
  67. </template>
  68. </div>
  69. </template>
  70. <script setup>
  71. import { reactive, watch, onMounted } from 'vue';
  72. import cloneDeep from 'lodash.clonedeep';
  73. const props = defineProps({
  74. modelValue: {
  75. type: [Object, String],
  76. default: () => ({}),
  77. },
  78. defaultValue: {
  79. type: Object,
  80. default: () => ({}),
  81. },
  82. });
  83. const emit = defineEmits(['update:modelValue']);
  84. const maskInfo = `
  85. Add mask to the input field
  86. <p class="mt-2">Supported patterns</p>
  87. <table class="tokens">
  88. <tbody>
  89. <tr>
  90. <td>0</td>
  91. <td>Any digit</td>
  92. </tr>
  93. <tr>
  94. <td>a</td>
  95. <td>Any letter</td>
  96. </tr>
  97. <tr>
  98. <td>*</td>
  99. <td>Any char</td>
  100. </tr>
  101. <tr>
  102. <td>[]</td>
  103. <td>Make input optional</td>
  104. </tr>
  105. <tr>
  106. <td>{}</td>
  107. <td>Include fixed part in unmasked value</td>
  108. </tr>
  109. <tr>
  110. <td>\`</td>
  111. <td>Prevent symbols shift back</td>
  112. </tr>
  113. <tr>
  114. <td>!</td>
  115. <td>Escape char</td>
  116. </tr>
  117. <tbody>
  118. </table>
  119. `;
  120. const cloneData = cloneDeep(props.modelValue || {});
  121. const options = reactive({
  122. ...(props.defaultValue || {}),
  123. ...cloneData,
  124. });
  125. function addMask() {
  126. options.masks.push({
  127. isRegex: false,
  128. mask: '',
  129. lazy: false,
  130. });
  131. }
  132. function addToken() {
  133. options.customTokens.push({
  134. symbol: '',
  135. regex: '',
  136. });
  137. }
  138. watch(
  139. options,
  140. () => {
  141. emit('update:modelValue', options);
  142. },
  143. { deep: true }
  144. );
  145. onMounted(() => {
  146. if (options.masks.length === 0) {
  147. addMask();
  148. }
  149. });
  150. </script>