| 39 | |
| 40 | /** The UI pattern for a grid, handling keyboard navigation, focus, and selection. */ |
| 41 | export class GridPattern { |
| 42 | /** The underlying grid behavior that this pattern is built on. */ |
| 43 | readonly gridBehavior: Grid<GridCellPattern>; |
| 44 | |
| 45 | /** The cells in the grid. */ |
| 46 | readonly cells = computed(() => this.gridBehavior.data.cells()); |
| 47 | |
| 48 | /** The tab index for the grid. */ |
| 49 | readonly tabIndex = computed(() => this.gridBehavior.gridTabIndex()); |
| 50 | |
| 51 | /** Whether the grid is disabled. */ |
| 52 | readonly disabled = computed(() => this.gridBehavior.gridDisabled()); |
| 53 | |
| 54 | /** Whether the grid is multi-selectable. */ |
| 55 | readonly multiSelectable = computed(() => |
| 56 | this.inputs.enableSelection() ? this.inputs.multi() : undefined, |
| 57 | ); |
| 58 | |
| 59 | /** The ID of the currently active descendant cell. */ |
| 60 | readonly activeDescendant = computed(() => this.gridBehavior.activeDescendant()); |
| 61 | |
| 62 | /** The currently active cell. */ |
| 63 | readonly activeCell = computed(() => this.gridBehavior.focusBehavior.activeCell()); |
| 64 | |
| 65 | /** The current selection anchor cell. */ |
| 66 | readonly anchorCell: SignalLike<GridCellPattern | undefined> = computed(() => |
| 67 | this.multiSelectable() ? this.gridBehavior.selectionAnchorCell() : undefined, |
| 68 | ); |
| 69 | |
| 70 | /** Whether to pause grid navigation and give the keyboard control to cell or widget. */ |
| 71 | readonly pauseNavigation: SignalLike<boolean> = computed(() => |
| 72 | this.gridBehavior.data |
| 73 | .cells() |
| 74 | .flat() |
| 75 | .reduce((res, c) => res || c.isActivated(), false), |
| 76 | ); |
| 77 | |
| 78 | /** Whether the focus is in the grid. */ |
| 79 | readonly isFocused = signal(false); |
| 80 | |
| 81 | /** Whether the grid has received focus once. */ |
| 82 | readonly hasBeenInteracted = signal(false); |
| 83 | |
| 84 | /** Whether the user is currently dragging to select a range of cells. */ |
| 85 | readonly dragging = signal(false); |
| 86 | |
| 87 | /** The key for navigating to the previous column. */ |
| 88 | readonly prevColKey = computed(() => |
| 89 | this.inputs.textDirection() === 'rtl' ? 'ArrowRight' : 'ArrowLeft', |
| 90 | ); |
| 91 | |
| 92 | /** The key for navigating to the next column. */ |
| 93 | readonly nextColKey = computed(() => |
| 94 | this.inputs.textDirection() === 'rtl' ? 'ArrowLeft' : 'ArrowRight', |
| 95 | ); |
| 96 | |
| 97 | /** The keydown event manager for the grid. */ |
| 98 | readonly keydown = computed(() => { |
nothing calls this directly
no test coverage detected