import type { GridRowId } from '../types' import type { ApiSection } from './types' export type GridScrollApi = { /** * Scroll grid to row and column. * * @param rowId : id of row to scroll to. Passing null will not scroll the grid vertically. * @param columnId : id of column to scroll to. Passing null or omitting will not scroll the grid horizontally. Defaults to first column if the provided column id is not valid. * @param mode : scroll-mode. 'always' means it will always scroll. 'auto' will only scroll if not in viewport. */ scrollTo: ( rowId: GridRowId | null, columnId?: string | null, mode?: 'always' | 'auto' ) => void /** * Scroll to first row in grid. * @param columnId : id of column to scroll to. Passing null or omitting will not scroll the grid horizontally. Defaults to first column if the provided column id is not valid. */ scrollToTop: (columnId?: string | null) => void /** * Scroll to last row in grid. * @param columnId : id of column to scroll to. Passing null or omitting will not scroll the grid horizontally. Defaults to first column if the provided column id is not valid. */ scrollToBottom: (columnId?: string | null) => void } export const createScrollApi: ApiSection = (store, events) => { const api: GridScrollApi = { scrollTo(rowId, columnId, mode = 'always') { const state = store.getState() const targetRowIndex = rowId === null ? null : store.selectors.selectRowIds(state).indexOf(rowId) let targetColumnIndex: number | null = null let targetColumnId = columnId if (columnId != null) { const columnIds = store.selectors.selectColumnIds(state) targetColumnId = columnId && columnIds.includes(columnId) ? columnId : columnIds[0] targetColumnIndex = columnIds.indexOf(targetColumnId) } if ( (targetColumnIndex !== null && targetColumnIndex >= 0) || (targetRowIndex !== null && targetRowIndex >= 0) ) { events.emit( 'scrollToIndex', targetRowIndex, targetColumnIndex, mode ) } }, scrollToBottom(columnId) { const rowIds = store.selectors.selectRowIds(store.getState()) const rowId = rowIds[rowIds.length - 1] api.scrollTo(rowId, columnId) }, scrollToTop(columnId) { const rowId = store.selectors.selectRowIds(store.getState())[0] api.scrollTo(rowId, columnId) }, } return api }