| 43 | |
| 44 | /** The UI pattern for a widget inside a grid cell. */ |
| 45 | export class GridCellWidgetPattern { |
| 46 | /** The html element that should receive focus. */ |
| 47 | readonly element: SignalLike<HTMLElement> = () => this.inputs.element(); |
| 48 | |
| 49 | /** The element that should receive focus. */ |
| 50 | readonly widgetHost: SignalLike<HTMLElement> = () => |
| 51 | resolveElement(this.inputs.focusTarget(), this.element()) ?? this.element(); |
| 52 | |
| 53 | /** Whether the widget is disabled. */ |
| 54 | readonly disabled: SignalLike<boolean> = computed( |
| 55 | () => this.inputs.disabled() || this.inputs.cell().disabled(), |
| 56 | ); |
| 57 | |
| 58 | /** The tab index for the widget. */ |
| 59 | readonly tabIndex: SignalLike<-1 | 0> = computed(() => { |
| 60 | if (this.inputs.focusTarget()) { |
| 61 | return -1; |
| 62 | } |
| 63 | return this.inputs.cell().widgetTabIndex(); |
| 64 | }); |
| 65 | |
| 66 | /** Whether the widget is the active widget in the cell. */ |
| 67 | readonly active: SignalLike<boolean> = computed( |
| 68 | () => this.inputs.cell().active() && this.inputs.cell().widget() === this, |
| 69 | ); |
| 70 | |
| 71 | /** Whether the widget is currently activated. */ |
| 72 | readonly isActivated: WritableSignalLike<boolean> = signal(false); |
| 73 | |
| 74 | /** The last event that caused the widget to be activated. */ |
| 75 | readonly lastActivateEvent: WritableSignalLike<KeyboardEvent | FocusEvent | undefined> = |
| 76 | signal(undefined); |
| 77 | |
| 78 | /** The last event that caused the widget to be deactivated. */ |
| 79 | readonly lastDeactivateEvent: WritableSignalLike<KeyboardEvent | FocusEvent | undefined> = |
| 80 | signal(undefined); |
| 81 | |
| 82 | /** The keyboard event manager for the widget. */ |
| 83 | readonly keydown = computed(() => { |
| 84 | const manager = new KeyboardEventManager(); |
| 85 | |
| 86 | // Simple widgets emit notification on interaction without capturing event flow |
| 87 | if (this.inputs.widgetType() === 'simple') { |
| 88 | return manager |
| 89 | .on('Enter', e => this.inputs.onActivate?.(e), { |
| 90 | preventDefault: false, |
| 91 | stopPropagation: false, |
| 92 | }) |
| 93 | .on(' ', e => this.inputs.onActivate?.(e), { |
| 94 | preventDefault: false, |
| 95 | stopPropagation: false, |
| 96 | }); |
| 97 | } |
| 98 | |
| 99 | // If a widget is activated, only listen to events that exits activate state. |
| 100 | if (this.isActivated()) { |
| 101 | manager.on('Escape', e => this.deactivate(e)); |
| 102 |
nothing calls this directly
no test coverage detected