CxJS

Forwarding Keyboard Inputs

The List widget supports receiving keyboard events from other widgets through the pipeKeyDown prop. A common use case is a search interface where the user types in a TextField while navigating results with arrow keys.

<div
  controller={PageController}
  class="flex flex-col gap-2"
  style="width: 300px"
>
  <TextField
    value={m.query}
    placeholder="Search..."
    onKeyDown={(e, instance) =>
      instance.getControllerByType(PageController).onKeyDown(e)
    }
    inputAttrs={{ autoComplete: "off" }}
    autoFocus
  />
  <List
    records={m.filteredRecords}
    selection={{ type: KeySelection, bind: m.selection, keyField: "id" }}
    mod="bordered"
    style="height: 200px; overflow-y: auto"
    emptyText="No items found."
    recordAlias={m.$record}
    pipeKeyDown={(cb, instance) =>
      instance.getControllerByType(PageController).pipeKeyDown(cb)
    }
    focused
    scrollSelectionIntoView
  >
    <HighlightedSearchText text={m.$record.text} query={m.query} />
  </List>
</div>
  • Australia
  • Austria
  • Belgium
  • Brazil
  • Canada
  • Denmark
  • Finland
  • France
  • Germany
  • Italy
  • Japan
  • Netherlands
  • Norway
  • Spain
  • Sweden
  • Switzerland
  • United Kingdom
  • United States

How It Works

  1. The List’s pipeKeyDown prop receives a callback for forwarding keyboard events
  2. Store the callback in the controller using getControllerByType for type safety
  3. On the TextField’s onKeyDown, forward arrow and enter keys to the stored callback
  4. Use focused prop to enable keyboard navigation without clicking the list
  5. Use scrollSelectionIntoView to keep the selected item visible

See Also