Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion modules/react-arborist/src/state/edit-slice.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/* Types */
export type EditState = { id: string | null };

export const initialEditState = (): EditState => ({ id: null });

/* Actions */
export function edit(id: string | null) {
return { type: "EDIT" as const, id };
}

/* Reducer */
export function reducer(state: EditState = { id: null }, action: ReturnType<typeof edit>) {
export function reducer(state: EditState = initialEditState(), action: ReturnType<typeof edit>) {
if (action.type === "EDIT") {
return { ...state, id: action.id };
} else {
Expand Down
4 changes: 3 additions & 1 deletion modules/react-arborist/src/state/focus-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

export type FocusState = { id: string | null; treeFocused: boolean };

export const initialFocusState = (): FocusState => ({ id: null, treeFocused: false });

/* Actions */

export function focus(id: string | null) {
Expand All @@ -15,7 +17,7 @@ export function treeBlur() {
/* Reducer */

export function reducer(
state: FocusState = { id: null, treeFocused: false },
state: FocusState = initialFocusState(),
action: ReturnType<typeof focus> | ReturnType<typeof treeBlur>,
) {
if (action.type === "FOCUS") {
Expand Down
9 changes: 6 additions & 3 deletions modules/react-arborist/src/state/initial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import { TreeProps } from "../types/tree-props.js";
import type { RootState } from "./root-reducer.js";
import { initialDndState } from "./dnd-slice.js";
import { initialDragState } from "./drag-slice.js";
import { initialEditState } from "./edit-slice.js";
import { initialFocusState } from "./focus-slice.js";
import { initialOpenState } from "./open-slice.js";
import { initialSelectionState } from "./selection-slice.js";

export const initialState = (props?: TreeProps<any>): RootState => ({
nodes: {
// Changes together
open: { filtered: {}, unfiltered: props?.initialOpenState ?? {} },
focus: { id: null, treeFocused: false },
edit: { id: null },
open: initialOpenState(props?.initialOpenState),
focus: initialFocusState(),
edit: initialEditState(),
drag: initialDragState(),
selection: initialSelectionState(),
},
Expand Down
7 changes: 6 additions & 1 deletion modules/react-arborist/src/state/open-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { ActionTypes } from "../types/utils.js";
export type OpenMap = { [id: string]: boolean };
export type OpenSlice = { unfiltered: OpenMap; filtered: OpenMap };

export const initialOpenState = (unfiltered: OpenMap = {}): OpenSlice => ({
filtered: {},
unfiltered,
});

/* Actions */
export const actions = {
open(id: string, filtered: boolean) {
Expand Down Expand Up @@ -38,7 +43,7 @@ function openMapReducer(state: OpenMap = {}, action: ActionTypes<typeof actions>
}

export function reducer(
state: OpenSlice = { filtered: {}, unfiltered: {} },
state: OpenSlice = initialOpenState(),
action: ActionTypes<typeof actions>,
): OpenSlice {
if (!action.type.startsWith("VISIBILITY")) return state;
Expand Down
Loading