chatscope / chatscope/chat-ui-kit-react
How to capture file data inside of Attachment Button?
- Dominant language
- JavaScript
- Stars
- 1.8k
- Forks
- 154
- PR merge metrics
- No merged PRs in 30d
Description
Hi everyone and @supersnager Thank you for this amazing package.
I try to implement a file attachment data using the `AttachmentButton` Component. I was wondering how can I combine the file data into the message data that was bind to the `MessageInput` Component?
The sample code as below:
`import React, { useState, useRef, useEffect } from 'react';
import { ChatContainer, ConversationHeader, MessageList, MessageInput, Message, AttachmentButton, SendButton } from '@chatscope/chat-ui-kit-react';
function ChatPage() {
const inputRef = useRef();
const fileRef = useRef(null);
const [messageInput, setMessageInput] = useState('');
const [messages, setMessages] = useState([]);
const [selectedFile, setSelectedFile] = useState('');
const handleSend = message => {
setMessages([{
message,
selectedFile,
direction: 'outgoing'
}]);
setMessageInput("");
inputRef.current.focus();
};
const onFileUpload = () => {
// Create an object of formData
const formData = new FormData();
// Update the formData object
formData.append(
"myFile",
selectedFile,
selectedFile.name
);
// Details of the uploaded file
console.log(selectedFile);
// Request made to the backend api
// Send formData object
// axios.post("api/uploadfile", formData);
};
useEffect(() => {
const theMessageList = () => {
// Execute the API here
}
theMessageList()
}, [messages])
return (
{messages.map((m, i) => )}
setMessageInput(messagesInput)}
value={messageInput} ref={inputRef}
attachButton={false}
sendButton={false}
style={{
flexGrow: 1,
borderTop: 0,
flexShrink: "initial",
paddingTop: "2.5vh",
paddingBottom: "2.5vh"
}}
/>
handleSend(messageInput)} disabled={messageInput.length === 0} style={{
fontSize: "2em",
marginLeft: 0,
paddingLeft: "0.2em",
paddingRight: "0.2em"
}} />
fileRef.current.click()}
onSend={onFileUpload}
/>
)
}
export default ChatPage`
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.