Skip to content

Commit

Permalink
Exposing on grid api (clearFocus) to enable outsideDeselect imperatively
Browse files Browse the repository at this point in the history
  • Loading branch information
underfisk committed Oct 30, 2020
1 parent edde80f commit 22dff87
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/ApolloSpreadsheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { ApolloSpreadsheetProps } from './ApolloSpreadsheetProps'
import { useSort } from './sort/useSort'
import { useLogger } from './logger'
import { isFunctionType } from './helpers'
import { useApiExtends } from './api'

const useStyles = makeStyles(() => ({
root: {
Expand Down Expand Up @@ -91,6 +92,16 @@ export const ApolloSpreadSheet = forwardRef(
apiRef,
initialised,
})
const clearFocus = useCallback(() => {
if (gridFocused) {
logger.debug('Grid clearFocus() invoked')
setGridFocused(false)
apiRef.current.selectCell({
rowIndex: -1,
colIndex: -1,
})
}
}, [apiRef, gridFocused, logger])

const onClickAway = useCallback(
(event: React.MouseEvent<Document>) => {
Expand All @@ -102,23 +113,16 @@ export const ApolloSpreadSheet = forwardRef(
props.outsideClickDeselects(event.target as HTMLElement)
) {
logger.debug('Grid click away detected.')
setGridFocused(false)
return apiRef.current.selectCell({
rowIndex: -1,
colIndex: -1,
})
return clearFocus()
}

if (props.outsideClickDeselects) {
logger.debug('Grid click away detected.')
setGridFocused(false)
apiRef.current.selectCell({
rowIndex: -1,
colIndex: -1,
})
clearFocus()
}
},
[gridFocused, props, logger, apiRef],
[gridFocused, props, logger, clearFocus],
)

// Detect if any element is clicked again to enable focus
Expand All @@ -131,6 +135,7 @@ export const ApolloSpreadSheet = forwardRef(

useApiEventHandler(apiRef, CELL_CLICK, onCellMouseHandler)
useApiEventHandler(apiRef, CELL_DOUBLE_CLICK, onCellMouseHandler)
useApiExtends(apiRef, { clearFocus }, 'CoreApi')

return (
<ClickAwayListener onClickAway={onClickAway}>
Expand Down
5 changes: 5 additions & 0 deletions src/api/types/coreApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ export interface CoreApi extends EventEmitter {
* @param args
*/
dispatchEvent: (name: string, ...args: any[]) => void

/**
* Removes the focus from the grid and wipes the highlight
*/
clearFocus(): void
}

0 comments on commit 22dff87

Please sign in to comment.