update method

  1. @override
(SearchModel<T>, Cmd?) update(
  1. Msg msg
)
override

Updates the component state in response to a message.

Returns the updated component (often this) and an optional command.

Implementation

@override
(SearchModel<T>, Cmd?) update(Msg msg) {
  final cmds = <Cmd>[];

  if (msg is KeyMsg) {
    final key = msg.key;

    // Check for Ctrl+C
    if (key.ctrl && key.runes.isNotEmpty && key.runes.first == 0x63) {
      return (this, Cmd.message(const SearchCancelledMsg()));
    }

    if (keyMatches(key, [keyMap.cancel])) {
      return (this, Cmd.message(const SearchCancelledMsg()));
    }

    if (keyMatches(key, [keyMap.select])) {
      if (_filteredItems.isNotEmpty) {
        final selected = _filteredItems[_cursor];
        return (
          this,
          Cmd.message(
            SearchSelectionMadeMsg<T>(selected.item, selected.index),
          ),
        );
      }
      return (this, null);
    }

    if (keyMatches(key, [keyMap.up])) {
      _cursorUp();
      return (this, null);
    } else if (keyMatches(key, [keyMap.down])) {
      _cursorDown();
      return (this, null);
    } else if (keyMatches(key, [keyMap.home])) {
      _goToStart();
      return (this, null);
    } else if (keyMatches(key, [keyMap.end])) {
      _goToEnd();
      return (this, null);
    } else if (keyMatches(key, [keyMap.pageUp])) {
      _pageUp();
      return (this, null);
    } else if (keyMatches(key, [keyMap.pageDown])) {
      _pageDown();
      return (this, null);
    }
  }

  // Forward other messages to input
  final oldValue = _input.value;
  final (newInput, inputCmd) = _input.update(msg);
  _input = newInput;
  if (inputCmd != null) cmds.add(inputCmd);

  // Re-run filter if query changed
  if (_input.value != oldValue) {
    _runFilter();
    _cursor = 0;
    _updatePagination();
  }

  return (this, cmds.isNotEmpty ? Cmd.batch(cmds) : null);
}