An easy-to-use platform for EEG experimentation in the classroom
1import React, { Component } from 'react';
2import { debounce } from 'lodash';
3import { sanitizeTextInput } from '../utils/ui';
4import { Dialog, DialogContent, DialogHeader, DialogTitle } from './ui/dialog';
5import { Button } from './ui/button';
6
7interface Props {
8 open: boolean;
9 onClose: (arg0: string) => void;
10 onExit: () => void;
11 header: string;
12}
13
14interface State {
15 enteredText: string;
16 isError: boolean;
17}
18
19export default class InputModal extends Component<Props, State> {
20 constructor(props: Props) {
21 super(props);
22 this.state = {
23 enteredText: '',
24 isError: false,
25 };
26 this.handleTextEntry = debounce(this.handleTextEntry, 100).bind(this);
27 this.handleClose = this.handleClose.bind(this);
28 this.handleEnterSubmit = this.handleEnterSubmit.bind(this);
29 this.handleExit = this.handleExit.bind(this);
30 }
31
32 handleTextEntry(event: React.ChangeEvent<HTMLInputElement>) {
33 this.setState({ enteredText: event.target.value });
34 }
35
36 handleClose() {
37 if (this.state.enteredText.length >= 1) {
38 this.props.onClose(sanitizeTextInput(this.state.enteredText));
39 } else {
40 this.setState({ isError: true });
41 }
42 }
43
44 handleExit() {
45 this.props.onExit();
46 }
47
48 handleEnterSubmit(event: React.KeyboardEvent<HTMLInputElement>) {
49 if (event.key === 'Enter') {
50 this.handleClose();
51 }
52 }
53
54 render() {
55 return (
56 <Dialog
57 open={this.props.open}
58 onOpenChange={(open) => {
59 if (!open) this.handleExit();
60 }}
61 >
62 <DialogContent className="max-w-sm text-center">
63 <DialogHeader>
64 <DialogTitle>{this.props.header}</DialogTitle>
65 </DialogHeader>
66 <input
67 className={[
68 'w-full border rounded px-3 py-2',
69 this.state.isError ? 'border-red-500' : 'border-gray-300',
70 ].join(' ')}
71 onChange={this.handleTextEntry}
72 onKeyDown={this.handleEnterSubmit}
73 autoFocus
74 />
75 <div className="flex justify-end mt-4">
76 <Button variant="default" onClick={this.handleClose}>
77 OK
78 </Button>
79 </div>
80 </DialogContent>
81 </Dialog>
82 );
83 }
84}