Commit 2dbbe0ca by zhaochengxiang

主数据定义

parent 704f99db
...@@ -120,6 +120,8 @@ const UpdateField = (props) => { ...@@ -120,6 +120,8 @@ const UpdateField = (props) => {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [form] = Form.useForm(); const [form] = Form.useForm();
const tableRef = useRef(null); const tableRef = useRef(null);
const dataRef = useRef([]);
const editingKeyRef = useRef(null);
const { attrIsEditingFunction } = useContext(EditTemplateContext); const { attrIsEditingFunction } = useContext(EditTemplateContext);
const cols = [ const cols = [
...@@ -235,8 +237,10 @@ const UpdateField = (props) => { ...@@ -235,8 +237,10 @@ const UpdateField = (props) => {
useEffect(() => { useEffect(() => {
if (visible) { if (visible) {
setEditingKey(null); setEditingKey(null);
editingKeyRef.current = null;
if (!template) { if (!template) {
setData([]); setData([]);
dataRef.current = [];
} else { } else {
getFileds(); getFileds();
} }
...@@ -301,6 +305,7 @@ const UpdateField = (props) => { ...@@ -301,6 +305,7 @@ const UpdateField = (props) => {
(data||[]).sort(compare); (data||[]).sort(compare);
setData(data||[]); setData(data||[]);
dataRef.current = data||[];
onChange && onChange(data||[]); onChange && onChange(data||[]);
}, },
error: () => { error: () => {
...@@ -316,9 +321,10 @@ const UpdateField = (props) => { ...@@ -316,9 +321,10 @@ const UpdateField = (props) => {
if (result) { if (result) {
setKeyword(''); setKeyword('');
const newData = [...data, {name: ''}]; const newData = [...dataRef.current, {name: ''}];
setData(newData); setData(newData);
dataRef.current = newData;
setInsertIndex(newData.length-1); setInsertIndex(newData.length-1);
editItem(newData[newData.length-1]); editItem(newData[newData.length-1]);
...@@ -335,7 +341,7 @@ const UpdateField = (props) => { ...@@ -335,7 +341,7 @@ const UpdateField = (props) => {
if (result) { if (result) {
setKeyword(''); setKeyword('');
let newData = [...data]; let newData = [...dataRef.current];
const index = newData.findIndex((item) => record.name === item.name); const index = newData.findIndex((item) => record.name === item.name);
if (index === 0) { if (index === 0) {
...@@ -349,6 +355,7 @@ const UpdateField = (props) => { ...@@ -349,6 +355,7 @@ const UpdateField = (props) => {
} }
setData(newData); setData(newData);
dataRef.current = newData;
setTimeout(() => { setTimeout(() => {
document.getElementById(`field-`)?.scrollIntoView(); document.getElementById(`field-`)?.scrollIntoView();
...@@ -366,6 +373,7 @@ const UpdateField = (props) => { ...@@ -366,6 +373,7 @@ const UpdateField = (props) => {
} }
setData(newData); setData(newData);
dataRef.current = newData;
onChange && onChange(newData); onChange && onChange(newData);
} }
...@@ -374,6 +382,7 @@ const UpdateField = (props) => { ...@@ -374,6 +382,7 @@ const UpdateField = (props) => {
removeItemLogic(record); removeItemLogic(record);
} else { } else {
setEditingKey(null); setEditingKey(null);
editingKeyRef.current = null;
removeItemLogic(record); removeItemLogic(record);
} }
} }
...@@ -394,9 +403,11 @@ const UpdateField = (props) => { ...@@ -394,9 +403,11 @@ const UpdateField = (props) => {
form.setFieldsValue({...record, dataType: dataTypeJson}); form.setFieldsValue({...record, dataType: dataTypeJson});
setEditingKey(record?.name); setEditingKey(record?.name);
editingKeyRef.current = record?.name;
} catch { } catch {
form.setFieldsValue({...record, dataType: {}}); form.setFieldsValue({...record, dataType: {}});
setEditingKey(record?.name); setEditingKey(record?.name);
editingKeyRef.current = record?.name;
} }
} }
...@@ -410,7 +421,7 @@ const UpdateField = (props) => { ...@@ -410,7 +421,7 @@ const UpdateField = (props) => {
const save = async() => { const save = async() => {
try { try {
if (editingKey !== null) { if (editingKeyRef.current !== null) {
const row = await form.validateFields(); const row = await form.validateFields();
if ((row.dataType.name||'')==='') { if ((row.dataType.name||'')==='') {
...@@ -425,7 +436,7 @@ const UpdateField = (props) => { ...@@ -425,7 +436,7 @@ const UpdateField = (props) => {
}) })
const newData = [...data]; const newData = [...data];
const index = newData.findIndex((item) => editingKey === item.name); const index = newData.findIndex((item) => editingKeyRef.current === item.name);
//判断字段名称是否唯一 //判断字段名称是否唯一
let _index; let _index;
...@@ -455,7 +466,11 @@ const UpdateField = (props) => { ...@@ -455,7 +466,11 @@ const UpdateField = (props) => {
} }
setEditingKey(null); setEditingKey(null);
editingKeyRef.current = null;
setData(newData); setData(newData);
dataRef.current = newData;
onChange && onChange(newData); onChange && onChange(newData);
} }
...@@ -477,6 +492,7 @@ const UpdateField = (props) => { ...@@ -477,6 +492,7 @@ const UpdateField = (props) => {
}); });
setData(newData); setData(newData);
dataRef.current = newData;
onChange && onChange(newData); onChange && onChange(newData);
}, },
[data], [data],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment