2021-04-22 17:01:46 +10:00
|
|
|
import React, { ChangeEvent, KeyboardEvent, useState } from 'react';
|
2021-04-22 16:56:59 +10:00
|
|
|
import { Button, Grid, TextField } from '@material-ui/core';
|
2021-04-19 13:33:23 +10:00
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
messageHandler: (msg: string) => void;
|
2021-04-22 15:11:37 +10:00
|
|
|
sendMessage: () => void;
|
2021-04-19 13:33:23 +10:00
|
|
|
}
|
|
|
|
|
|
2021-04-22 17:01:46 +10:00
|
|
|
export default function MessageInput(props: Props) {
|
|
|
|
|
const [inputText, setInputText] = useState<string>('')
|
2021-04-19 13:33:23 +10:00
|
|
|
|
2021-04-22 17:01:46 +10:00
|
|
|
const sendMessage = () => {
|
|
|
|
|
props.sendMessage()
|
|
|
|
|
setInputText('')
|
2021-04-19 13:33:23 +10:00
|
|
|
}
|
|
|
|
|
|
2021-04-22 17:01:46 +10:00
|
|
|
const messageHandler = (event: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
setInputText(event.target.value)
|
|
|
|
|
props.messageHandler(event.target.value);
|
|
|
|
|
};
|
2021-04-19 13:33:23 +10:00
|
|
|
|
2021-04-22 17:01:46 +10:00
|
|
|
const keyPressHandler = (event: KeyboardEvent<HTMLInputElement>) => {
|
2021-04-22 15:11:37 +10:00
|
|
|
if (event.key === 'Enter') {
|
2021-04-22 17:01:46 +10:00
|
|
|
sendMessage()
|
2021-04-22 15:11:37 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 13:33:23 +10:00
|
|
|
return (
|
2021-04-22 16:56:59 +10:00
|
|
|
<Grid container spacing={2} direction='row' alignItems='center'>
|
|
|
|
|
<Grid item xs={11}>
|
|
|
|
|
<TextField variant='outlined'
|
|
|
|
|
label='Send a message'
|
2021-04-22 17:01:46 +10:00
|
|
|
value={inputText}
|
2021-04-22 16:56:59 +10:00
|
|
|
fullWidth
|
|
|
|
|
style={{ margin: 8 }}
|
|
|
|
|
margin="normal"
|
|
|
|
|
InputLabelProps={{
|
|
|
|
|
shrink: true,
|
|
|
|
|
}}
|
2021-04-22 17:01:46 +10:00
|
|
|
onChange={messageHandler}
|
|
|
|
|
onKeyPress={keyPressHandler}
|
2021-04-22 16:56:59 +10:00
|
|
|
/>
|
|
|
|
|
</Grid>
|
|
|
|
|
<Grid item xs={1}>
|
2021-04-22 17:01:46 +10:00
|
|
|
<Button variant="contained" color="primary" size="large" onClick={sendMessage}>
|
2021-04-22 16:56:59 +10:00
|
|
|
Send
|
|
|
|
|
</Button>
|
|
|
|
|
</Grid>
|
|
|
|
|
</Grid>
|
2021-04-19 13:33:23 +10:00
|
|
|
);
|
|
|
|
|
}
|