1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-07-19 01:29:05 +00:00
seahub/frontend/src/components/dialog/create-file-dialog.js

84 lines
2.3 KiB
JavaScript
Raw Normal View History

import React from 'react';
2018-10-16 10:19:51 +00:00
import PropTypes from 'prop-types';
import { Button, Modal, ModalHeader, Input, ModalBody, ModalFooter, Form, FormGroup, Label, Col, FormText } from 'reactstrap';
import { gettext } from '../../utils/constants';
2018-10-16 10:19:51 +00:00
const propTypes = {
fileType: PropTypes.string,
parentPath: PropTypes.string.isRequired,
onAddFile: PropTypes.func.isRequired,
addFileCancel: PropTypes.func.isRequired,
};
class CreateFile extends React.Component {
constructor(props) {
super(props);
this.state = {
parentPath: '',
childName: props.fileType,
};
2018-10-16 10:19:51 +00:00
this.newInput = React.createRef();
}
handleChange = (e) => {
this.setState({
childName: e.target.value,
2018-10-16 10:19:51 +00:00
}) ;
}
handleSubmit = () => {
2018-10-16 10:19:51 +00:00
let path = this.state.parentPath + this.state.childName;
this.props.onAddFile(path);
}
2018-08-27 09:12:27 +00:00
handleKeyPress = (e) => {
if (e.key === 'Enter') {
this.handleSubmit();
}
}
toggle = () => {
this.props.addFileCancel();
}
componentDidMount() {
2018-10-16 10:19:51 +00:00
if (this.props.parentPath === '/') {
this.setState({parentPath: this.props.parentPath});
} else {
2018-10-16 10:19:51 +00:00
this.setState({parentPath: this.props.parentPath + '/'});
}
this.newInput.focus();
this.newInput.setSelectionRange(0,0);
}
render() {
return (
<Modal isOpen={true} toggle={this.toggle}>
2018-10-16 10:19:51 +00:00
<ModalHeader toggle={this.toggle}>{gettext('New File')}</ModalHeader>
<ModalBody>
<Form>
<FormGroup row>
<Label sm={3}>Parent path: </Label>
<Col sm={9} className="parent-path"><FormText>{this.state.parentPath}</FormText></Col>
</FormGroup>
<FormGroup row>
2018-10-16 10:19:51 +00:00
<Label for="fileName" sm={3}>{gettext('Name')}: </Label>
<Col sm={9}>
2018-10-16 10:19:51 +00:00
<Input onKeyPress={this.handleKeyPress} innerRef={input => {this.newInput = input;}} id="fileName" placeholder={gettext('newName')} value={this.state.childName} onChange={this.handleChange}/>
</Col>
</FormGroup>
</Form>
</ModalBody>
<ModalFooter>
2018-10-16 10:19:51 +00:00
<Button color="primary" onClick={this.handleSubmit}>{gettext('Submit')}</Button>
<Button color="secondary" onClick={this.toggle}>{gettext('Cancel')}</Button>
</ModalFooter>
</Modal>
2018-10-16 10:19:51 +00:00
);
}
}
2018-10-16 10:19:51 +00:00
CreateFile.propTypes = propTypes;
export default CreateFile;