mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-22 03:47:09 +00:00
add rename-input component
This commit is contained in:
72
frontend/src/components/rename-input.js
Normal file
72
frontend/src/components/rename-input.js
Normal file
@@ -0,0 +1,72 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
const propTypes = {
|
||||
type: PropTypes.string,
|
||||
name: PropTypes.string.isRequired,
|
||||
onRenameConfirm: PropTypes.func.isRequired,
|
||||
onRenameCancel: PropTypes.func.isRequired,
|
||||
};
|
||||
class RenameInput extends React.Component {
|
||||
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
name: props.name
|
||||
};
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
this.refs.renameInput.focus();
|
||||
if (this.props.type === 'file') {
|
||||
var endIndex = this.props.name.lastIndexOf('.');
|
||||
this.refs.renameInput.setSelectionRange(0, endIndex, 'forward');
|
||||
} else {
|
||||
this.refs.renameInput.setSelectionRange(0, -1);
|
||||
}
|
||||
}
|
||||
|
||||
onChange = (e) => {
|
||||
this.setState({name: e.target.value});
|
||||
}
|
||||
|
||||
onClick = (e) => {
|
||||
e.nativeEvent.stopImmediatePropagation();
|
||||
}
|
||||
|
||||
onKeyPress = (e) => {
|
||||
if (e.key === 'Enter') {
|
||||
this.onRenameConfirm(e);
|
||||
}
|
||||
}
|
||||
|
||||
onRenameConfirm = (e) => {
|
||||
e.nativeEvent.stopImmediatePropagation();
|
||||
this.props.onRenameConfirm(this.state.name);
|
||||
}
|
||||
|
||||
onRenameCancel = (e) => {
|
||||
e.nativeEvent.stopImmediatePropagation();
|
||||
this.props.onRenameCancel();
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<div className="rename-container">
|
||||
<input
|
||||
ref="renameInput"
|
||||
value={this.state.name}
|
||||
onChange={this.onChange}
|
||||
onKeyPress={this.onKeyPress}
|
||||
onClick={this.onClick}
|
||||
/>
|
||||
<button className="btn btn-secondary sf2-icon-confirm confirm" onClick={this.onRenameConfirm}></button>
|
||||
<button className="btn btn-secondary sf2-icon-cancel cancel" onClick={this.onRenameCancel}></button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
RenameInput.propTypes = propTypes;
|
||||
|
||||
export default RenameInput;
|
Reference in New Issue
Block a user