mirror of
https://github.com/haiwen/seahub.git
synced 2025-07-19 17:39:39 +00:00
94 lines
2.5 KiB
JavaScript
94 lines
2.5 KiB
JavaScript
import React from 'react';
|
|
import PropTypes from 'prop-types';
|
|
import { gettext } from '../../utils/constants';
|
|
import { Button, Modal, ModalHeader, Input, ModalBody, ModalFooter, Alert } from 'reactstrap';
|
|
|
|
const propTypes = {
|
|
wiki: PropTypes.object,
|
|
onRename: PropTypes.func.isRequired,
|
|
toggleCancel: PropTypes.func.isRequired,
|
|
};
|
|
|
|
class RenameWikiDialog extends React.Component {
|
|
|
|
constructor(props) {
|
|
super(props);
|
|
this.state = {
|
|
newName: props.wiki.name,
|
|
errMessage: '',
|
|
isSubmitBtnActive: false,
|
|
};
|
|
this.newInput = React.createRef();
|
|
}
|
|
|
|
handleChange = (e) => {
|
|
this.setState({
|
|
isSubmitBtnActive: !!e.target.value.trim(),
|
|
newName: e.target.value
|
|
});
|
|
};
|
|
|
|
handleSubmit = () => {
|
|
let { isValid, errMessage } = this.validateInput();
|
|
if (!isValid) {
|
|
this.setState({ errMessage: errMessage });
|
|
} else {
|
|
this.props.onRename(this.state.newName.trim());
|
|
}
|
|
};
|
|
|
|
handleKeyDown = (e) => {
|
|
if (e.key === 'Enter') {
|
|
this.handleSubmit();
|
|
}
|
|
};
|
|
|
|
toggle = () => {
|
|
this.props.toggleCancel();
|
|
};
|
|
|
|
validateInput = () => {
|
|
let newName = this.state.newName.trim();
|
|
let isValid = true;
|
|
let errMessage = '';
|
|
if (!newName) {
|
|
isValid = false;
|
|
errMessage = gettext('Name is required.');
|
|
return { isValid, errMessage };
|
|
}
|
|
if (newName.indexOf('/') > -1) {
|
|
isValid = false;
|
|
errMessage = gettext('Name should not include ' + '\'/\'' + '.');
|
|
return { isValid, errMessage };
|
|
}
|
|
return { isValid, errMessage };
|
|
};
|
|
|
|
render() {
|
|
return (
|
|
<Modal isOpen={true} toggle={this.toggle}>
|
|
<ModalHeader toggle={this.toggle}>{gettext('Rename Wiki')}</ModalHeader>
|
|
<ModalBody>
|
|
<p>{gettext('New Wiki name')}</p>
|
|
<Input
|
|
onKeyDown={this.handleKeyDown}
|
|
innerRef={this.newInput}
|
|
placeholder="newName"
|
|
value={this.state.newName}
|
|
onChange={this.handleChange}
|
|
/>
|
|
{this.state.errMessage && <Alert color="danger" className="mt-2">{this.state.errMessage}</Alert>}
|
|
</ModalBody>
|
|
<ModalFooter>
|
|
<Button color="secondary" onClick={this.toggle}>{gettext('Cancel')}</Button>
|
|
<Button color="primary" onClick={this.handleSubmit} disabled={!this.state.isSubmitBtnActive}>{gettext('Submit')}</Button>
|
|
</ModalFooter>
|
|
</Modal>
|
|
);
|
|
}
|
|
}
|
|
|
|
RenameWikiDialog.propTypes = propTypes;
|
|
|
|
export default RenameWikiDialog;
|