2018-08-22 08:39:42 +00:00
|
|
|
import React from 'react';
|
2018-10-09 02:56:59 +00:00
|
|
|
import { gettext } from '../../../utils/constants';
|
2018-08-22 08:39:42 +00:00
|
|
|
import { Button, Modal, ModalHeader, Input, ModalBody, ModalFooter } from 'reactstrap';
|
|
|
|
|
|
|
|
class Rename extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
newName: '',
|
|
|
|
};
|
|
|
|
this.newInput = React.createRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChange = (e) => {
|
|
|
|
this.setState({
|
|
|
|
newName: e.target.value,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit = () => {
|
|
|
|
this.props.onRename(this.state.newName);
|
|
|
|
}
|
|
|
|
|
2018-08-27 09:12:27 +00:00
|
|
|
handleKeyPress = (e) => {
|
|
|
|
if (e.key === 'Enter') {
|
2018-09-29 10:32:53 +00:00
|
|
|
this.handleSubmit();
|
2018-08-27 09:12:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 08:39:42 +00:00
|
|
|
toggle = () => {
|
|
|
|
this.props.toggleCancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
|
|
|
this.setState({
|
|
|
|
newName: this.props.currentNode.name
|
2018-09-29 10:32:53 +00:00
|
|
|
});
|
2018-08-22 08:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.changeState(this.props.currentNode);
|
|
|
|
this.newInput.focus();
|
|
|
|
let type = this.props.currentNode.type;
|
2018-09-29 10:32:53 +00:00
|
|
|
if (type === 'file') {
|
|
|
|
var endIndex = this.props.currentNode.name.lastIndexOf('.md');
|
|
|
|
this.newInput.setSelectionRange(0, endIndex, 'forward');
|
2018-08-22 08:39:42 +00:00
|
|
|
} else {
|
|
|
|
this.newInput.setSelectionRange(0, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
this.changeState(nextProps.currentNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
changeState(currentNode) {
|
|
|
|
this.setState({newName: currentNode.name});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let type = this.props.currentNode.type;
|
|
|
|
return (
|
|
|
|
<Modal isOpen={true} toggle={this.toggle}>
|
2018-09-29 10:32:53 +00:00
|
|
|
<ModalHeader toggle={this.toggle}>{type === 'file' ? gettext('Rename File') : gettext('Rename Folder') }</ModalHeader>
|
2018-08-22 08:39:42 +00:00
|
|
|
<ModalBody>
|
2018-09-29 10:32:53 +00:00
|
|
|
<p>{type === 'file' ? gettext('Enter the new file name:'): gettext('Enter the new folder name:')}</p>
|
|
|
|
<Input onKeyPress={this.handleKeyPress} innerRef={input => {this.newInput = input;}} placeholder="newName" value={this.state.newName} onChange={this.handleChange} />
|
2018-08-22 08:39:42 +00:00
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter>
|
2018-09-29 10:32:53 +00:00
|
|
|
<Button color="primary" onClick={this.handleSubmit}>{gettext('Submit')}</Button>
|
|
|
|
<Button color="secondary" onClick={this.toggle}>{gettext('Cancel')}</Button>
|
2018-08-22 08:39:42 +00:00
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
2018-09-29 10:32:53 +00:00
|
|
|
);
|
2018-08-22 08:39:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Rename;
|