- udpated spreadsheet form field functionality;
This commit is contained in:
@@ -70,9 +70,6 @@ const Table = ({
|
||||
setColumns(newColumns);
|
||||
}, [columnsCfg, disabled]);
|
||||
|
||||
useEffect(() => {
|
||||
setTableValue(rowsCfg);
|
||||
}, [rowsCfg]);
|
||||
|
||||
useEffect(() => {
|
||||
const stateFieldData = pathOr([], ['stateFieldData'], tableColumns);
|
||||
@@ -102,16 +99,19 @@ const Table = ({
|
||||
setLastRow(!isEmpty(lastRowValues) ? lastRowData : []);
|
||||
}, [tableColumns]);
|
||||
|
||||
// Saved data (defaultValue) takes priority over template defaults (rowsCfg).
|
||||
// Using a single effect for both so the second render caused by rowsCfg changing
|
||||
// doesn't overwrite the saved data that was just loaded from the API.
|
||||
useEffect(() => {
|
||||
if (!equal(tableValue, defaultValue)) {
|
||||
setTableValue(defaultValue);
|
||||
const next = (defaultValue && !isEmpty(defaultValue)) ? defaultValue : rowsCfg;
|
||||
if (!equal(tableValue, next)) {
|
||||
setTableValue(next);
|
||||
}
|
||||
}, [defaultValue]);
|
||||
}, [rowsCfg, defaultValue]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
useEffect(() => {
|
||||
setTableValue(defaultValue);
|
||||
register(fieldName, properConfig(config));
|
||||
}, []);
|
||||
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
@@ -61,6 +61,7 @@ const Spreadsheet = ({ fieldName, label, errors = {}, defaultValue, setDataFn, t
|
||||
const isRestoringRef = useRef(false);
|
||||
const tagCellMapRef = useRef({});
|
||||
const reinitializeRef = useRef(null);
|
||||
const initializedFromSavedRef = useRef(false);
|
||||
const fileInputRef = useRef(null);
|
||||
const addSheetsFileInputRef = useRef(null);
|
||||
|
||||
@@ -173,6 +174,7 @@ const Spreadsheet = ({ fieldName, label, errors = {}, defaultValue, setDataFn, t
|
||||
};
|
||||
|
||||
const initialData = parseWorkbook(defaultValue) || templateData || { name: 'Sheet' };
|
||||
initializedFromSavedRef.current = !!parseWorkbook(defaultValue);
|
||||
doInit(initialData);
|
||||
reinitializeRef.current = doInit;
|
||||
|
||||
@@ -186,6 +188,17 @@ const Spreadsheet = ({ fieldName, label, errors = {}, defaultValue, setDataFn, t
|
||||
};
|
||||
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
// If the component mounted before saved data was available (reset() cleared form values),
|
||||
// reinitialize univerjs once when the valid saved workbook arrives via defaultValue.
|
||||
useEffect(() => {
|
||||
if (initializedFromSavedRef.current) return; // already showing saved data
|
||||
if (!reinitializeRef.current) return; // init useEffect hasn't run yet
|
||||
const parsedDefault = parseWorkbook(defaultValue);
|
||||
if (!parsedDefault) return; // still no valid workbook
|
||||
initializedFromSavedRef.current = true; // prevent re-triggering on user edits
|
||||
reinitializeRef.current(parsedDefault);
|
||||
}, [defaultValue]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
useEffect(() => {
|
||||
const el = containerRef.current;
|
||||
if (!el) return;
|
||||
|
||||
@@ -116,10 +116,6 @@ const Table = ({
|
||||
setColumns(newColumns);
|
||||
}, [columnsCfg, disabled]);
|
||||
|
||||
useEffect(() => {
|
||||
setRows(rowsCfg);
|
||||
}, [rowsCfg]);
|
||||
|
||||
useEffect(() => {
|
||||
const stateFieldData = pathOr([], ['stateFieldData'], tableColumns);
|
||||
let rowsData = pathOr([], ['rowsData'], tableColumns);
|
||||
@@ -147,16 +143,19 @@ const Table = ({
|
||||
setLastRow(!isEmpty(lastRowValues) ? lastRowData : []);
|
||||
}, [tableColumns, shouldDisableNewRows]);
|
||||
|
||||
// Saved data (defaultValue) takes priority over template defaults (rowsCfg).
|
||||
// Using a single effect for both so the second render caused by rowsCfg changing
|
||||
// doesn't overwrite the saved data that was just loaded from the API.
|
||||
useEffect(() => {
|
||||
if (!equal(rows, defaultValue)) {
|
||||
setRows(defaultValue);
|
||||
const next = (defaultValue && !isEmpty(defaultValue)) ? defaultValue : rowsCfg;
|
||||
if (!equal(rows, next)) {
|
||||
setRows(next);
|
||||
}
|
||||
}, [defaultValue]);
|
||||
}, [rowsCfg, defaultValue]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
useEffect(() => {
|
||||
setRows(defaultValue);
|
||||
register(fieldName, properConfig(config));
|
||||
}, []);
|
||||
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
Reference in New Issue
Block a user