Files
superset2/superset/assets/javascripts/components/EditableTitle.jsx
Grace Guo 3949d39478 Allow user update slice title in visualize flow (#3466)
* 1. after user make sql query and visualize, allow user click title to update slice title, and create a new slice at the same time.
2. don't save new title if it is empty. Will still show old title.

* change saveSlice call response and update explore view
2017-09-25 11:38:29 -07:00

113 lines
2.5 KiB
JavaScript

import React from 'react';
import PropTypes from 'prop-types';
import TooltipWrapper from './TooltipWrapper';
import { t } from '../locales';
const propTypes = {
title: PropTypes.string,
canEdit: PropTypes.bool,
onSaveTitle: PropTypes.func,
noPermitTooltip: PropTypes.string,
};
const defaultProps = {
title: t('Title'),
canEdit: false,
};
class EditableTitle extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
isEditing: false,
title: this.props.title,
lastTitle: this.props.title,
};
this.handleClick = this.handleClick.bind(this);
this.handleBlur = this.handleBlur.bind(this);
this.handleChange = this.handleChange.bind(this);
this.handleKeyPress = this.handleKeyPress.bind(this);
}
componentWillReceiveProps(nextProps) {
if (nextProps.title !== this.state.title) {
this.setState({
lastTitle: this.state.title,
title: nextProps.title,
});
}
}
handleClick() {
if (!this.props.canEdit) {
return;
}
this.setState({
isEditing: true,
});
}
handleBlur() {
if (!this.props.canEdit) {
return;
}
this.setState({
isEditing: false,
});
if (!this.state.title.length) {
this.setState({
title: this.state.lastTitle,
});
return;
}
if (this.state.lastTitle !== this.state.title) {
this.setState({
lastTitle: this.state.title,
});
this.props.onSaveTitle(this.state.title);
}
}
handleChange(ev) {
if (!this.props.canEdit) {
return;
}
this.setState({
title: ev.target.value,
});
}
handleKeyPress(ev) {
if (ev.key === 'Enter') {
ev.preventDefault();
this.handleBlur();
}
}
render() {
return (
<span className="editable-title">
<TooltipWrapper
label="title"
tooltip={this.props.canEdit ? t('click to edit title') :
this.props.noPermitTooltip || t('You don\'t have the rights to alter this title.')}
>
<input
required
type={this.state.isEditing ? 'text' : 'button'}
value={this.state.title}
onChange={this.handleChange}
onBlur={this.handleBlur}
onClick={this.handleClick}
onKeyPress={this.handleKeyPress}
/>
</TooltipWrapper>
</span>
);
}
}
EditableTitle.propTypes = propTypes;
EditableTitle.defaultProps = defaultProps;
export default EditableTitle;