Skip to content

Commit cca92ad

Browse files
committed
Implement GetEntireData
1 parent 91ef816 commit cca92ad

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

src/Spreadsheet.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,16 @@ export type Props<CellType extends Types.CellBase> = {
121121
) => void;
122122
};
123123

124+
export type Ref<CellType extends Types.CellBase> = {
125+
getEntireData(): Matrix.Matrix<CellType>;
126+
};
127+
124128
/**
125129
* The Spreadsheet component
126130
*/
127131
const Spreadsheet = <CellType extends Types.CellBase>(
128-
props: Props<CellType>
132+
props: Props<CellType>,
133+
ref: React.Ref<Ref<CellType>>
129134
): React.ReactElement => {
130135
const {
131136
className,
@@ -553,9 +558,21 @@ const Spreadsheet = <CellType extends Types.CellBase>(
553558
]
554559
);
555560

561+
React.useImperativeHandle(
562+
ref,
563+
(): Ref<CellType> => {
564+
return {
565+
getEntireData() {
566+
return state.model.data;
567+
},
568+
};
569+
},
570+
[state.model.data]
571+
);
572+
556573
return (
557574
<context.Provider value={reducerElements}>{rootNode}</context.Provider>
558575
);
559576
};
560577

561-
export default Spreadsheet;
578+
export default React.forwardRef(Spreadsheet);

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import DataViewer from "./DataViewer";
44

55
export default Spreadsheet;
66
export { Spreadsheet, DataEditor, DataViewer };
7-
export type { Props } from "./Spreadsheet";
7+
export type { Props, Ref } from "./Spreadsheet";
88
export { createEmpty as createEmptyMatrix } from "./matrix";
99
export type { Matrix } from "./matrix";
1010
export {

src/stories/Spreadsheet.stories.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
createEmptyMatrix,
55
Spreadsheet,
66
Props,
7+
Ref,
78
CellBase,
89
EntireWorksheetSelection,
910
Selection,
@@ -315,3 +316,21 @@ export const ControlledSelection: StoryFn<Props<StringCell>> = (props) => {
315316
</div>
316317
);
317318
};
319+
320+
export const GetEntireData: StoryFn<Props<StringCell>> = (props) => {
321+
const ref = React.useRef<Ref<StringCell>>(null);
322+
323+
const getEntireData = React.useCallback(() => {
324+
const data = ref.current?.getEntireData();
325+
console.log("data", data);
326+
}, []);
327+
328+
return (
329+
<>
330+
<div>
331+
<button onClick={getEntireData}>Get entire data</button>
332+
</div>
333+
<Spreadsheet ref={ref} {...props} />
334+
</>
335+
);
336+
};

0 commit comments

Comments
 (0)