|
@@ -2,12 +2,12 @@ import consts from '@/basic/consts'
|
|
|
import { editFormApi } from '@/services/common'
|
|
import { editFormApi } from '@/services/common'
|
|
|
import { Input, Row, Col, Select } from 'antd'
|
|
import { Input, Row, Col, Select } from 'antd'
|
|
|
import React, { useState, useEffect, useRef } from 'react'
|
|
import React, { useState, useEffect, useRef } from 'react'
|
|
|
|
|
+import LazyCascader from '../LazyCascader'
|
|
|
import './index.less'
|
|
import './index.less'
|
|
|
|
|
|
|
|
const EditableFormItem = ({ label, dataIndex, record, dataId, span, editCellType, requestUrl }) => {
|
|
const EditableFormItem = ({ label, dataIndex, record, dataId, span, editCellType, requestUrl }) => {
|
|
|
-
|
|
|
|
|
const [editing, setEditing] = useState(false)
|
|
const [editing, setEditing] = useState(false)
|
|
|
- const inputRef = useRef(null)
|
|
|
|
|
|
|
+ const iRef = useRef(null)
|
|
|
const [val, setVal] = useState('')
|
|
const [val, setVal] = useState('')
|
|
|
const { Option } = Select
|
|
const { Option } = Select
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
@@ -16,7 +16,7 @@ const EditableFormItem = ({ label, dataIndex, record, dataId, span, editCellType
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
// 处于编辑状态自动聚焦
|
|
// 处于编辑状态自动聚焦
|
|
|
if (editing) {
|
|
if (editing) {
|
|
|
- inputRef.current.focus()
|
|
|
|
|
|
|
+ iRef.current.focus()
|
|
|
}
|
|
}
|
|
|
// 当val为空时,需要设置默认值
|
|
// 当val为空时,需要设置默认值
|
|
|
if (!val) {
|
|
if (!val) {
|
|
@@ -29,19 +29,27 @@ const EditableFormItem = ({ label, dataIndex, record, dataId, span, editCellType
|
|
|
setVal(record)
|
|
setVal(record)
|
|
|
}, [dataId])
|
|
}, [dataId])
|
|
|
|
|
|
|
|
-
|
|
|
|
|
const toggleEdit = () => {
|
|
const toggleEdit = () => {
|
|
|
setEditing(!editing)
|
|
setEditing(!editing)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const handleConfirm = async value => {
|
|
const handleConfirm = async value => {
|
|
|
- const { code = -1 } = await editFormApi(requestUrl, { [dataIndex]: value, id: dataId })
|
|
|
|
|
|
|
+ const params = { id: dataId }
|
|
|
|
|
+ if (editCellType === 'cascader') {
|
|
|
|
|
+ const [province = '', city = '', area = ''] = value
|
|
|
|
|
+ params.province = province
|
|
|
|
|
+ params.city = city
|
|
|
|
|
+ params.area = area
|
|
|
|
|
+ } else {
|
|
|
|
|
+ params[dataIndex] = value
|
|
|
|
|
+ }
|
|
|
|
|
+ const { code = -1 } = await editFormApi(requestUrl, { ...params })
|
|
|
if (code === consts.RET_CODE.SUCCESS) {
|
|
if (code === consts.RET_CODE.SUCCESS) {
|
|
|
toggleEdit()
|
|
toggleEdit()
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
const save = async e => {
|
|
const save = async e => {
|
|
|
- const { value = '' } = e.currentTarget
|
|
|
|
|
|
|
+ const { value } = e.currentTarget
|
|
|
if (value === record || !value) {
|
|
if (value === record || !value) {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
@@ -50,36 +58,34 @@ const EditableFormItem = ({ label, dataIndex, record, dataId, span, editCellType
|
|
|
toggleEdit()
|
|
toggleEdit()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // function handleSelectChange(value) {
|
|
|
|
|
- // console.log(`selected ${value}`)
|
|
|
|
|
- // }
|
|
|
|
|
- // const handleOnChange = e => {
|
|
|
|
|
- // setVal(value)
|
|
|
|
|
- // const { value = '' } = e.currentTarget
|
|
|
|
|
- // }
|
|
|
|
|
-
|
|
|
|
|
let cell = null
|
|
let cell = null
|
|
|
|
|
|
|
|
if (editing) {
|
|
if (editing) {
|
|
|
const options = (
|
|
const options = (
|
|
|
<>
|
|
<>
|
|
|
- <Option key="男" vlaue="男">男</Option>
|
|
|
|
|
- <Option key="女" vlaue="女">女</Option>
|
|
|
|
|
|
|
+ <Option key="男" vlaue="男">
|
|
|
|
|
+ 男
|
|
|
|
|
+ </Option>
|
|
|
|
|
+ <Option key="女" vlaue="女">
|
|
|
|
|
+ 女
|
|
|
|
|
+ </Option>
|
|
|
</>
|
|
</>
|
|
|
)
|
|
)
|
|
|
switch (editCellType) {
|
|
switch (editCellType) {
|
|
|
case 'cascader':
|
|
case 'cascader':
|
|
|
- // cell = ()
|
|
|
|
|
|
|
+ cell = (
|
|
|
|
|
+ <LazyCascader
|
|
|
|
|
+ defaultValue={val}
|
|
|
|
|
+ onChange={value => save({ currentTarget: { value } })}></LazyCascader>
|
|
|
|
|
+ )
|
|
|
break
|
|
break
|
|
|
case 'select':
|
|
case 'select':
|
|
|
cell = (
|
|
cell = (
|
|
|
<Select
|
|
<Select
|
|
|
defaultValue={val}
|
|
defaultValue={val}
|
|
|
- ref={inputRef}
|
|
|
|
|
// onBlur={save}
|
|
// onBlur={save}
|
|
|
- onChange={(value) => save({ currentTarget: { value } })}
|
|
|
|
|
- className="zh-width-100P"
|
|
|
|
|
- >
|
|
|
|
|
|
|
+ onChange={value => save({ currentTarget: { value } })}
|
|
|
|
|
+ className="zh-width-100P">
|
|
|
{options}
|
|
{options}
|
|
|
</Select>
|
|
</Select>
|
|
|
)
|
|
)
|
|
@@ -87,11 +93,11 @@ const EditableFormItem = ({ label, dataIndex, record, dataId, span, editCellType
|
|
|
default:
|
|
default:
|
|
|
cell = (
|
|
cell = (
|
|
|
<Input
|
|
<Input
|
|
|
- ref={inputRef}
|
|
|
|
|
|
|
+ ref={iRef}
|
|
|
onPressEnter={save}
|
|
onPressEnter={save}
|
|
|
onBlur={save}
|
|
onBlur={save}
|
|
|
defaultValue={val}
|
|
defaultValue={val}
|
|
|
- // onChange={handleOnChange}
|
|
|
|
|
|
|
+ // onChange={handleOnChange}
|
|
|
/>
|
|
/>
|
|
|
)
|
|
)
|
|
|
break
|
|
break
|