import React, { Component } from 'react'; import { debounce } from 'lodash'; import { sanitizeTextInput } from '../utils/ui'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from './ui/dialog'; import { Button } from './ui/button'; interface Props { open: boolean; onClose: (arg0: string) => void; onExit: () => void; header: string; } interface State { enteredText: string; isError: boolean; } export default class InputModal extends Component { constructor(props: Props) { super(props); this.state = { enteredText: '', isError: false, }; this.handleTextEntry = debounce(this.handleTextEntry, 100).bind(this); this.handleClose = this.handleClose.bind(this); this.handleEnterSubmit = this.handleEnterSubmit.bind(this); this.handleExit = this.handleExit.bind(this); } handleTextEntry(event: React.ChangeEvent) { this.setState({ enteredText: event.target.value }); } handleClose() { if (this.state.enteredText.length >= 1) { this.props.onClose(sanitizeTextInput(this.state.enteredText)); } else { this.setState({ isError: true }); } } handleExit() { this.props.onExit(); } handleEnterSubmit(event: React.KeyboardEvent) { if (event.key === 'Enter') { this.handleClose(); } } render() { return ( { if (!open) this.handleExit(); }} > {this.props.header}
); } }