update method

  1. @override
(PasswordModel, 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
(PasswordModel, Cmd?) update(Msg msg) {
  if (!_focused) {
    return (this, null);
  }

  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 PasswordCancelledMsg()));
    }

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

    if (keyMatches(key, [keyMap.submit])) {
      final validationError = _validate();
      if (validationError != null) {
        _error = validationError;
        return (this, null);
      }
      return (this, Cmd.message(PasswordSubmittedMsg(value)));
    }

    if (keyMatches(key, [keyMap.deleteBackward])) {
      _deleteBackward();
    } else if (keyMatches(key, [keyMap.deleteForward])) {
      _deleteForward();
    } else if (keyMatches(key, [keyMap.deleteAll])) {
      _deleteAll();
    } else if (keyMatches(key, [keyMap.cursorLeft])) {
      _setCursorPosition(_pos - 1);
    } else if (keyMatches(key, [keyMap.cursorRight])) {
      _setCursorPosition(_pos + 1);
    } else if (keyMatches(key, [keyMap.cursorStart])) {
      _setCursorPosition(0);
    } else if (keyMatches(key, [keyMap.cursorEnd])) {
      _setCursorPosition(_value.length);
    } else if (key.runes.isNotEmpty) {
      // Regular character input
      _insertRunes(key.runes);
    }
  }

  // Update cursor
  final (newCursor, cursorCmd) = cursor.update(msg);
  cursor = newCursor;
  if (cursorCmd != null) cmds.add(cursorCmd);

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