Skip to content

Commit

Permalink
Add onEvaluatedDataChange
Browse files Browse the repository at this point in the history
Co-authored-by: Iddan Aaronsohn <mail@aniddan.com>
  • Loading branch information
BALURAM1226 and iddan committed Mar 6, 2024
1 parent feb3c51 commit b061c81
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Spreadsheet.test.tsx
Expand Up @@ -97,6 +97,7 @@ const EXAMPLE_DATA = createEmptyMatrix<CellType>(ROWS, COLUMNS);
const EXAMPLE_PROPS: Props<CellType> = {
data: EXAMPLE_DATA,
onChange: jest.fn(),
onEvaluatedDataChange: jest.fn(),
};
const EXAMPLE_VALUE: Value = "EXAMPLE_VALUE";
const EXAMPLE_CELL: CellType = { value: EXAMPLE_VALUE };
Expand Down
14 changes: 14 additions & 0 deletions src/Spreadsheet.tsx
Expand Up @@ -119,6 +119,8 @@ export type Props<CellType extends Types.CellBase> = {
nextCell: null | CellType,
coords: null | Point.Point
) => void;
/** Callback called when the Spreadsheet's evaluated data changes. */
onEvaluatedDataChange?: (data: Matrix.Matrix<CellType>) => void;
};

/**
Expand Down Expand Up @@ -146,6 +148,7 @@ const Spreadsheet = <CellType extends Types.CellBase>(
onActivate = () => {},
onBlur = () => {},
onCellCommit = () => {},
onEvaluatedDataChange = () => {},
} = props;
type State = Types.StoreState<CellType>;

Expand Down Expand Up @@ -242,6 +245,17 @@ const Spreadsheet = <CellType extends Types.CellBase>(
prevDataRef.current = state.model.data;
}, [state.model.data, onChange, props.data]);

const prevEvaluatedDataRef = React.useRef<Matrix.Matrix<CellType>>(
state.model.evaluatedData
);
React.useEffect(() => {
if (state?.model?.evaluatedData !== prevEvaluatedDataRef?.current) {
onEvaluatedDataChange(state?.model?.evaluatedData);
}

prevEvaluatedDataRef.current = state.model.evaluatedData;
}, [state?.model?.evaluatedData, onEvaluatedDataChange]);

// Listen to selection changes
const prevSelectedRef = React.useRef<Selection>(state.selected);
React.useEffect(() => {
Expand Down

0 comments on commit b061c81

Please sign in to comment.