index.js 3.76 KB
Newer Older
田翔's avatar
田翔 committed
1
import React, { useState, useMemo, useEffect } from 'react'
2
import { Select, message } from 'antd'
3
import { GetSelectItemList, GetFieldValueFromTable, getStationListByUserID, getStationIDListByUserID } from '../../../../apis/process'
田翔's avatar
田翔 committed
4 5 6

const { Option } = Select

7
const ComboBox = (props) => {
田翔's avatar
田翔 committed
8

9
  const userID = window?.globalConfig?.userInfo?.OID || 1
10
  const { value, onChange, schema, addons } = props
11
  const { placeholder, disabled, sourceType, options, dictionary, tableName, fieldName, presetValue, isMultiple, isEdit, isMySite, isSearch, isStoreID } = schema
12 13 14
  const [dictionaryList, setDictionaryList] = useState([])
  const [tableData, setTableData] = useState([])
  const [site, setSite] = useState([])
田翔's avatar
田翔 committed
15

田翔's avatar
田翔 committed
16 17 18 19 20 21 22 23
  useEffect(() => {
    if (addons) {
      addons.setValue(addons.dataPath, presetValue)
    } else {
      onChange(presetValue)
    }
  }, [presetValue])

田翔's avatar
田翔 committed
24
  const valueShow = useMemo(() => {
25
    if (isMultiple || isEdit) {
田翔's avatar
田翔 committed
26
      return value ? value.split(',') : []
田翔's avatar
田翔 committed
27
    } else {
田翔's avatar
田翔 committed
28
      return value || null
田翔's avatar
田翔 committed
29
    }
田翔's avatar
田翔 committed
30
  }, [isEdit, isMultiple, value])
田翔's avatar
田翔 committed
31

田翔's avatar
田翔 committed
32
  const enums = useMemo(() => {
田翔's avatar
田翔 committed
33
    switch (sourceType) {
34 35 36
      case '手动输入':
        return Array.isArray(options) ? options.map(v => v.value) : []
      case '数据字典':
田翔's avatar
田翔 committed
37
        return Array.isArray(dictionaryList) ? dictionaryList.map(v => v.nodeValue) : []
38 39 40 41
      case '表数据':
        return Array.isArray(tableData) ? tableData : []
      case '站点':
        return Array.isArray(site) ? site : []
42 43 44
      default:
        return []
    }
田翔's avatar
田翔 committed
45
  }, [sourceType, options, dictionaryList, tableData, site])
田翔's avatar
田翔 committed
46

47 48 49 50
  const children = useMemo(() => {
    let children = []
    if (Array.isArray(enums)) {
      enums.forEach(v => {
51 52 53 54 55
        if (sourceType === '站点') {
          children.push(<Option key={isStoreID ? v.stationID : v.stationName}>{v.stationName}</Option>)
        } else {
          children.push(<Option key={v}>{v}</Option>)
        }
56 57 58
      })
    }
    return children
59
  }, [sourceType, enums, isStoreID])
田翔's avatar
田翔 committed
60

61 62 63
  const handleChange = value => {
    if (isMultiple || isEdit) {
      onChange(value.join(','))
田翔's avatar
田翔 committed
64
    } else {
65
      onChange(value)
田翔's avatar
田翔 committed
66 67 68
    }
  }

69
  const onFocus = async () => {
70
    if (addons) {
田翔's avatar
田翔 committed
71
      if (sourceType === '数据字典') {
72 73 74 75
        if (!dictionary) {
          return message.error('请选择数据字典!')
        }
        const { data } = await GetSelectItemList({ nodeName: dictionary })
76
        if (Array.isArray(data)) {
77 78 79
          setDictionaryList(data)
        }
      }
田翔's avatar
田翔 committed
80
      if (sourceType === '表数据') {
81 82 83 84 85 86 87 88 89 90 91 92 93
        if (!tableName) {
          return message.info('请选择表名!')
        }
        if (!fieldName) {
          return message.info('请选择字段名!')
        }
        const { code, data, msg } = await GetFieldValueFromTable(tableName, fieldName)
        if (code === 0) {
          if (Array.isArray(data)) {
            setTableData(data.filter(v => v))
          } else {
            setTableData([])
          }
94
        } else {
95
          message.error(msg)
96 97
        }
      }
田翔's avatar
田翔 committed
98
      if (sourceType === '站点') {
99
        const { code, data, msg } = await getStationIDListByUserID(userID, !isMySite)
100 101 102 103 104 105
        if (code === 0) {
          if (Array.isArray(data)) {
            setSite(data)
          } else {
            setSite([])
          }
106
        } else {
107
          message.error(msg)
108 109
        }
      }
110 111
    }
  }
田翔's avatar
田翔 committed
112

113 114 115 116
  return (
    <Select
      onFocus={onFocus}
      style={{ width: '100%' }}
117
      mode={isEdit ? 'tags' : (isMultiple ? 'multiple' : '')}
118 119
      disabled={disabled}
      showArrow={!disabled}
120
      showSearch={isSearch}
121 122 123
      value={valueShow}
      placeholder={placeholder}
      onChange={handleChange}
田翔's avatar
田翔 committed
124
      allowClear
125
      getPopupContainer={(targterNode) => targterNode.parentElement || document.body}
126
    >
127
      {children}
128 129
    </Select>
  )
田翔's avatar
田翔 committed
130

田翔's avatar
田翔 committed
131 132
}

133
export default ComboBox