naf-input.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <div class="naf-input">
  3. <el-select
  4. v-if="dict"
  5. v-model="holdValue"
  6. :placeholder="placeholder || '请选择'"
  7. :disabled="disabled"
  8. :size="size"
  9. :clearable="clearable"
  10. @change="handleChange"
  11. >
  12. <template v-if="typeof dict == 'function'">
  13. <template v-for="(_item, _index) in dict()">
  14. <el-option v-if="typeof _item == 'string'" :key="'option-item-' + _index" :label="_item" :value="_item"></el-option>
  15. <el-option v-else :key="'option-item-' + _index" :label="_item.name" :value="_item.code" :disabled="_item.status == '1'"></el-option>
  16. </template>
  17. </template>
  18. <template v-else>
  19. <template v-for="(_item, _index) in dict">
  20. <el-option v-if="typeof _item == 'string'" :key="'option-item-' + _index" :label="_item" :value="_item"></el-option>
  21. <el-option v-else :key="'option-item-' + _index" :label="_item.name" :value="_item.code" :disabled="_item.status == '1'"></el-option>
  22. </template>
  23. </template>
  24. </el-select>
  25. <el-checkbox v-else-if="type == 'checkbox'" v-model="holdValue" v-bind="options" @change="handleChange">
  26. {{ placeholder }}
  27. </el-checkbox>
  28. <el-date-picker
  29. v-else-if="type == 'date' || type == 'datetime'"
  30. :type="type"
  31. v-model="holdValue"
  32. :value-format="valueFormat"
  33. v-bind="options"
  34. @change="handleChange"
  35. >
  36. </el-date-picker>
  37. <el-input
  38. v-else
  39. v-model="holdValue"
  40. :type="type"
  41. v-bind="options"
  42. @change="handleChange(true)"
  43. @input="handleInput"
  44. :minlength="minlength || 0"
  45. :maxlength="maxlength || 48"
  46. >
  47. <template #append>
  48. <slot name="append"> </slot>
  49. </template>
  50. <template #prepend>
  51. <slot name="prepend"> </slot>
  52. </template>
  53. <template #prefix>
  54. <slot name="prefix"> </slot>
  55. </template>
  56. <template #suffix>
  57. <slot name="suffix"> </slot>
  58. </template>
  59. </el-input>
  60. </div>
  61. </template>
  62. <script>
  63. import _ from 'lodash';
  64. export default {
  65. props: {
  66. value: { required: true },
  67. disabled: Boolean,
  68. type: String,
  69. dict: [Array, Boolean, Function],
  70. placeholder: [String, Boolean],
  71. clearable: Boolean,
  72. size: String,
  73. valueFormat: String,
  74. maxlength: Number,
  75. minlength: Number,
  76. },
  77. data() {
  78. return {
  79. holdValue: this.value,
  80. };
  81. },
  82. computed: {
  83. options() {
  84. return { placeholder: this.placeholder || undefined, clearable: this.clearable, disabled: this.disabled, size: this.size };
  85. },
  86. },
  87. methods: {
  88. handleChange(flag) {
  89. if (typeof this.holdValue == 'string' && this.holdValue && this.holdValue != this.holdValue.trim()) {
  90. this.holdValue = this.holdValue.trim();
  91. }
  92. this.$emit('input', this.holdValue);
  93. this.$emit('change', this.holdValue);
  94. },
  95. handleInput() {
  96. if (typeof this.holdValue == 'string' && this.holdValue && this.holdValue != this.holdValue.trim()) {
  97. this.holdValue = this.holdValue.trim();
  98. }
  99. this.$emit('input', this.holdValue);
  100. },
  101. },
  102. watch: {
  103. value(val) {
  104. if (this.holdValue != val) {
  105. this.holdValue = val;
  106. }
  107. },
  108. },
  109. };
  110. </script>
  111. <style></style>