2
schangxiang@126.com
2025-05-12 6177ed5cb88df34f2a67d9d0610e3e0dc7030e70
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<template>
  <div class="customFormItem">
    <div
      v-if="showLabel"
      class="label"
      :style="{ width: labelWidth || 'auto' }"
    >
      <span v-if="!showToolTip" ref="contentRef" class="label-content">{{
        label
      }}</span>
      <el-tooltip
        v-else
        class="box-item"
        effect="dark"
        :content="showToolTip ? label : ''"
      >
        <span class="label-content" ref="contentRef">{{ label }}</span>
      </el-tooltip>
    </div>
    <div class="content">
      <div v-if="isReadOnly" :style="{ width: inputWidth || 'auto' }">
        {{ readOnlyLabel }}
      </div>
      <div v-else-if="($props.source = 'productForm')">
        <component
          :is="componentName"
          v-model="value"
          v-bind="combineAttrs($attrs, $props)"
          class="fix-input-style fix-datetime-picker productForm-picker"
          format="YYYY-MM-DD HH:mm"
          date-format="YYYY-MM-DD"
          time-format="HH:mm"
          popper-class="light-datetime-picker productForm-picker"
          :style="{ width: inputWidth || 'auto' }"
        >
          <slot></slot>
        </component>
      </div>
      <div v-else>
        <component
          :is="componentName"
          v-model="value"
          v-bind="combineAttrs($attrs, $props)"
          class="fix-input-style fix-datetime-picker"
          popper-class="light-datetime-picker"
          :style="{ width: inputWidth || 'auto' }"
        >
          <slot></slot>
        </component>
      </div>
    </div>
  </div>
</template>
 
<script lang="ts">
import { PropType, defineComponent, computed, ref, onMounted } from 'vue'
import { isFunction, omit } from 'lodash'
import { useVModel } from '@vueuse/core'
import { ElInput, ElSelect, ElDatePicker } from 'element-plus'
 
export default defineComponent({
  name: 'CustomFormItem',
  components: { ElInput, ElSelect, ElDatePicker },
  props: {
    showLabel: {
      type: Boolean,
      default: true,
    },
    label: {
      type: String,
      default: '',
    },
    componentName: {
      type: [String, Object] as PropType<
        string | ReturnType<typeof defineComponent>
      >,
      default: '',
    },
    isReadOnly: {
      type: Boolean,
      default: false,
    },
    formatter: {
      type: [null, Function] as PropType<(...args: any[]) => void | null>,
      default: null,
    },
    modelValue: {
      type: [String, Number, Array, Date],
      default: '',
    },
    labelWidth: {
      type: String,
      default: '',
    },
    inputWidth: {
      type: String,
      default: '',
    },
    source: {
      type: String,
      default: '',
    },
  },
 
  setup(props) {
    const value = useVModel(props)
    const showToolTip = ref(true)
    const contentRef = ref()
 
    // 只读状态下返回一个formatter 函数来处理数据
    const readOnlyLabel = computed(() => {
      if (props.isReadOnly && isFunction(props.formatter)) {
        return props.formatter(value)
      }
 
      return value
    })
 
    onMounted(() => {
      try {
        const offerWidth = contentRef.value.offsetWidth
        const BASE_WIDTH = 80
 
        showToolTip.value = offerWidth > BASE_WIDTH
      } catch (error) {
        console.log(error)
      }
    })
 
    const combineAttrs = (
      attrs: Record<string, unknown>,
      props: Record<string, unknown>,
      omitPropNames: Array<string> = []
    ) => {
      const newObject = { ...attrs, ...props }
      return omit(newObject, omitPropNames)
    }
 
    return { value, readOnlyLabel, contentRef, showToolTip, combineAttrs }
  },
})
</script>
 
<style lang="scss" scoped>
@import '../../styles/common.scss';
 
.customFormItem {
  display: inline-flex;
  align-items: center;
  .label {
    overflow: hidden;
    text-overflow: ellipsis;
 
    &-content {
      width: 100%;
      color: #666;
      white-space: nowrap;
    }
  }
 
  .content {
    margin-left: 10px;
 
    :deep(.cs-select) {
      width: 100%;
    }
 
    :deep(.cs-date-editor.cs-input) {
      width: 100%;
    }
  }
}
</style>
<style lang="scss" scoped>
@import '../../styles/common.scss';
</style>
<style lang="scss">
// .productForm-picker {
//   .cs-time-spinner {
//     .cs-scrollbar {
//       width: 50%;
//     }
 
//     .cs-scrollbar:nth-child(3) {
//       display: none;
//     }
//   }
 
// }
</style>