index.js 1.83 KB
Newer Older
1
import React, { useEffect, useState } from 'react'
田翔's avatar
田翔 committed
2 3 4 5 6 7 8 9
import { Select, message } from 'antd'
import { GetSelectItemList } from '../../../../apis/process'

const { Option } = Select

const RelevanceSelect = (props) => {

  const { value, onChange, schema, addons } = props
10
  const { disabled, placeholder, fieldParent, dictionary, presetValue } = schema
田翔's avatar
田翔 committed
11 12
  const [options, setOptions] = useState([])

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

田翔's avatar
田翔 committed
21
  const selectChange = (value) => {
田翔's avatar
田翔 committed
22
    if (addons) {
田翔's avatar
田翔 committed
23
      onChange(value || '')
田翔's avatar
田翔 committed
24
    }
田翔's avatar
田翔 committed
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
  }

  const onfocus = async () => {
    if (!addons) return
    if (!dictionary) {
      return message.error('缺少数据字典!')
    }
    const { code, data, msg } = await GetSelectItemList({ nodeName: dictionary })
    if (code === 0) {
      if (Array.isArray(data)) {
        let filterKey = null
        Object.keys(addons.formData).forEach(v => {
          if (addons.formData[v]) {
            Object.keys(addons.formData[v]).forEach(s => {
              if (fieldParent === s) {
                filterKey = addons.formData[v][s]
              }
            })
          }
        })
        if (filterKey) {
          setOptions(data.filter(v => v.nodeName === filterKey))
        } else {
          setOptions(data)
        }
      }
    } else {
      message.error(msg)
    }
  }

  return (
    <Select
58
      value={value}
田翔's avatar
田翔 committed
59 60 61 62
      onFocus={onfocus}
      style={{ width: '100%' }}
      onChange={selectChange}
      disabled={disabled}
田翔's avatar
田翔 committed
63
      placeholder={disabled ? (placeholder || '') : (placeholder || '请选择内容')}
64
      allowClear
田翔's avatar
田翔 committed
65 66 67 68 69 70 71 72 73 74
    >
      {
        options.map((v, i) => <Option value={v.nodeValue} key={i}>{v.nodeValue}</Option>)
      }
    </Select>
  )

}

export default RelevanceSelect