Skip to content

Commit

Permalink
feat: add support for changing the submit listener
Browse files Browse the repository at this point in the history
This feature allows to override the default behavior, so new messages
can be stored externally.
  • Loading branch information
mlopezFC committed Sep 13, 2024
1 parent 8dad95e commit a94aabb
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class ChatAssistant extends Div {
private MessageInput messageInput;
private Span whoIsTyping;
private boolean minimized = true;
private Registration defaultSubmitListenerRegistration;

public ChatAssistant() {
this(new ArrayList<>());
Expand All @@ -81,7 +82,7 @@ public ChatAssistant(List<Message> messages) {
this.add(content);
messageInput = new MessageInput();
messageInput.setSizeFull();
messageInput
defaultSubmitListenerRegistration = messageInput
.addSubmitListener(se -> this.sendMessage(Message.builder().messageTime(LocalDateTime.now())
.sender(Sender.builder().name("User").build()).content(se.getValue()).build()));
whoIsTyping = new Span();
Expand Down Expand Up @@ -140,11 +141,12 @@ public void clearWhoIsTyping() {
}

/**
* Adds a SubmitListener that will be notified when the user submits a message on the underlying messageInput
* Sets the SubmitListener that will be notified when the user submits a message on the underlying messageInput
* @param listener
* @return
*/
public Registration addSubmitListener(ComponentEventListener<SubmitEvent> listener) {
public Registration setSubmitListener(ComponentEventListener<SubmitEvent> listener) {
defaultSubmitListenerRegistration.remove();
return messageInput.addSubmitListener(listener);
}

Expand Down

0 comments on commit a94aabb

Please sign in to comment.