form-item.uvue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <template>
  2. <view class="form-item-wrapper" :class="{ horizontal: layout === 'horizontal', vertical: layout === 'vertical' }">
  3. <view class="form-item-main">
  4. <view class="form-item-label-row" v-if="label.length > 0">
  5. <text class="form-item-required" v-if="required">*</text>
  6. <text class="form-item-label" :style="layout === 'horizontal' && labelWidth.length > 0 ? { width: labelWidth } : {}">{{ label }}</text>
  7. </view>
  8. <view class="form-item-content">
  9. <slot></slot>
  10. </view>
  11. </view>
  12. <view class="form-item-error" v-if="error.length > 0">
  13. <text class="form-item-error-text">{{ error }}</text>
  14. </view>
  15. </view>
  16. </template>
  17. <script setup lang="uts">
  18. // Props 定义
  19. type Props = {
  20. label?: string
  21. required?: boolean
  22. error?: string
  23. layout?: 'horizontal' | 'vertical'
  24. labelWidth?: string
  25. }
  26. const props = withDefaults(defineProps<Props>(), {
  27. label: '',
  28. required: false,
  29. error: '',
  30. layout: 'horizontal',
  31. labelWidth: '140rpx'
  32. })
  33. </script>
  34. <style lang="scss">
  35. .form-item-wrapper {
  36. margin-bottom: 24rpx;
  37. .form-item-main {
  38. .form-item-label-row {
  39. flex-direction: row;
  40. align-items: center;
  41. position: relative;
  42. margin-left: 10rpx;
  43. overflow: visible;
  44. .form-item-required {
  45. position: absolute;
  46. left: -12rpx;
  47. top: 5rpx;
  48. font-weight: 400;
  49. font-size: 28rpx;
  50. color: #ff3b30;
  51. margin-right: 4rpx;
  52. }
  53. .form-item-label {
  54. font-weight: 400;
  55. font-size: 28rpx;
  56. color: #6e7580;
  57. }
  58. }
  59. .form-item-content {
  60. flex: 1;
  61. }
  62. }
  63. .form-item-error {
  64. margin-top: 8rpx;
  65. padding-left: 4rpx;
  66. .form-item-error-text {
  67. font-weight: 400;
  68. font-size: 24rpx;
  69. color: #ff3b30;
  70. line-height: 32rpx;
  71. }
  72. }
  73. // 左右布局(默认)
  74. &.horizontal {
  75. .form-item-main {
  76. flex-direction: row;
  77. align-items: center;
  78. }
  79. }
  80. // 上下布局
  81. &.vertical {
  82. .form-item-main {
  83. flex-direction: column;
  84. .form-item-label-row {
  85. margin-bottom: 16rpx;
  86. }
  87. .form-item-content {
  88. width: 100%;
  89. }
  90. }
  91. }
  92. }
  93. </style>