FieldInput.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <div class="_fd-field-input">
  3. <i class="fc-icon icon-group" @click.stop="copy"></i>
  4. <el-input
  5. v-model="formValue"
  6. :readonly="fieldReadonly || disabled"
  7. :disabled="fieldReadonly || disabled"
  8. @focus="onFocus"
  9. @blur="onInput"
  10. >
  11. <template #append v-if="!fieldReadonly">
  12. <i class="fc-icon icon-auto" @click="makeField"></i>
  13. </template>
  14. </el-input>
  15. </div>
  16. </template>
  17. <script>
  18. import {defineComponent} from 'vue';
  19. import uniqueId from '@form-create/utils/lib/unique';
  20. import errorMessage from '../utils/message';
  21. import {copyTextToClipboard} from '../utils/index';
  22. import is from '@form-create/utils/lib/type';
  23. export default defineComponent({
  24. name: 'FieldInput',
  25. inject: ['designer'],
  26. emits: ['input'],
  27. props: {
  28. value: String,
  29. disabled: Boolean,
  30. },
  31. computed: {
  32. fieldReadonly() {
  33. return this.designer.fieldReadonly;
  34. },
  35. activeRule() {
  36. return this.designer.activeRule;
  37. },
  38. t() {
  39. return this.designer.t;
  40. }
  41. },
  42. data() {
  43. return {
  44. formValue: this.value || '',
  45. oldValue: '',
  46. };
  47. },
  48. watch: {
  49. value(n) {
  50. this.formValue = n;
  51. }
  52. },
  53. methods: {
  54. copy() {
  55. copyTextToClipboard(this.value);
  56. },
  57. getSubChildren() {
  58. let subChildren = this.designer.getSubFormChildren(this.activeRule) || [];
  59. subChildren = is.trueArray(subChildren) ? subChildren : this.designer.children;
  60. return subChildren;
  61. },
  62. getSubFieldChildren() {
  63. const subChildren = this.getSubChildren();
  64. const list = [];
  65. const getRule = (children) => {
  66. children && children.forEach(rule => {
  67. if (rule && rule._fc_drag_tag && rule.field) {
  68. list.push({...rule, children: []});
  69. } else if (rule && rule.children) {
  70. getRule(rule.children);
  71. }
  72. });
  73. return list;
  74. };
  75. return getRule(subChildren);
  76. },
  77. checkValue() {
  78. const oldField = this.oldValue;
  79. let field = (this.formValue || '').replace(/[\s\ ]/g, '');
  80. if (!field) {
  81. errorMessage(this.t('computed.fieldEmpty'));
  82. return oldField;
  83. } else if (!/^[a-zA-Z]/.test(field)) {
  84. errorMessage(this.t('computed.fieldChar'));
  85. return oldField;
  86. } else if (oldField !== field) {
  87. const flag = field.indexOf('.') > -1;
  88. if (flag) {
  89. field = field.replaceAll('.', '_');
  90. }
  91. if (this.getSubFieldChildren().filter(v => v.field === field).length > 0) {
  92. errorMessage(this.t('computed.fieldExist', {label: field}));
  93. return oldField;
  94. }
  95. if (flag) {
  96. return field;
  97. }
  98. }
  99. this.oldValue = '';
  100. return field;
  101. },
  102. onFocus() {
  103. this.oldValue = this.formValue;
  104. },
  105. makeField() {
  106. this.oldValue = this.formValue;
  107. this.formValue = uniqueId();
  108. this.onInput();
  109. },
  110. onInput() {
  111. if (this.formValue !== this.value) {
  112. this.formValue = this.checkValue();
  113. this.oldValue = this.formValue;
  114. if (this.formValue !== this.value) {
  115. this.$emit('input', this.formValue);
  116. }
  117. }
  118. },
  119. },
  120. });
  121. </script>
  122. <style>
  123. ._fd-field-input {
  124. width: 100%;
  125. }
  126. ._fd-field-input > .fc-icon {
  127. position: absolute;
  128. right: 28px;
  129. top: 1px;
  130. z-index: 3;
  131. color: #a8abb2;
  132. cursor: pointer;
  133. width: 24px;
  134. height: 24px;
  135. text-align: center;
  136. }
  137. ._fd-field-input > .fc-icon:hover {
  138. color: #2E73FF;
  139. }
  140. ._fd-field-input .el-input-group__append {
  141. width: 25px;
  142. padding: 0;
  143. margin: 0;
  144. color: #606266;
  145. cursor: pointer;
  146. text-align: center;
  147. }
  148. </style>