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

104 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-12-25 08:28:17 +00:00
import React from 'react';
import PropTypes from 'prop-types';
2019-01-09 06:39:17 +00:00
import { gettext } from '../../utils/constants';
2019-01-24 09:15:01 +00:00
import Select from 'react-select';
import '../../css/select-editor.css';
2018-12-25 08:28:17 +00:00
const propTypes = {
isTextMode: PropTypes.bool.isRequired, // there will be two mode. first: text and select. second: just select
2018-12-26 08:07:22 +00:00
isEditIconShow: PropTypes.bool.isRequired,
2019-01-09 06:39:17 +00:00
options: PropTypes.array.isRequired,
currentOption: PropTypes.string.isRequired,
translateOption: PropTypes.func.isRequired,
2019-01-24 09:15:01 +00:00
translateExplanation: PropTypes.func,
2019-01-14 05:33:36 +00:00
onOptionChanged: PropTypes.func.isRequired,
2018-12-25 08:28:17 +00:00
};
2019-01-09 06:39:17 +00:00
class SelectEditor extends React.Component {
2018-12-25 08:28:17 +00:00
constructor(props) {
super(props);
this.state = {
isEditing: false,
}
2019-01-24 09:15:01 +00:00
this.options = [];
2018-12-25 08:28:17 +00:00
}
componentDidMount() {
document.addEventListener('click', this.onHideSelect);
2019-01-24 09:15:01 +00:00
this.setOptions();
}
setOptions = () => {
this.options = [];
const options = this.props.options;
for (let i = 0, length = options.length; i < length; i++) {
let option = {};
option.value = options[i];
option.label = <div>{this.props.translateOption(options[i])}{ this.props.translateExplanation && <div className="permission-editor-explanation">{this.props.translateExplanation(options[i])}</div>}</div>;
this.options.push(option);
}
2018-12-25 08:28:17 +00:00
}
componentWillUnmount() {
document.removeEventListener('click', this.onHideSelect);
}
2019-01-24 09:15:01 +00:00
onEditPermission = (e) => {
2018-12-25 08:28:17 +00:00
e.nativeEvent.stopImmediatePropagation();
this.setState({isEditing: true});
}
2019-01-14 05:33:36 +00:00
onOptionChanged = (e) => {
2019-01-24 09:15:01 +00:00
let permission = e.value;
2019-01-09 06:39:17 +00:00
if (permission !== this.props.currentOption) {
2019-01-14 05:33:36 +00:00
this.props.onOptionChanged(permission);
2018-12-25 08:28:17 +00:00
}
this.setState({isEditing: false});
}
onSelectHandler = (e) => {
e.nativeEvent.stopImmediatePropagation();
}
onHideSelect = () => {
this.setState({isEditing: false});
}
render() {
2019-01-09 06:39:17 +00:00
let { currentOption, options, isTextMode } = this.props;
2018-12-25 08:28:17 +00:00
// scence1: isTextMode (text)editor-icon --> select
// scence2: !isTextMode select
return (
2019-01-24 09:15:01 +00:00
<div className="permission-editor" onClick={this.onSelectHandler}>
2018-12-25 08:28:17 +00:00
{(!isTextMode || this.state.isEditing) &&
2019-01-24 09:15:01 +00:00
<Select
options={this.options}
className="permission-editor-select"
classNamePrefix="select"
onChange={this.onOptionChanged}
menuPlacement="auto"
/>
2018-12-25 08:28:17 +00:00
}
{(isTextMode && !this.state.isEditing) &&
<div>
2019-01-09 06:39:17 +00:00
{this.props.translateOption(currentOption)}
2018-12-26 08:07:22 +00:00
{this.props.isEditIconShow && (
<span
title={gettext('Edit')}
2018-12-28 08:34:04 +00:00
className="fa fa-pencil attr-action-icon"
2019-01-24 09:15:01 +00:00
onClick={this.onEditPermission}>
2018-12-26 08:07:22 +00:00
</span>
)}
2018-12-25 08:28:17 +00:00
</div>
}
</div>
);
}
}
2019-01-09 06:39:17 +00:00
SelectEditor.propTypes = propTypes;
2018-12-25 08:28:17 +00:00
2019-01-09 06:39:17 +00:00
export default SelectEditor;