Skip to content
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

Done #72

Open
wants to merge 1 commit into
base: dapetrov0/homework_2
Choose a base branch
from
Open

Done #72

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/components/Chat/index.js
Original file line number Diff line number Diff line change
@@ -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
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно просто 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 (
<div className='chat'>
<div className="message-list">
<div className="messages">
{messagesArr && messagesArr.map(item =>
<Message key={item} text={item.text}/>
)}

</div>
<input className="input-message"
value={messageInput}
onChange={this.changeInputMessage}
onKeyPress={this.sendMessageOnEnter}
/>
</div>
</div>
);
}
}

export default Chat;
18 changes: 18 additions & 0 deletions src/components/Message/Message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from "react";
import "./Message.css";

class Message extends React.Component {

render() {
const {
text
} = this.props;

return (
<span className='message'>{text}</span>
);
}
}


export default Message;