index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <template>
  2. <view class="form-field">
  3. <!-- 横向 -->
  4. <view v-if="layout === 'horizontal'" class="form-field-horizontal">
  5. <view class="form-field-label" :style="{ width: labelWidth }">
  6. <view class="form-field-required">
  7. <text v-if="required"> * </text>
  8. </view>
  9. <text class="form-field-title">
  10. {{ label }}
  11. </text>
  12. </view>
  13. <view class="form-field-body-right">
  14. <slot />
  15. </view>
  16. </view>
  17. <!-- 纵向 -->
  18. <view v-else class="form-field-vertical">
  19. <view class="form-field-label">
  20. <view class="form-field-required">
  21. <text v-if="required"> * </text>
  22. </view>
  23. <text class="form-field-title">
  24. {{ label }}
  25. </text>
  26. <text v-if="description" class="form-field-description">
  27. {{ description }}
  28. </text>
  29. </view>
  30. <view class="form-field-body-left">
  31. <slot />
  32. </view>
  33. </view>
  34. </view>
  35. </template>
  36. <script setup lang="ts">
  37. type FormFieldLayout = 'horizontal' | 'vertical'
  38. interface Props {
  39. label: string
  40. required?: boolean
  41. layout?: FormFieldLayout
  42. labelWidth?: string
  43. description?: string
  44. }
  45. withDefaults(defineProps<Props>(), {
  46. required: false,
  47. layout: 'horizontal',
  48. labelWidth: '220rpx',
  49. description: '',
  50. })
  51. </script>
  52. <style lang="scss" scoped>
  53. .form-field {
  54. background-color: #fff;
  55. .form-field-horizontal {
  56. display: flex;
  57. align-items: center;
  58. text-align: left;
  59. padding: 20rpx 28rpx;
  60. .form-field-body-right {
  61. line-height: 36rpx;
  62. display: flex;
  63. align-items: center;
  64. justify-content: flex-end;
  65. flex: 1;
  66. }
  67. }
  68. .form-field-vertical {
  69. padding-bottom: 20rpx;
  70. display: flex;
  71. flex-direction: column;
  72. .form-field-label {
  73. padding: 24rpx 28rpx;
  74. }
  75. .form-field-body-left {
  76. padding: 0 28rpx;
  77. display: flex;
  78. align-items: center;
  79. justify-content: flex-start;
  80. flex: 1;
  81. }
  82. }
  83. .form-field-title {
  84. color: #303133;
  85. font-size: 32rpx;
  86. white-space: normal;
  87. word-break: break-all;
  88. }
  89. .form-field-label {
  90. display: flex;
  91. align-items: center;
  92. line-height: 36rpx;
  93. }
  94. .form-field-required {
  95. margin-right: 8rpx;
  96. color: #fa3534;
  97. font-size: 30rpx;
  98. }
  99. }
  100. </style>