-
Notifications
You must be signed in to change notification settings - Fork 14.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[explore] adding y_axis_bounds to force Y axis bounds (#2878)
* [explore] adding y_axis_bounds to force Y axis * Handling comments
- Loading branch information
1 parent
c5f2eaf
commit e300273
Showing
9 changed files
with
209 additions
and
42 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
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
95 changes: 95 additions & 0 deletions
95
superset/assets/javascripts/explore/components/controls/BoundsControl.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,95 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Col, Row, FormGroup, FormControl } from 'react-bootstrap'; | ||
import ControlHeader from '../ControlHeader'; | ||
|
||
const propTypes = { | ||
name: PropTypes.string.isRequired, | ||
label: PropTypes.string, | ||
description: PropTypes.string, | ||
onChange: PropTypes.func, | ||
value: PropTypes.array, | ||
}; | ||
|
||
const defaultProps = { | ||
label: null, | ||
description: null, | ||
onChange: () => {}, | ||
value: [null, null], | ||
}; | ||
|
||
export default class BoundsControl extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
minMax: [ | ||
props.value[0] === null ? '' : props.value[0], | ||
props.value[1] === null ? '' : props.value[1], | ||
], | ||
}; | ||
this.onChange = this.onChange.bind(this); | ||
this.onMinChange = this.onMinChange.bind(this); | ||
this.onMaxChange = this.onMaxChange.bind(this); | ||
} | ||
onMinChange(event) { | ||
this.setState({ | ||
minMax: [ | ||
event.target.value, | ||
this.state.minMax[1], | ||
], | ||
}, this.onChange); | ||
} | ||
onMaxChange(event) { | ||
this.setState({ | ||
minMax: [ | ||
this.state.minMax[0], | ||
event.target.value, | ||
], | ||
}, this.onChange); | ||
} | ||
onChange() { | ||
const mm = this.state.minMax; | ||
const errors = []; | ||
if (mm[0] && isNaN(mm[0])) { | ||
errors.push('`Min` value should be numeric or empty'); | ||
} | ||
if (mm[1] && isNaN(mm[1])) { | ||
errors.push('`Max` value should be numeric or empty'); | ||
} | ||
if (errors.length === 0) { | ||
this.props.onChange([parseFloat(mm[0]), parseFloat(mm[1])], errors); | ||
} else { | ||
this.props.onChange([null, null], errors); | ||
} | ||
} | ||
render() { | ||
return ( | ||
<div> | ||
<ControlHeader {...this.props} /> | ||
<FormGroup bsSize="small"> | ||
<Row> | ||
<Col xs={6}> | ||
<FormControl | ||
type="text" | ||
placeholder="Min" | ||
onChange={this.onMinChange} | ||
value={this.state.minMax[0]} | ||
/> | ||
</Col> | ||
<Col xs={6}> | ||
<FormControl | ||
type="text" | ||
placeholder="Max" | ||
onChange={this.onMaxChange} | ||
value={this.state.minMax[1]} | ||
/> | ||
</Col> | ||
</Row> | ||
</FormGroup> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
BoundsControl.propTypes = propTypes; | ||
BoundsControl.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
38 changes: 38 additions & 0 deletions
38
superset/assets/spec/javascripts/explorev2/components/BoundsControl_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,38 @@ | ||
/* eslint-disable no-unused-expressions */ | ||
import React from 'react'; | ||
import { FormControl } from 'react-bootstrap'; | ||
import sinon from 'sinon'; | ||
import { expect } from 'chai'; | ||
import { describe, it, beforeEach } from 'mocha'; | ||
import { mount } from 'enzyme'; | ||
|
||
import BoundsControl from '../../../../javascripts/explore/components/controls/BoundsControl'; | ||
|
||
const defaultProps = { | ||
name: 'y_axis_bounds', | ||
label: 'Bounds of the y axis', | ||
onChange: sinon.spy(), | ||
}; | ||
|
||
describe('BoundsControl', () => { | ||
let wrapper; | ||
|
||
beforeEach(() => { | ||
wrapper = mount(<BoundsControl {...defaultProps} />); | ||
}); | ||
|
||
it('renders two FormControls', () => { | ||
expect(wrapper.find(FormControl)).to.have.lengthOf(2); | ||
}); | ||
|
||
it('errors on non-numeric', () => { | ||
wrapper.find(FormControl).first().simulate('change', { target: { value: 's' } }); | ||
expect(defaultProps.onChange.calledWith([null, null])).to.be.true; | ||
expect(defaultProps.onChange.getCall(0).args[1][0]).to.contain('value should be numeric'); | ||
}); | ||
it('casts to numeric', () => { | ||
wrapper.find(FormControl).first().simulate('change', { target: { value: '1' } }); | ||
wrapper.find(FormControl).last().simulate('change', { target: { value: '5' } }); | ||
expect(defaultProps.onChange.calledWith([1, 5])).to.be.true; | ||
}); | ||
}); |
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