Преглед изворни кода

feat: editForm加入useOptions,根据传入的type自动引入options

lanjianrong пре 5 година
родитељ
комит
aad55c0b4e

+ 0 - 9
src/components/EditableForm/src/consts.js

@@ -1,9 +0,0 @@
-import { useStore } from 'dva'
-
-const store = useStore()
-export const selectOptions = {
-  gender: [
-    { label: '男', value: '男' },
-    { label: '女', value: '女' }
-  ]
-}

+ 0 - 2
src/components/EditableForm/src/editableForm.jsx

@@ -1,10 +1,8 @@
 import { Row } from 'antd'
 import React from 'react'
 import EditableFormItem from './editableFormItem'
-import { useStore } from 'dva'
 
 const EditableForm = ({ dataSource, columns, type, tartgetUrl }) => {
-  const store = useStore()
   // console.log(store)
   const newColumns = columns.map(item => {
     const newItem = { ...item }

+ 6 - 15
src/components/EditableForm/src/editableFormItem.jsx

@@ -5,6 +5,7 @@ import React, { useState, useEffect, useRef } from 'react'
 import LazyCascader from '../../LazyCascader'
 import { connect } from 'dva'
 import './index.less'
+import useOptions from './useOptions'
 
 const EditableFormItem = ({
   type,
@@ -19,12 +20,11 @@ const EditableFormItem = ({
 }) => {
   const record = dataSource[dataIndex]
   const dataId = dataSource.id
-
+  const options = useOptions(dataIndex)
   const [editing, setEditing] = useState(false)
   const iRef = useRef(null)
   const [val, setVal] = useState('')
   const [extraVal, setExtraVal] = useState('')
-  const { Option } = Select
   useEffect(() => {
     setVal('')
     setExtraVal('')
@@ -94,16 +94,6 @@ const EditableFormItem = ({
   if (editCellType === 'custom') {
     cell = customCell
   } else if (editing) {
-    const options = (
-      <>
-        <Option key="男" vlaue="男">
-          男
-        </Option>
-        <Option key="女" vlaue="女">
-          女
-        </Option>
-      </>
-    )
     switch (editCellType) {
       case 'cascader':
         cell = (
@@ -122,10 +112,11 @@ const EditableFormItem = ({
             defaultValue={val}
             ref={iRef}
             onBlur={save}
+            options={options}
+            mode={Array.isArray(val) ? 'multiple' : ''}
             onChange={value => save({ currentTarget: { value } })}
-            className="zh-width-100P">
-            {options}
-          </Select>
+            className="zh-width-100P"
+          />
         )
         break
       case 'textarea':

+ 31 - 0
src/components/EditableForm/src/useOptions.js

@@ -0,0 +1,31 @@
+import { useStore, useDispatch } from 'dva'
+import { useEffect } from 'react'
+
+const genderOptions = [
+  { label: '男', value: '男' },
+  { label: '女', value: '女' }
+]
+const typeEnum = {
+  NATUREIDS: 'natureIds',
+  GENDER: 'gender'
+}
+
+export default function useOptions(type) {
+  const store = useStore()
+  const dispatch = useDispatch()
+
+  useEffect(() => {
+    if (type === typeEnum.NATUREIDS) {
+      dispatch({
+        type: 'customer/fetch'
+      })
+    }
+  }, [])
+  if (type === typeEnum.GENDER) {
+    return genderOptions
+  }
+  if (type === typeEnum.NATUREIDS) {
+    return store.getState().customer.natures
+  }
+  return null
+}