1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-07-17 08:41:40 +00:00
seahub/frontend/src/components/dialog/create-folder-dialog.js

109 lines
3.1 KiB
JavaScript
Raw Normal View History

import React from 'react';
2018-10-16 10:19:51 +00:00
import PropTypes from 'prop-types';
2019-01-25 07:44:04 +00:00
import { Button, Modal, ModalHeader, Input, ModalBody, ModalFooter, Form, FormGroup, Label, Alert } from 'reactstrap';
import { gettext } from '../../utils/constants';
2019-01-25 07:44:04 +00:00
import { Utils } from '../../utils/utils';
2018-10-16 10:19:51 +00:00
const propTypes = {
fileType: PropTypes.string,
parentPath: PropTypes.string.isRequired,
onAddFolder: PropTypes.func.isRequired,
2019-01-25 07:44:04 +00:00
checkDuplicatedName: PropTypes.func.isRequired,
2018-10-16 10:19:51 +00:00
addFolderCancel: PropTypes.func.isRequired,
};
class CreateForder extends React.Component {
constructor(props) {
super(props);
this.state = {
parentPath: '',
2019-01-25 07:44:04 +00:00
childName: '',
errMessage: '',
isSubmitBtnActive: false,
};
2018-10-16 10:19:51 +00:00
this.newInput = React.createRef();
}
2019-01-25 07:44:04 +00:00
componentDidMount() {
let parentPath = this.props.parentPath;
if (parentPath[parentPath.length - 1] === '/') { // mainPanel
this.setState({parentPath: parentPath});
} else {
this.setState({parentPath: parentPath + '/'}); // sidePanel
}
this.newInput.focus();
this.newInput.setSelectionRange(0,0);
}
handleChange = (e) => {
if (!e.target.value.trim()) {
this.setState({isSubmitBtnActive: false});
} else {
this.setState({isSubmitBtnActive: true});
}
2019-01-25 07:44:04 +00:00
this.setState({childName: e.target.value});
}
handleSubmit = () => {
2019-01-25 07:44:04 +00:00
let newName = this.state.childName;
let isDuplicated = this.checkDuplicatedName();
2019-01-25 07:44:04 +00:00
if (isDuplicated) {
let errMessage = gettext('The name "{name}" is already taken. Please choose a different name.');
2019-01-25 07:44:04 +00:00
errMessage = errMessage.replace('{name}', Utils.HTMLescape(newName));
this.setState({errMessage: errMessage});
} else {
let path = this.state.parentPath + newName;
this.props.onAddFolder(path);
}
}
2018-08-27 09:12:27 +00:00
handleKeyPress = (e) => {
if (e.key === 'Enter') {
this.handleSubmit();
2019-01-25 07:44:04 +00:00
e.preventDefault();
2018-08-27 09:12:27 +00:00
}
}
toggle = () => {
this.props.addFolderCancel();
}
2019-01-25 07:44:04 +00:00
checkDuplicatedName = () => {
let isDuplicated = this.props.checkDuplicatedName(this.state.childName);
return isDuplicated;
}
render() {
return (
<Modal isOpen={true} toggle={this.toggle}>
2018-10-16 10:19:51 +00:00
<ModalHeader toggle={this.toggle}>{gettext('New Folder')}</ModalHeader>
<ModalBody>
<Form>
2018-12-17 03:54:25 +00:00
<FormGroup>
<Label for="folderName">{gettext('Name')}</Label>
<Input
id="folderName"
value={this.state.childName}
innerRef={input => {this.newInput = input;}}
onKeyPress={this.handleKeyPress}
onChange={this.handleChange}
/>
</FormGroup>
</Form>
2019-01-25 07:44:04 +00:00
{this.state.errMessage && <Alert color="danger" className="mt-2">{this.state.errMessage}</Alert>}
</ModalBody>
<ModalFooter>
2018-10-16 10:19:51 +00:00
<Button color="secondary" onClick={this.toggle}>{gettext('Cancel')}</Button>
<Button color="primary" onClick={this.handleSubmit} disabled={!this.state.isSubmitBtnActive}>{gettext('Submit')}</Button>
</ModalFooter>
</Modal>
2018-10-16 10:19:51 +00:00
);
}
}
2018-10-16 10:19:51 +00:00
CreateForder.propTypes = propTypes;
export default CreateForder;