-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Edit Dashboard title and Slice title in place (#2940)
* Edit Dashboard title and Slice title in place Add EditableTitle component into Dashboard and Explore view to support edit title inline.
- Loading branch information
Grace Guo
authored
Jun 14, 2017
1 parent
da0a87a
commit 8329ea2
Showing
13 changed files
with
302 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import TooltipWrapper from './TooltipWrapper'; | ||
|
||
const propTypes = { | ||
title: PropTypes.string, | ||
canEdit: PropTypes.bool, | ||
onSaveTitle: PropTypes.func.isRequired, | ||
}; | ||
const defaultProps = { | ||
title: '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); | ||
} | ||
handleClick() { | ||
if (!this.props.canEdit) { | ||
return; | ||
} | ||
|
||
this.setState({ | ||
isEditing: true, | ||
}); | ||
} | ||
handleBlur() { | ||
if (!this.props.canEdit) { | ||
return; | ||
} | ||
|
||
this.setState({ | ||
isEditing: false, | ||
}); | ||
|
||
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, | ||
}); | ||
} | ||
render() { | ||
return ( | ||
<span className="editable-title"> | ||
<TooltipWrapper | ||
label="title" | ||
tooltip={this.props.canEdit ? 'click to edit title' : '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} | ||
/> | ||
</TooltipWrapper> | ||
</span> | ||
); | ||
} | ||
} | ||
EditableTitle.propTypes = propTypes; | ||
EditableTitle.defaultProps = defaultProps; | ||
|
||
export default EditableTitle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
superset/assets/spec/javascripts/profile/EditableTitle_spec.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import { describe, it } from 'mocha'; | ||
import sinon from 'sinon'; | ||
import { expect } from 'chai'; | ||
|
||
import EditableTable from '../../../javascripts/components/EditableTitle'; | ||
|
||
describe('EditableTitle', () => { | ||
const callback = sinon.spy(); | ||
const mockProps = { | ||
title: 'my title', | ||
canEdit: true, | ||
onSaveTitle: callback, | ||
}; | ||
const mockEvent = { | ||
target: { | ||
value: 'new title', | ||
}, | ||
}; | ||
const editableWrapper = shallow(<EditableTable {...mockProps} />); | ||
const notEditableWrapper = shallow(<EditableTable title="my title" />); | ||
it('is valid', () => { | ||
expect( | ||
React.isValidElement(<EditableTable {...mockProps} />), | ||
).to.equal(true); | ||
}); | ||
it('should render title', () => { | ||
const titleElement = editableWrapper.find('input'); | ||
expect(titleElement.props().value).to.equal('my title'); | ||
expect(titleElement.props().type).to.equal('button'); | ||
}); | ||
|
||
describe('should handle click', () => { | ||
it('should change title', () => { | ||
editableWrapper.find('input').simulate('click'); | ||
expect(editableWrapper.find('input').props().type).to.equal('text'); | ||
}); | ||
it('should not change title', () => { | ||
notEditableWrapper.find('input').simulate('click'); | ||
expect(notEditableWrapper.find('input').props().type).to.equal('button'); | ||
}); | ||
}); | ||
|
||
describe('should handle change', () => { | ||
afterEach(() => { | ||
editableWrapper.setState({ title: 'my title' }); | ||
editableWrapper.setState({ lastTitle: 'my title' }); | ||
}); | ||
it('should change title', () => { | ||
editableWrapper.find('input').simulate('change', mockEvent); | ||
expect(editableWrapper.find('input').props().value).to.equal('new title'); | ||
}); | ||
it('should not change title', () => { | ||
notEditableWrapper.find('input').simulate('change', mockEvent); | ||
expect(editableWrapper.find('input').props().value).to.equal('my title'); | ||
}); | ||
}); | ||
|
||
describe('should handle blur', () => { | ||
beforeEach(() => { | ||
editableWrapper.find('input').simulate('click'); | ||
expect(editableWrapper.find('input').props().type).to.equal('text'); | ||
}); | ||
afterEach(() => { | ||
callback.reset(); | ||
editableWrapper.setState({ title: 'my title' }); | ||
editableWrapper.setState({ lastTitle: 'my title' }); | ||
}); | ||
|
||
it('should trigger callback', () => { | ||
editableWrapper.setState({ title: 'new title' }); | ||
editableWrapper.find('input').simulate('blur'); | ||
expect(editableWrapper.find('input').props().type).to.equal('button'); | ||
expect(callback.callCount).to.equal(1); | ||
expect(callback.getCall(0).args[0]).to.equal('new title'); | ||
}); | ||
it('should not trigger callback', () => { | ||
editableWrapper.find('input').simulate('blur'); | ||
expect(editableWrapper.find('input').props().type).to.equal('button'); | ||
// no change | ||
expect(callback.callCount).to.equal(0); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.