Skip to content

Commit

Permalink
remove cell locks
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-kot committed Jul 28, 2023
1 parent d7a68c7 commit dd60a22
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
5 changes: 3 additions & 2 deletions src/table/grid-navigation/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ export interface GridNavigationAPI {
focusCell: (coordinates: { rowIndex: number; colIndex: number }) => void;
}

export interface FocusedItem {
export interface FocusedCell {
rowIndex: number;
colIndex: number;
element: HTMLElement;
cellElement: HTMLTableCellElement;
element: Element;
}
47 changes: 20 additions & 27 deletions src/table/grid-navigation/use-grid-navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

import { useEffect, useMemo } from 'react';
import { findFocusinItem } from './utils';
import { FocusedItem, GridNavigationAPI, GridNavigationProps } from './interfaces';
import { findFocusinCell } from './utils';
import { FocusedCell, GridNavigationAPI, GridNavigationProps } from './interfaces';

export function useGridNavigation({
tableRole,
Expand Down Expand Up @@ -46,7 +46,7 @@ class GridNavigationModel {
private _container: null | HTMLElement = null;

// State
private focusedItem: null | FocusedItem = null;
private focusedCell: null | FocusedCell = null;

public init(container: HTMLElement) {
this._container = container;
Expand Down Expand Up @@ -93,36 +93,29 @@ class GridNavigationModel {
return this._container;
}

private onFocusin(event: FocusEvent) {
const item = findFocusinItem(event);
if (!item) {
// TODO: is this helper still needed?
private updateFocusedCell(cell: null | FocusedCell) {
if (this.focusedCell?.element === cell?.element) {
return;
}
this.setFocusedItem(item);

console.log('FOCUS IN', item.rowIndex, item.colIndex, item.element);
}

private onFocusout() {
console.log('FOCUS OUT');
this.setFocusedItem(null);
}

private onKeydown() {
console.log('onkeydown');
this.focusedCell = cell;
}

private setFocusedItem(item: null | FocusedItem) {
if (
this.focusedItem?.rowIndex === item?.rowIndex &&
this.focusedItem?.colIndex === item?.colIndex &&
this.focusedItem?.element === item?.element
) {
private onFocusin = (event: FocusEvent) => {
const cell = findFocusinCell(event);
if (!cell) {
return;
}
}
this.updateFocusedCell(cell);

console.log('FOCUS IN', cell.rowIndex, cell.colIndex, cell.element);
};

private lockCellFocus() {}
private onFocusout = () => {
this.updateFocusedCell(null);
};

private unlockCellFocus() {}
private onKeydown = () => {
console.log('onkeydown');
};
}
6 changes: 3 additions & 3 deletions src/table/grid-navigation/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { FocusedItem } from './interfaces';
import { FocusedCell } from './interfaces';

export function findFocusinItem(event: FocusEvent): null | FocusedItem {
export function findFocusinCell(event: FocusEvent): null | FocusedCell {
if (!(event.target instanceof Element)) {
return null;
}
Expand All @@ -21,5 +21,5 @@ export function findFocusinItem(event: FocusEvent): null | FocusedItem {
return null;
}

return { rowIndex: isNaN(rowIndex) ? 0 : rowIndex, colIndex, element: closestCell };
return { rowIndex: isNaN(rowIndex) ? 0 : rowIndex, colIndex, cellElement: closestCell, element: event.target };
}

0 comments on commit dd60a22

Please sign in to comment.