|
|
@@ -24,11 +24,13 @@ const EditableFormItem = ({
|
|
|
const options = useOptions(dataIndex)
|
|
|
const [editing, setEditing] = useState(false)
|
|
|
const iRef = useRef(null)
|
|
|
- const [val, setVal] = useState('')
|
|
|
- const [extraVal, setExtraVal] = useState('')
|
|
|
+ const [state, setState] = useState({
|
|
|
+ val: '',
|
|
|
+ extraVal: ''
|
|
|
+ })
|
|
|
+
|
|
|
useEffect(() => {
|
|
|
- setVal('')
|
|
|
- setExtraVal('')
|
|
|
+ setState({ ...state, val: '', extraVal: '' })
|
|
|
}, [])
|
|
|
useEffect(() => {
|
|
|
// 处于编辑状态自动聚焦, 级联有问题暂无解决办法
|
|
|
@@ -36,6 +38,8 @@ const EditableFormItem = ({
|
|
|
if (iRef.current.selectRef) {
|
|
|
// select选择器相关
|
|
|
iRef.current.selectRef.current.focus()
|
|
|
+ } else if (iRef.current.cascadeRef) {
|
|
|
+ iRef.current.cascadeRef.current.focus()
|
|
|
} else {
|
|
|
iRef.current.focus()
|
|
|
}
|
|
|
@@ -47,16 +51,18 @@ const EditableFormItem = ({
|
|
|
type: 'district/fetchWithValues',
|
|
|
payload: record
|
|
|
})
|
|
|
+ setState({ ...state, popupVisible: true })
|
|
|
}
|
|
|
// 当val为空时,需要设置默认值
|
|
|
- if (!val) {
|
|
|
- setVal(record)
|
|
|
+ if (!state.val) {
|
|
|
+ // setVal(record)
|
|
|
+ setState({ ...state, val: '' })
|
|
|
}
|
|
|
- }, [record, editing])
|
|
|
+ }, [record, editing, iRef])
|
|
|
|
|
|
// 单独设置一个依赖,防止dataId变化
|
|
|
useEffect(() => {
|
|
|
- setVal(record)
|
|
|
+ setState({ ...state, val: record })
|
|
|
}, [dataId])
|
|
|
|
|
|
const toggleEdit = () => {
|
|
|
@@ -83,14 +89,15 @@ const EditableFormItem = ({
|
|
|
}
|
|
|
}
|
|
|
const save = async e => {
|
|
|
+ // console.log('失去焦点了', e)
|
|
|
+ // console.log(e)
|
|
|
const { value, local = '' } = e.currentTarget
|
|
|
if (value === record || !value) {
|
|
|
toggleEdit()
|
|
|
return
|
|
|
}
|
|
|
await handleConfirm(value)
|
|
|
- local && setExtraVal(local)
|
|
|
- setVal(value)
|
|
|
+ setState({ ...state, val: value, extraVal: local })
|
|
|
toggleEdit()
|
|
|
}
|
|
|
|
|
|
@@ -105,15 +112,26 @@ const EditableFormItem = ({
|
|
|
cell = (
|
|
|
<LazyCascader
|
|
|
ref={iRef}
|
|
|
- defaultValue={val}
|
|
|
+ defaultValue={state.val}
|
|
|
+ allowClear={false}
|
|
|
className="zh-width-100P"
|
|
|
+ autoFocus={true}
|
|
|
onChange={(value, local) => save({ currentTarget: { value, local } })}
|
|
|
+ popupVisible={true}
|
|
|
onBlur={save}
|
|
|
/>
|
|
|
)
|
|
|
break
|
|
|
case 'select':
|
|
|
- cell = <ChangeSelect ref={iRef} defaultValue={val} options={options} onSave={save} />
|
|
|
+ cell = (
|
|
|
+ <ChangeSelect
|
|
|
+ ref={iRef}
|
|
|
+ defaultValue={state.val}
|
|
|
+ options={options}
|
|
|
+ onSave={save}
|
|
|
+ onBlur={save}
|
|
|
+ />
|
|
|
+ )
|
|
|
break
|
|
|
case 'textarea':
|
|
|
cell = (
|
|
|
@@ -121,7 +139,7 @@ const EditableFormItem = ({
|
|
|
ref={iRef}
|
|
|
onPressEnter={save}
|
|
|
onBlur={save}
|
|
|
- defaultValue={val}
|
|
|
+ defaultValue={state.val}
|
|
|
// onChange={handleOnChange}
|
|
|
/>
|
|
|
)
|
|
|
@@ -132,19 +150,19 @@ const EditableFormItem = ({
|
|
|
ref={iRef}
|
|
|
onPressEnter={save}
|
|
|
onBlur={save}
|
|
|
- defaultValue={val}
|
|
|
+ defaultValue={state.val}
|
|
|
// onChange={handleOnChange}
|
|
|
/>
|
|
|
)
|
|
|
break
|
|
|
}
|
|
|
} else {
|
|
|
- let content = val
|
|
|
+ let content = state.val
|
|
|
if (editCellType === 'cascader') {
|
|
|
- content = extraVal || dataSource.local
|
|
|
+ content = state.extraVal || dataSource.local
|
|
|
}
|
|
|
if (editCellType === 'select') {
|
|
|
- content = extraVal || dataSource.nature
|
|
|
+ content = state.extraVal || dataSource.nature
|
|
|
}
|
|
|
cell = (
|
|
|
<div className="editable-cell-value-wrap" onClick={toggleEdit}>
|