-
Notifications
You must be signed in to change notification settings - Fork 14.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add separate limit setting for SqlLab #4941
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
superset/assets/spec/javascripts/sqllab/LimitControl_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,63 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
|
||
import { Label } from 'react-bootstrap'; | ||
import LimitControl from '../../../src/SqlLab/components/LimitControl'; | ||
import ControlHeader from '../../../src/explore/components/ControlHeader'; | ||
|
||
describe('LimitControl', () => { | ||
const defaultProps = { | ||
value: 0, | ||
defaultQueryLimit: 1000, | ||
maxRow: 100000, | ||
onChange: () => {}, | ||
}; | ||
let wrapper; | ||
const factory = o => <LimitControl {...o} />; | ||
beforeEach(() => { | ||
wrapper = shallow(factory(defaultProps)); | ||
}); | ||
it('is a valid element', () => { | ||
expect(React.isValidElement(<LimitControl {...defaultProps} />)).toEqual(true); | ||
}); | ||
it('renders a Label', () => { | ||
expect(wrapper.find(Label)).toHaveLength(1); | ||
}); | ||
it('loads the correct state', () => { | ||
const value = 100; | ||
wrapper = shallow(factory({ ...defaultProps, value })); | ||
expect(wrapper.state().textValue).toEqual(value.toString()); | ||
wrapper.find(Label).first().simulate('click'); | ||
expect(wrapper.state().showOverlay).toBe(true); | ||
expect(wrapper.find(ControlHeader).props().validationErrors).toHaveLength(0); | ||
}); | ||
it('handles invalid value', () => { | ||
wrapper.find(Label).first().simulate('click'); | ||
wrapper.setState({ textValue: 'invalid' }); | ||
expect(wrapper.find(ControlHeader).props().validationErrors).toHaveLength(1); | ||
}); | ||
it('handles negative value', () => { | ||
wrapper.find(Label).first().simulate('click'); | ||
wrapper.setState({ textValue: '-1' }); | ||
expect(wrapper.find(ControlHeader).props().validationErrors).toHaveLength(1); | ||
}); | ||
it('handles value above max row', () => { | ||
wrapper.find(Label).first().simulate('click'); | ||
wrapper.setState({ textValue: (defaultProps.maxRow + 1).toString() }); | ||
expect(wrapper.find(ControlHeader).props().validationErrors).toHaveLength(1); | ||
}); | ||
it('opens and closes', () => { | ||
wrapper.find(Label).first().simulate('click'); | ||
expect(wrapper.state().showOverlay).toBe(true); | ||
wrapper.find('.ok').first().simulate('click'); | ||
expect(wrapper.state().showOverlay).toBe(false); | ||
}); | ||
it('resets and closes', () => { | ||
const value = 100; | ||
wrapper = shallow(factory({ ...defaultProps, value })); | ||
wrapper.find(Label).first().simulate('click'); | ||
expect(wrapper.state().textValue).toEqual(value.toString()); | ||
wrapper.find('.reset').simulate('click'); | ||
expect(wrapper.state().textValue).toEqual(defaultProps.defaultQueryLimit.toString()); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { | ||
Button, | ||
Label, | ||
FormGroup, | ||
FormControl, | ||
Overlay, | ||
Popover, | ||
} from 'react-bootstrap'; | ||
import { t } from '@superset-ui/translation'; | ||
|
||
import ControlHeader from '../../explore/components/ControlHeader'; | ||
|
||
const propTypes = { | ||
value: PropTypes.number, | ||
defaultQueryLimit: PropTypes.number.isRequired, | ||
maxRow: PropTypes.number.isRequired, | ||
onChange: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default class LimitControl extends React.PureComponent { | ||
constructor(props) { | ||
super(props); | ||
const { value, defaultQueryLimit } = props; | ||
this.state = { | ||
textValue: value.toString() || defaultQueryLimit.toString(), | ||
showOverlay: false, | ||
}; | ||
this.handleHide = this.handleHide.bind(this); | ||
this.handleToggle = this.handleToggle.bind(this); | ||
this.submitAndClose = this.submitAndClose.bind(this); | ||
} | ||
|
||
setValueAndClose(val) { | ||
this.setState({ textValue: val }, this.submitAndClose); | ||
} | ||
|
||
submitAndClose() { | ||
const value = parseInt(this.state.textValue, 10) || this.props.defaultQueryLimit; | ||
this.props.onChange(value); | ||
this.setState({ showOverlay: false }); | ||
} | ||
|
||
isValidLimit(limit) { | ||
const value = parseInt(limit, 10); | ||
return !(Number.isNaN(value) || value <= 0 || (this.props.maxRow && value > this.props.maxRow)); | ||
} | ||
|
||
handleToggle() { | ||
this.setState({ showOverlay: !this.state.showOverlay }); | ||
} | ||
|
||
handleHide() { | ||
this.setState({ showOverlay: false }); | ||
} | ||
|
||
renderPopover() { | ||
const textValue = this.state.textValue; | ||
const isValid = this.isValidLimit(textValue); | ||
const errorMsg = 'Row limit must be positive integer' + | ||
(this.props.maxRow ? ` and not greater than ${this.props.maxRow}` : ''); | ||
return ( | ||
<Popover id="sqllab-limit-results"> | ||
<div style={{ width: '100px' }}> | ||
<ControlHeader | ||
label={t('Row limit')} | ||
validationErrors={!isValid ? [t(errorMsg)] : []} | ||
/> | ||
<FormGroup> | ||
<FormControl | ||
type="text" | ||
value={textValue} | ||
placeholder={t(`Max: ${this.props.maxRow}`)} | ||
bsSize="small" | ||
onChange={e => this.setState({ textValue: e.target.value })} | ||
/> | ||
</FormGroup> | ||
<div className="clearfix"> | ||
<Button | ||
bsSize="small" | ||
bsStyle="primary" | ||
className="float-left ok" | ||
disabled={!isValid} | ||
onClick={this.submitAndClose} | ||
> | ||
Ok | ||
</Button> | ||
<Button | ||
bsSize="small" | ||
className="float-right reset" | ||
onClick={this.setValueAndClose.bind(this, this.props.defaultQueryLimit.toString())} | ||
> | ||
Reset | ||
</Button> | ||
</div> | ||
</div> | ||
</Popover> | ||
); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<Label | ||
style={{ cursor: 'pointer' }} | ||
onClick={this.handleToggle} | ||
> | ||
LIMIT {this.props.value || this.props.maxRow} | ||
</Label> | ||
<Overlay | ||
rootClose | ||
show={this.state.showOverlay} | ||
onHide={this.handleHide} | ||
trigger="click" | ||
placement="right" | ||
target={this} | ||
> | ||
{this.renderPopover()} | ||
</Overlay> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
LimitControl.propTypes = propTypes; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the
id
necessary?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is primarily for getting rid of the warning. Do we want to remove it?