index copy.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  2. <div v-if="types[type]" class="ui-input" :style="style" :class="{ require: props.require, error: props.error }">
  3. <component :is="types[type].component" v-bind="childProps" :model-value="props.modelValue" @update:model-value="newValue => emit('update:modelValue', newValue)">
  4. <template v-for="(slot, name) in $slots" #[name]="raw">
  5. <slot :name="name" v-bind="raw" />
  6. </template>
  7. </component>
  8. <slot />
  9. <p v-if="error" class="error-msg">{{ error }}</p>
  10. </div>
  11. </template>
  12. <script setup lang="ts">
  13. import { computed } from 'vue'
  14. import radio from './radio.vue'
  15. import checkbox from './checkbox.vue'
  16. import text from './text.vue'
  17. import select from './select.vue'
  18. import range from './range.vue'
  19. import textarea from './textarea.vue'
  20. import number from './number.vue'
  21. import uiSwitch from './switch.vue'
  22. import file from './file.vue'
  23. import search from './search.vue'
  24. import richtext from './richtext.vue'
  25. import {
  26. checkboxPropsDesc,
  27. filePropsDesc,
  28. inputPropsDesc,
  29. numberPropsDesc,
  30. radioPropsDesc,
  31. rangePropsDesc,
  32. richtextPropsDesc,
  33. searchPropsDesc,
  34. selectPropsDesc,
  35. switchPropsDesc,
  36. textPropsDesc,
  37. textareaPropsDesc,
  38. } from './state.js.bk'
  39. const types = {
  40. checkbox: {
  41. component: checkbox,
  42. propsDesc: checkboxPropsDesc,
  43. },
  44. text: {
  45. component: text,
  46. propsDesc: textPropsDesc,
  47. },
  48. select: {
  49. component: select,
  50. propsDesc: selectPropsDesc,
  51. },
  52. radio: {
  53. component: radio,
  54. propsDesc: radioPropsDesc,
  55. },
  56. range: {
  57. component: range,
  58. propsDesc: rangePropsDesc,
  59. },
  60. number: {
  61. component: number,
  62. propsDesc: numberPropsDesc,
  63. },
  64. switch: {
  65. component: uiSwitch,
  66. propsDesc: switchPropsDesc,
  67. },
  68. textarea: {
  69. component: textarea,
  70. propsDesc: textareaPropsDesc,
  71. },
  72. file: {
  73. component: file,
  74. propsDesc: filePropsDesc,
  75. },
  76. search: {
  77. component: search,
  78. propsDesc: searchPropsDesc,
  79. },
  80. richtext: {
  81. component: richtext,
  82. propsDesc: richtextPropsDesc,
  83. },
  84. }
  85. const props = defineProps(inputPropsDesc)
  86. const emit = defineEmits(['update:modelValue'])
  87. const type = computed(() => (types[props.type] ? props.type : 'text'))
  88. const childProps = computed(() => {
  89. const retain = Object.keys(types[type.value].propsDesc)
  90. const childProps = {}
  91. for (const key in props) {
  92. if (retain.includes(key)) {
  93. childProps[key] = props[key]
  94. }
  95. }
  96. if (!types[props.type]) {
  97. childProps.type = props.type
  98. }
  99. return childProps
  100. })
  101. const style = computed(() => {
  102. const style = {}
  103. const keys = Object.keys(childProps.value)
  104. if (!keys.includes('width')) {
  105. style.width = props.width
  106. }
  107. if (!keys.includes('height')) {
  108. style.height = props.height
  109. }
  110. return style
  111. })
  112. </script>