-
Notifications
You must be signed in to change notification settings - Fork 218
docs: Document how to compose MessageInput with modular upload for file attachments #5474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Artur-
wants to merge
1
commit into
main
Choose a base branch
from
modular-upload-messageinput
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/com/vaadin/demo/component/messages/MessageInputUpload.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package com.vaadin.demo.component.messages; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| import com.vaadin.demo.DemoExporter; // hidden-source-line | ||
| import com.vaadin.flow.component.html.Div; | ||
| import com.vaadin.flow.component.messages.MessageInput; | ||
| import com.vaadin.flow.component.messages.MessageList; | ||
| import com.vaadin.flow.component.messages.MessageListItem; | ||
| import com.vaadin.flow.component.orderedlayout.FlexComponent; | ||
| import com.vaadin.flow.component.orderedlayout.HorizontalLayout; | ||
| import com.vaadin.flow.component.orderedlayout.VerticalLayout; | ||
| import com.vaadin.flow.component.upload.UploadButton; | ||
| import com.vaadin.flow.component.upload.UploadFileList; | ||
| import com.vaadin.flow.component.upload.UploadFileListVariant; | ||
| import com.vaadin.flow.component.upload.UploadManager; | ||
| import com.vaadin.flow.router.Route; | ||
| import com.vaadin.flow.server.streams.UploadHandler; | ||
|
|
||
| @Route("message-input-upload") | ||
| public class MessageInputUpload extends Div { | ||
|
|
||
| // Pending files, keyed by file name | ||
| private final Map<String, byte[]> pendingFiles = new LinkedHashMap<>(); | ||
|
|
||
| public MessageInputUpload() { | ||
| // tag::snippet[] | ||
| var handler = UploadHandler.inMemory((metadata, data) -> { | ||
| pendingFiles.put(metadata.fileName(), data); | ||
| }); | ||
|
|
||
| var manager = new UploadManager(this, handler); | ||
| manager.setMaxFiles(5); | ||
| manager.setMaxFileSize(10L * 1024 * 1024); // 10 MB | ||
|
|
||
| // Remove files from the pending map when the user removes them | ||
| manager.addFileRemovedListener(event -> { | ||
| pendingFiles.remove(event.getFileName()); | ||
| }); | ||
|
|
||
| var uploadButton = new UploadButton(manager); | ||
|
|
||
| var fileList = new UploadFileList(manager); | ||
| fileList.addThemeVariants(UploadFileListVariant.THUMBNAILS); | ||
| fileList.setWidthFull(); | ||
|
|
||
| var messageInput = new MessageInput(); | ||
| var messageList = new MessageList(); | ||
|
|
||
| messageInput.addSubmitListener(event -> { | ||
| var message = new MessageListItem(event.getValue(), | ||
| Instant.now(), "You"); | ||
| message.setUserColorIndex(1); | ||
|
|
||
| // Add pending files as attachments | ||
| for (var entry : pendingFiles.entrySet()) { | ||
| message.addAttachment(new MessageListItem.Attachment( | ||
| entry.getKey(), "#", "application/octet-stream")); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part doesn't look helpful. It's not clear how to convert something received from the upload handler into a URL that you can provide here. Also the mime type doesn't match what was uploaded. |
||
| } | ||
|
|
||
| var items = new ArrayList<>(messageList.getItems()); | ||
| items.add(message); | ||
| messageList.setItems(items); | ||
|
|
||
| // Clear pending attachments | ||
| pendingFiles.clear(); | ||
| manager.clearFileList(); | ||
| }); | ||
|
|
||
| var inputLayout = new HorizontalLayout(uploadButton, messageInput); | ||
| inputLayout.setWidthFull(); | ||
| inputLayout.expand(messageInput); | ||
| inputLayout.setAlignItems(FlexComponent.Alignment.END); | ||
|
|
||
| var layout = new VerticalLayout(messageList, fileList, inputLayout); | ||
| layout.expand(messageList); | ||
| layout.setSizeFull(); | ||
| // end::snippet[] | ||
|
|
||
| setSizeFull(); // hidden-source-line | ||
| add(layout); | ||
| } | ||
|
|
||
| public static class Exporter // hidden-source-line | ||
| extends DemoExporter<MessageInputUpload> { // hidden-source-line | ||
| } // hidden-source-line | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example doesn't work, upload button and the file list are not rendered / registered as custom components.