zs
2025-05-13 0cd95d98fdf7ed22d53398242feb2a3ca185cf09
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// import { ElInput } from "element-plus";
import {
  defineComponent,
  PropType,
  ref,
  Ref,
  SetupContext,
  computed,
  unref,
  markRaw,
  DefineComponent,
} from 'vue'
import styles from './DyFormForHighQuery.module.scss'
import ElInput from 'element-plus/es/components/input/index'
import Option from '@/components/Select/Option'
import Select from '@/components/Select/Select'
import SelectInput from '@/components/SelectInput/SelectInput'
import type { FormInstance } from 'element-plus'
import Icon from '../Icon/Icon'
import {
  FormPropsType,
  FormItemPropType,
  PropsType,
  OptionItemType,
} from './DyFormForHighQuery.d'
import Variable from '../Variable/Variable'
import Title from '../Title/Title'
import TextareaFlow from '../Flow/Flow'
import get from 'lodash/get'
import set from 'lodash/set'
 
const formItemElementMap = markRaw<Record<string, any>>({
  input: ElInput,
  select: Select,
  selectInput: SelectInput,
  variable: Variable,
  textareaFlow: TextareaFlow,
})
 
const Type: Record<string, any> = {
  select: 'select',
}
export default defineComponent<FormPropsType>({
  //@ts-ignore
  name: '动态表单',
  props: {
    labelWidth: {
      type: String,
      default: '100px',
    },
    labelPosition: {
      type: String,
      default: 'left',
    },
    formData: {
      type: Object as PropType<{ [key: string]: any }>,
      default: () => ({}),
    },
    formItemProps: {
      type: Array,
      default: () => [],
    },
    inLine: {
      type: Boolean,
      default: false,
    },
  },
  setup(props: PropsType, { attrs, emit, expose }: SetupContext) {
    const formRef = ref<FormInstance>()
    const form: any = computed({
      get() {
        return props.formData
      },
      set(v) {
        emit('update:formData', v)
      },
    })
 
    const currentWidgetModel = computed(() => {
      return (path: string) => {
        return get(form.value, path)
      }
    })
 
    const validate = () => {
      if (!formRef.value) return false
      return new Promise((resolve, reject) => {
        formRef.value?.validate((valid: boolean) => {
          if (valid) {
            resolve(true)
          } else {
            reject(false)
          }
        })
      })
    }
 
    const resetForm = () => {
      if (!formRef.value) return false
      formRef.value.resetFields()
    }
 
    const formItemProps = computed(() => {
      return props.formItemProps || []
    })
 
    expose({ validate, resetForm })
 
    const FormRender: any = ($props: any) => {
      const item: FormItemPropType = $props.item
      const options = $props.item.options || []
      if (item.el && Type[item.el as string]) {
        return options.map((el: OptionItemType) => (
          <Option
            label={el.label || el.description || el.name}
            value={el.value}
          ></Option>
        ))
      }
      return null
    }
    const FormRenderForHighSelectOptions: any = ($props: any) => {
      const item: FormItemPropType = $props.item
      const options = $props.item.highSelectAttrs?.options || []
      if (1===1) {
        return options.map((el: OptionItemType) => (
          <Option
            label={el.label || el.description || el.name}
            value={el.value}
          ></Option>
        ))
      }
      return null
    }
 
    const onUpdateModelValue = (v: string | number, prop: string) => {
      set(form.value, prop, v)
    }
 
    return () => {
      return (
        <div class={styles.formStyle}>
          <el-form
            labelPosition={props.labelPosition}
            labelWidth={props.labelWidth}
            model={form.value}
            ref={formRef}
            inline={props.inLine}
          >
            {formItemProps.value.map(
              (item: FormItemPropType, index: number) => {
                if (item.isTitle) {
                  if (typeof item.title === 'string') {
                    return (
                      <Title style="margin-bottom: 10px">{item.title}</Title>
                    )
                  }
                  return item.title
                }
 
                const itemProps: FormItemPropType = {}
                Object.entries(item).forEach(([key, value]) => {
                  itemProps[key] = unref(value)
                })
 
                const el =
                  typeof itemProps.el === 'string'
                    ? formItemElementMap[itemProps.el]
                    : itemProps.el || null
                const Component = el
                const el2 =
                   formItemElementMap['select'];
                const ComponentForHighSelect = el2
                return Component && !item.isHide ? (
                
                  <el-form-item
                    label={itemProps.label}
                    prop={itemProps.prop}
                    rules={itemProps.rules}
                    key={itemProps.prop}
                    vSlots={
                      itemProps.labelIcon
                        ? {
                            label: () => (
                              <label class={styles.formitemPropsLabel}>
                                {itemProps.label}
                                <Icon icon={itemProps.labelIcon} />
                              </label>
                            ),
                          }
                        : null
                    }
                  >
                   {/* 添加的比较操作符选择器 */}
                   {/* 使用flex布局让三个元素在同一行 */}
                   <div class="flex items-center w-full" style="width:100%">
                  {/* <el-select
                  size="small"
                  style="margin-right: 8px; min-width: 10px;"
                >
                  <el-option label="等于" value="2" />
                  <el-option label="不等于" value="8" />
                </el-select>  */}
                 {/* 当组件类型不是日期控件时显示ComponentForHighSelect */}
                 {(!itemProps.isDateControl && (
  // ComponentForHighSelect 的渲染代码
  <ComponentForHighSelect style="width:150px;"
                   {...itemProps.highSelectAttrs}
                     modelValue={currentWidgetModel.value(itemProps.highSelectAttrs?.prop || '')}
                     onUpdate:modelValue={(val: string | number) =>
                        onUpdateModelValue(val, itemProps.highSelectAttrs?.prop || '')
                      }>
                  <FormRenderForHighSelectOptions item={itemProps} />
                </ComponentForHighSelect>
))}
 {/* 日期控件时的占位元素 */}
{itemProps.isDateControl && <span style="width:150px;margin-right:8px;"></span>}
                
                      &nbsp; 
                    <Component
                      style={{
                        width: itemProps.width, // 默认占满剩余宽度
                        // width: itemProps.width , // 默认占满剩余宽度
                        height: itemProps.height,
                      }}
                      {...itemProps}
                      // v-model={form.value[itemProps.prop as keyof any]}
                      modelValue={currentWidgetModel.value(itemProps.prop)}
                      onUpdate:modelValue={(val: string | number) =>
                        onUpdateModelValue(val, itemProps.prop)
                      }
                    >
                      <FormRender item={itemProps} />
                    </Component>
                    </div>
                  </el-form-item>
                ) : null
              }
            )}
          </el-form>
        </div>
      )
    }
  },
})