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-22 17:05:19 +10:00
|
|
|
import { useWaku } from './WakuContext';
|
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) {
|
2021-04-22 17:02:10 +10:00
|
|
|
const [inputText, setInputText] = useState<string>('');
|
2021-04-22 17:05:19 +10:00
|
|
|
const { waku } = useWaku();
|
2021-04-19 13:33:23 +10:00
|
|
|
|
2021-04-22 17:01:46 +10:00
|
|
|
const sendMessage = () => {
|
2021-04-22 17:02:10 +10:00
|
|
|
props.sendMessage();
|
|
|
|
|
setInputText('');
|
|
|
|
|
};
|
2021-04-19 13:33:23 +10:00
|
|
|
|
2021-04-22 17:01:46 +10:00
|
|
|
const messageHandler = (event: ChangeEvent<HTMLInputElement>) => {
|
2021-04-22 17:02:10 +10:00
|
|
|
setInputText(event.target.value);
|
2021-04-22 17:01:46 +10:00
|
|
|
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:02:10 +10:00
|
|
|
sendMessage();
|
2021-04-22 15:11:37 +10:00
|
|
|
}
|
2021-04-22 17:02:10 +10:00
|
|
|
};
|
2021-04-22 15:11:37 +10:00
|
|
|
|
2021-04-22 17:02:10 +10:00
|
|
|
return (
|
2021-04-22 17:03:33 +10:00
|
|
|
<Grid container spacing={2} direction="row" alignItems="center">
|
2021-04-22 17:02:10 +10:00
|
|
|
<Grid item xs={11}>
|
2021-04-22 17:03:33 +10:00
|
|
|
<TextField
|
|
|
|
|
variant="outlined"
|
|
|
|
|
label="Send a message"
|
|
|
|
|
value={inputText}
|
|
|
|
|
fullWidth
|
|
|
|
|
style={{ margin: 8 }}
|
|
|
|
|
margin="normal"
|
|
|
|
|
InputLabelProps={{
|
|
|
|
|
shrink: true,
|
|
|
|
|
}}
|
|
|
|
|
onChange={messageHandler}
|
|
|
|
|
onKeyPress={keyPressHandler}
|
2021-04-22 17:05:19 +10:00
|
|
|
disabled={!waku}
|
2021-04-22 17:02:10 +10:00
|
|
|
/>
|
|
|
|
|
</Grid>
|
|
|
|
|
<Grid item xs={1}>
|
2021-04-22 17:03:33 +10:00
|
|
|
<Button
|
|
|
|
|
variant="contained"
|
|
|
|
|
color="primary"
|
|
|
|
|
size="large"
|
|
|
|
|
onClick={sendMessage}
|
2021-04-22 17:05:19 +10:00
|
|
|
disabled={!waku}
|
2021-04-22 17:03:33 +10:00
|
|
|
>
|
2021-04-22 17:02:10 +10:00
|
|
|
Send
|
|
|
|
|
</Button>
|
2021-04-22 16:56:59 +10:00
|
|
|
</Grid>
|
2021-04-22 17:02:10 +10:00
|
|
|
</Grid>
|
|
|
|
|
);
|
2021-04-19 13:33:23 +10:00
|
|
|
}
|