update method

  1. @override
(MultiSearchModel<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
(MultiSearchModel<T>, Cmd?) update(Msg msg) {
  final cmds = <Cmd>[];

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

    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.confirm])) {
      final sortedIndices = _selected.toList()..sort();
      final selectedItems = sortedIndices.map((i) => _items[i]).toList();
      return (
        this,
        Cmd.message(
          MultiSearchSelectionMadeMsg<T>(selectedItems, sortedIndices),
        ),
      );
    }

    if (keyMatches(key, [keyMap.toggle])) {
      _toggleSelection();
      return (this, null);
    } else if (keyMatches(key, [keyMap.toggleAll])) {
      _toggleAll();
      return (this, null);
    } else 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);
    }
  }

  final oldValue = _input.value;
  final (newInput, inputCmd) = _input.update(msg);
  _input = newInput;
  if (inputCmd != null) cmds.add(inputCmd);

  if (_input.value != oldValue) {
    _runFilter();
    _cursor = 0;
    _updatePagination();
  }

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