-
Notifications
You must be signed in to change notification settings - Fork 14k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[explore] viz type selector as modal (#2787)
* [explore] viz type selector as modal * Addressing comments * Adressing comments
- Loading branch information
1 parent
3a4cd3a
commit 66403f1
Showing
8 changed files
with
184 additions
and
12 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
108 changes: 108 additions & 0 deletions
108
superset/assets/javascripts/explore/components/controls/VizTypeControl.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,108 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Label, Row, Col, FormControl, Modal } from 'react-bootstrap'; | ||
import visTypes from '../../stores/visTypes'; | ||
import ControlHeader from '../ControlHeader'; | ||
|
||
const propTypes = { | ||
description: PropTypes.string, | ||
label: PropTypes.string, | ||
name: PropTypes.string.isRequired, | ||
onChange: PropTypes.func, | ||
value: PropTypes.string.isRequired, | ||
}; | ||
|
||
const defaultProps = { | ||
onChange: () => {}, | ||
}; | ||
|
||
export default class VizTypeControl extends React.PureComponent { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
showModal: false, | ||
filter: '', | ||
}; | ||
this.toggleModal = this.toggleModal.bind(this); | ||
this.changeSearch = this.changeSearch.bind(this); | ||
} | ||
onChange(vizType) { | ||
this.props.onChange(vizType); | ||
this.setState({ showModal: false }); | ||
} | ||
toggleModal() { | ||
this.setState({ showModal: !this.state.showModal }); | ||
} | ||
changeSearch(event) { | ||
this.setState({ filter: event.target.value }); | ||
} | ||
renderVizType(vizType) { | ||
const vt = vizType; | ||
return ( | ||
<div | ||
className={`viztype-selector-container ${vt === this.props.value ? 'selected' : ''}`} | ||
onClick={this.onChange.bind(this, vt)} | ||
> | ||
<img | ||
alt={`viz-type-${vt}`} | ||
width="100%" | ||
className={`viztype-selector ${this.props.value === vt ? 'selected' : ''}`} | ||
src={`/static/assets/images/viz_thumbnails/${vt}.png`} | ||
/> | ||
<div className="viztype-label"> | ||
<strong>{visTypes[vt].label}</strong> | ||
</div> | ||
</div>); | ||
} | ||
render() { | ||
const filter = this.state.filter; | ||
const filteredVizTypes = Object.keys(visTypes) | ||
.filter(vt => filter.length === 0 || visTypes[vt].label.toLowerCase().includes(filter)); | ||
|
||
const imgPerRow = 4; | ||
const rows = []; | ||
for (let i = 0; i <= filteredVizTypes.length; i += imgPerRow) { | ||
rows.push( | ||
<Row> | ||
{filteredVizTypes.slice(i, i + imgPerRow).map(vt => ( | ||
<Col md={3} key={`grid-col-${vt}`}> | ||
{this.renderVizType(vt)} | ||
</Col> | ||
))} | ||
</Row>); | ||
} | ||
return ( | ||
<div> | ||
<ControlHeader | ||
{...this.props} | ||
rightNode={ | ||
<a onClick={this.toggleModal}>edit</a> | ||
} | ||
/> | ||
<Label onClick={this.toggleModal} style={{ cursor: 'pointer' }}> | ||
{visTypes[this.props.value].label} | ||
</Label> | ||
<Modal show={this.state.showModal} onHide={this.toggleModal} bsSize="lg"> | ||
<Modal.Header closeButton> | ||
<Modal.Title>Select a visualization type</Modal.Title> | ||
</Modal.Header> | ||
<Modal.Body> | ||
<div> | ||
<FormControl | ||
id="formControlsText" | ||
type="text" | ||
bsSize="sm" | ||
value={this.state.filter} | ||
placeholder="Search / Filter" | ||
onChange={this.changeSearch} | ||
/> | ||
</div> | ||
{rows} | ||
</Modal.Body> | ||
</Modal> | ||
</div>); | ||
} | ||
} | ||
|
||
VizTypeControl.propTypes = propTypes; | ||
VizTypeControl.defaultProps = defaultProps; |
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
37 changes: 37 additions & 0 deletions
37
superset/assets/spec/javascripts/explorev2/components/VizTypeControl_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,37 @@ | ||
import React from 'react'; | ||
import sinon from 'sinon'; | ||
import { expect } from 'chai'; | ||
import { describe, it, beforeEach } from 'mocha'; | ||
import { shallow } from 'enzyme'; | ||
import { Modal } from 'react-bootstrap'; | ||
import VizTypeControl from '../../../../javascripts/explore/components/controls/VizTypeControl'; | ||
|
||
const defaultProps = { | ||
name: 'viz_type', | ||
label: 'Visualization Type', | ||
value: 'table', | ||
onChange: sinon.spy(), | ||
}; | ||
|
||
describe('VizTypeControl', () => { | ||
let wrapper; | ||
|
||
beforeEach(() => { | ||
wrapper = shallow(<VizTypeControl {...defaultProps} />); | ||
}); | ||
|
||
it('renders a Modal', () => { | ||
expect(wrapper.find(Modal)).to.have.lengthOf(1); | ||
}); | ||
|
||
it('calls onChange when toggled', () => { | ||
const select = wrapper.find('.viztype-selector-container').first(); | ||
select.simulate('click'); | ||
expect(defaultProps.onChange.called).to.equal(true); | ||
}); | ||
it('filters images based on text input', () => { | ||
expect(wrapper.find('img').length).to.be.above(20); | ||
wrapper.setState({ filter: 'time' }); | ||
expect(wrapper.find('img').length).to.be.below(10); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -422,3 +422,8 @@ hr { | |
} | ||
} | ||
.space-loop(6); | ||
|
||
a { | ||
cursor: pointer; | ||
} | ||
|