fs-form-item.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <view class="fs-form-item" :class="['fs-form-item-' + position, { 'fs-form-item-border': border }]">
  3. <view class="fs-form-item-label-before" v-if="slots.before"><slot name="before"></slot></view>
  4. <view
  5. class="fs-form-item-label"
  6. :class="['text-' + align, { 'fs-form-item-required': required }]"
  7. :style="{ width: width }"
  8. v-if="label"
  9. >
  10. {{ label }}
  11. </view>
  12. <view class="fs-form-item-right"><slot></slot></view>
  13. </view>
  14. </template>
  15. <script>
  16. /**
  17. * 表单项组件
  18. * @description 表单项组件
  19. * @property {String} label label
  20. * @property {String} labelWidth label宽度
  21. * @property {String} labelPosition = [left | top] label位置
  22. * @property {String} labelAlign = [left | center | right | justify] label对齐方式
  23. * @property {Boolean} required 是否必填,值为true时会有红色星号
  24. * @property {Boolean} border 是否显示边框
  25. */
  26. export default {
  27. name: 'fs-form-item'
  28. }
  29. </script>
  30. <script setup>
  31. import { inject, provide, computed, useSlots } from 'vue'
  32. const props = defineProps({
  33. label: String,
  34. labelWidth: String,
  35. labelPosition: {
  36. type: String,
  37. validator(value) {
  38. return ['left', 'top'].includes(value)
  39. }
  40. },
  41. labelAlign: {
  42. type: String,
  43. validator(value) {
  44. return ['left', 'center', 'right', 'justify'].includes(value)
  45. }
  46. },
  47. required: Boolean,
  48. border: {
  49. type: Boolean,
  50. default: true
  51. }
  52. })
  53. const slots = useSlots()
  54. const form = inject('form', {})
  55. const position = props.labelPosition || form.labelPosition || 'left'
  56. const width = props.labelWidth || form.labelWidth || '120rpx'
  57. const align = props.labelAlign || form.labelAlign || 'left'
  58. provide('form-item-position', position)
  59. </script>
  60. <style lang="scss" scoped>
  61. .fs-form-item {
  62. display: flex;
  63. background-color: #fff;
  64. padding: 20rpx var(--gutter);
  65. min-height: 110rpx;
  66. &-border {
  67. border-bottom: 2rpx solid var(--border-color);
  68. }
  69. &-left {
  70. align-items: center;
  71. // .label{
  72. // text-align-last: justify;
  73. // }
  74. }
  75. &-top {
  76. flex-direction: column;
  77. .form-item-label {
  78. margin-bottom: 10rpx;
  79. width: auto !important;
  80. }
  81. }
  82. &-label {
  83. font-size: var(--content-size);
  84. margin-right: 20rpx;
  85. &-before {
  86. margin-right: 20rpx;
  87. }
  88. }
  89. &-required {
  90. position: relative;
  91. padding-left: 14rpx;
  92. &::before {
  93. position: absolute;
  94. content: '*';
  95. color: red;
  96. left: 0;
  97. }
  98. }
  99. &-right {
  100. flex: 1;
  101. }
  102. }
  103. </style>