components.Table.provider.TableActions.tsx Maven / Gradle / Ivy
The newest version!
/* eslint-disable @typescript-eslint/no-explicit-any */
import React, { FC, useMemo } from 'react'
import { createContext, useContext } from 'use-context-selector'
import { TableActions } from '../enum'
type TableActionContextValue = {
toggleExpandRow(rowValue: string, isOpen: boolean): void
selectRows(rowValues: string[]): void
deselectRows(rowValues: string[]): void
selectSingleRow(rowValue: string): void
setFocusOnRow(rowValue: string | null, model?: any): void
onRowClick(model: any): void
onChangeFilter(model: Record): void
onUpdateModel(model: Record, rowIndex: number): void
}
const MESSAGE = 'Need an implementation method\''
const tableActionsContext = createContext({
toggleExpandRow() {
console.warn(MESSAGE)
},
selectRows() {
console.warn(MESSAGE)
},
deselectRows() {
console.warn(MESSAGE)
},
selectSingleRow() {
console.warn(MESSAGE)
},
setFocusOnRow() {
console.warn(MESSAGE)
},
onRowClick() {
console.warn(MESSAGE)
},
onChangeFilter() {
console.warn(MESSAGE)
},
onUpdateModel() {
console.warn(MESSAGE)
},
})
type TableActionsProviderProps = {
actionListener(action: TableActions, payload: any): void
}
export const TableActionsProvider: FC = ({
actionListener,
children,
}) => {
const methods = useMemo(() => ({
toggleExpandRow(rowValue, isOpen) {
actionListener(TableActions.toggleExpandRow, { rowValue, isOpen })
},
selectRows(listRowValue) {
actionListener(TableActions.selectRows, { listRowValue })
},
deselectRows(listRowValue) {
actionListener(TableActions.deselectRows, { listRowValue })
},
selectSingleRow(rowValue) {
actionListener(TableActions.selectSingleRow, { rowValue })
},
setFocusOnRow(rowValue, model) {
actionListener(TableActions.setFocusOnRow, { rowValue, model })
},
onRowClick(model) {
actionListener(TableActions.onRowClick, { model })
},
onChangeFilter(model) {
actionListener(TableActions.onChangeFilter, { model })
},
onUpdateModel(model, rowIndex) {
actionListener(TableActions.onUpdateModel, { model, rowIndex })
},
}), [actionListener])
return (
{children}
)
}
TableActionsProvider.displayName = 'TableActionsProvider'
TableActionsProvider.defaultProps = {
actionListener: () => {},
}
export const useTableActions = () => {
const context = useContext(tableActionsContext)
if (!context) {
console.warn('useTableActions must be used in TableActionsProvider')
}
return context
}