diff --git a/modules/react-arborist/src/state/edit-slice.ts b/modules/react-arborist/src/state/edit-slice.ts index 297f620..6df9f2c 100644 --- a/modules/react-arborist/src/state/edit-slice.ts +++ b/modules/react-arborist/src/state/edit-slice.ts @@ -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) { +export function reducer(state: EditState = initialEditState(), action: ReturnType) { if (action.type === "EDIT") { return { ...state, id: action.id }; } else { diff --git a/modules/react-arborist/src/state/focus-slice.ts b/modules/react-arborist/src/state/focus-slice.ts index d38377c..fe23154 100644 --- a/modules/react-arborist/src/state/focus-slice.ts +++ b/modules/react-arborist/src/state/focus-slice.ts @@ -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) { @@ -15,7 +17,7 @@ export function treeBlur() { /* Reducer */ export function reducer( - state: FocusState = { id: null, treeFocused: false }, + state: FocusState = initialFocusState(), action: ReturnType | ReturnType, ) { if (action.type === "FOCUS") { diff --git a/modules/react-arborist/src/state/initial.ts b/modules/react-arborist/src/state/initial.ts index 3d758fc..623c16e 100644 --- a/modules/react-arborist/src/state/initial.ts +++ b/modules/react-arborist/src/state/initial.ts @@ -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): 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(), }, diff --git a/modules/react-arborist/src/state/open-slice.ts b/modules/react-arborist/src/state/open-slice.ts index 5bf9766..f0e5a2a 100644 --- a/modules/react-arborist/src/state/open-slice.ts +++ b/modules/react-arborist/src/state/open-slice.ts @@ -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) { @@ -38,7 +43,7 @@ function openMapReducer(state: OpenMap = {}, action: ActionTypes } export function reducer( - state: OpenSlice = { filtered: {}, unfiltered: {} }, + state: OpenSlice = initialOpenState(), action: ActionTypes, ): OpenSlice { if (!action.type.startsWith("VISIBILITY")) return state;