import React from 'react'; import PropTypes from 'prop-types'; import { Button, Modal, ModalHeader, Input, ModalBody, ModalFooter, Form, FormGroup, Label, Alert } from 'reactstrap'; import { gettext } from '../../utils/constants'; const propTypes = { wikiPageName: PropTypes.string, onAddPage: PropTypes.func, handleClose: PropTypes.func, }; class AddWikiPageDialog extends React.Component { constructor(props) { super(props); const { wikiPageName = '' } = props; this.state = { wikiPageName, errMessage: '', isSubmitBtnActive: !!wikiPageName.length, }; this.inputRef = React.createRef(); } handleChange = (e) => { const isSubmitBtnActive = !!e.target.value.trim(); const wikiPageName = e.target.value; this.setState({ isSubmitBtnActive, wikiPageName }); }; handleSubmit = () => { if (!this.state.isSubmitBtnActive) return; // first param set false to prevent redirect to new page this.props.onAddPage(false, this.state.wikiPageName); this.props.handleClose(); }; handleKeyDown = (e) => { if (e.key === 'Enter') { e.preventDefault(); this.handleSubmit(); } }; onDialogLoad = () => { const input = this.inputRef.current; if (!input) return; input.focus(); const focusPosition = this.props.wikiPageName.length; input.setSelectionRange(focusPosition, focusPosition); }; render() { const { handleClose } = this.props; return ( {gettext('New page')}
{this.state.errMessage && {this.state.errMessage}}
); } } AddWikiPageDialog.propTypes = propTypes; export default AddWikiPageDialog;