default-form.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <!-- 默认通用表单组件 -->
  2. <template>
  3. <view class="default-form-component">
  4. <uni-card title="表单信息" spacing="0">
  5. <view v-for="(item, index) in formElements" :key="index" class="form-item">
  6. <text class="form-label">{{ item.label }}:</text>
  7. <text class="form-value">{{ item.value || '-' }}</text>
  8. </view>
  9. </uni-card>
  10. </view>
  11. </template>
  12. <script setup lang="ts">
  13. import { ref, watch } from 'vue'
  14. const props = defineProps({
  15. formData: {
  16. type: Object,
  17. default: () => ({})
  18. },
  19. formElements: {
  20. type: Array,
  21. default: () => []
  22. },
  23. repeatingForm: {
  24. type: Object,
  25. default: () => ({ elementItem: [], elements: [] })
  26. }
  27. })
  28. const emits = defineEmits(['update'])
  29. const formElements = ref<any[]>([])
  30. // 监听表单数据变化
  31. watch(() => props.formData, (newVal) => {
  32. if (newVal && newVal.formElements) {
  33. formElements.value = newVal.formElements
  34. emits('update', newVal)
  35. }
  36. }, { immediate: true, deep: true })
  37. // 暴露验证方法给父组件
  38. defineExpose({
  39. validate: async () => {
  40. return Promise.resolve()
  41. }
  42. })
  43. </script>
  44. <style lang="scss" scoped>
  45. .default-form-component {
  46. .form-item {
  47. display: flex;
  48. margin-bottom: 10px;
  49. .form-label {
  50. color: #666;
  51. width: 100px;
  52. flex-shrink: 0;
  53. }
  54. .form-value {
  55. color: #333;
  56. flex: 1;
  57. }
  58. }
  59. }
  60. </style>