import type { Store } from '../state' import type { GridRowId } from '../types' import { findNextEditable } from '../utils/find-next-editable' import type { ApiSection } from './types' export type GridEditApi = { start: (location: { columnId: string; rowId: GridRowId }) => void stop: () => void next: () => void previous: () => void } const navigateToNextEditable = ( navigationApi: GridEditApi, store: Store, forward = true ) => { const next = findNextEditable(store, forward) if (next === null) { navigationApi.stop() return } const { columnId, rowId } = next store.dispatch({ type: 'updateEdit', payload: { columnId, rowId, }, }) } export const createEditApi: ApiSection = (store) => { const api: GridEditApi = { start: ({ columnId, rowId }) => { store.dispatch({ type: 'updateEdit', payload: { columnId, rowId }, }) }, stop: () => { store.dispatch({ type: 'updateEdit', payload: null, }) }, next: () => navigateToNextEditable(api, store, true), previous: () => navigateToNextEditable(api, store, false), } return api }