diff --git a/src/components/Chat/index.js b/src/components/Chat/index.js new file mode 100644 index 0000000..51c22ee --- /dev/null +++ b/src/components/Chat/index.js @@ -0,0 +1,54 @@ +import React from "react"; +import "./Chat.css"; +import Message from "./../Message"; + +class Chat extends React.Component { + + state = { + messageInput: "", + messages: [] + }; + + changeInputMessage = (e) => { + this.setState({ + messageInput: e.target.value + }); + }; + + sendMessageOnEnter = (e) => { + if (e.key === "Enter") { + this.setState((prevState) => { + return { + messageInput: "", + messages: prevState.messages.concat([{ text: prevState.messageInput }]) + }; + }); + } + }; + + render() { + + const messagesArr = this.state.messages; + const messageInput = this.state.messageInput; + + return ( +
+
+
+ {messagesArr && messagesArr.map(item => + + )} + +
+ +
+
+ ); + } +} + +export default Chat; \ No newline at end of file diff --git a/src/components/Message/Message.js b/src/components/Message/Message.js new file mode 100644 index 0000000..00ed063 --- /dev/null +++ b/src/components/Message/Message.js @@ -0,0 +1,18 @@ +import React from "react"; +import "./Message.css"; + +class Message extends React.Component { + + render() { + const { + text + } = this.props; + + return ( + {text} + ); + } +} + + +export default Message;