Skip to content

Commit 9d11c28

Browse files
committed
Share more cod in reducer and test
1 parent e337037 commit 9d11c28

2 files changed

Lines changed: 43 additions & 44 deletions

File tree

src/reducer.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import * as React from "react";
22
import { Action } from "@reduxjs/toolkit";
33
import * as Types from "./types";
44
import * as Actions from "./actions";
5-
import reducer, { INITIAL_STATE, hasKeyDownHandler } from "./reducer";
5+
import reducer, {
6+
INITIAL_STATE,
7+
hasKeyDownHandler,
8+
isActiveReadOnly,
9+
} from "./reducer";
610
import { createEmptyMatrix } from "./util";
711
import * as Point from "./point";
812
import * as Matrix from "./matrix";
@@ -137,3 +141,28 @@ describe("hasKeyDownHandler", () => {
137141
);
138142
});
139143
});
144+
145+
describe("isActiveReadOnly", () => {
146+
const cases = [
147+
["returns false if no active", INITIAL_STATE, false],
148+
[
149+
"returns false if active is not read only",
150+
{ ...INITIAL_STATE, active: Point.ORIGIN },
151+
false,
152+
],
153+
[
154+
"returns true if active is read only",
155+
{
156+
...INITIAL_STATE,
157+
data: [
158+
[{ readOnly: true, value: undefined }],
159+
] as Matrix.Matrix<Types.CellBase>,
160+
active: Point.ORIGIN,
161+
},
162+
true,
163+
],
164+
] as const;
165+
test.each(cases)("%s", (name, state, expected) => {
166+
expect(isActiveReadOnly(state)).toBe(expected);
167+
});
168+
});

src/reducer.ts

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -178,49 +178,17 @@ const reducer = createReducer(INITIAL_STATE, (builder) => {
178178
lastCommit: commit,
179179
};
180180
});
181-
builder.addCase(Actions.edit, (state) => {
182-
if (isActiveReadOnly(state)) {
183-
return;
184-
}
185-
return { ...state, mode: "edit" };
186-
});
187-
builder.addCase(Actions.view, (state) => {
188-
return { ...state, mode: "view" };
189-
});
190-
builder.addCase(Actions.clear, (state) => {
191-
if (!state.active) {
192-
return;
193-
}
194-
const selectedPoints = state.selected
195-
? Array.from(PointRange.iterate(state.selected))
196-
: [];
197-
const changes = selectedPoints.map((point) => {
198-
const cell = Matrix.get(point, state.data);
199-
return {
200-
...state,
201-
prevCell: cell || null,
202-
nextCell: null,
203-
};
204-
});
205-
return {
206-
...state,
207-
data: selectedPoints.reduce(
208-
(acc, point) => Matrix.set(point, undefined, acc),
209-
state.data
210-
),
211-
...commit(changes),
212-
};
213-
});
214-
builder.addCase(Actions.blur, (state) => {
215-
return { ...state, active: null };
216-
});
181+
builder.addCase(Actions.edit, edit);
182+
builder.addCase(Actions.view, view);
183+
builder.addCase(Actions.clear, clear);
184+
builder.addCase(Actions.blur, blur);
217185
builder.addCase(Actions.keyPress, (state, action) => {
218186
const { event } = action.payload;
219187
if (isActiveReadOnly(state) || event.metaKey) {
220188
return;
221189
}
222190
if (state.mode === "view" && state.active) {
223-
return { ...state, mode: "edit" };
191+
return edit(state);
224192
}
225193
return;
226194
});
@@ -447,14 +415,16 @@ export function hasKeyDownHandler(
447415
return getKeyDownHandler(state, event) !== undefined;
448416
}
449417

450-
function getActive<Cell extends Types.CellBase>(
418+
/** Returns whether the active cell is read only */
419+
export function isActiveReadOnly(state: Types.StoreState): boolean {
420+
const activeCell = getActive(state);
421+
return Boolean(activeCell?.readOnly);
422+
}
423+
424+
/** Gets active cell from given state */
425+
export function getActive<Cell extends Types.CellBase>(
451426
state: Types.StoreState<Cell>
452427
): Cell | null {
453428
const activeCell = state.active && Matrix.get(state.active, state.data);
454429
return activeCell || null;
455430
}
456-
457-
const isActiveReadOnly = (state: Types.StoreState<Types.CellBase>): boolean => {
458-
const activeCell = getActive(state);
459-
return Boolean(activeCell && activeCell.readOnly);
460-
};

0 commit comments

Comments
 (0)