-
Notifications
You must be signed in to change notification settings - Fork 14.4k
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 a Async Select that fetches options from given endpoint #1909
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const $ = window.$ = require('jquery'); | ||
import React from 'react'; | ||
import Select from 'react-select'; | ||
|
||
const propTypes = { | ||
dataEndpoint: React.PropTypes.string.isRequired, | ||
onChange: React.PropTypes.func.isRequired, | ||
mutator: React.PropTypes.func.isRequired, | ||
value: React.PropTypes.number, | ||
valueRenderer: React.PropTypes.func, | ||
placeholder: React.PropTypes.string, | ||
}; | ||
|
||
const defaultProps = { | ||
placeholder: 'Select ...', | ||
valueRenderer: (o) => (<div>{o.label}</div>), | ||
}; | ||
|
||
class AsyncSelect extends React.PureComponent { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
isLoading: false, | ||
options: [], | ||
}; | ||
} | ||
componentDidMount() { | ||
this.fetchOptions(); | ||
} | ||
fetchOptions() { | ||
this.setState({ isLoading: true }); | ||
const mutator = this.props.mutator; | ||
$.get(this.props.dataEndpoint, (data) => { | ||
this.setState({ options: mutator ? mutator(data) : data, isLoading: false }); | ||
}); | ||
} | ||
onChange(opt) { | ||
this.props.onChange(opt); | ||
} | ||
render() { | ||
return ( | ||
<div> | ||
<Select | ||
placeholder={this.props.placeholder} | ||
options={this.state.options} | ||
value={this.props.value} | ||
isLoading={this.state.isLoading} | ||
onChange={this.onChange.bind(this)} | ||
valueRenderer={this.props.valueRenderer} | ||
/> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
AsyncSelect.propTypes = propTypes; | ||
AsyncSelect.defaultProps = defaultProps; | ||
|
||
export default AsyncSelect; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,35 @@ | ||
import React from 'react'; | ||
import Select from 'react-select'; | ||
import DatabaseSelect from '../../../javascripts/SqlLab/components/DatabaseSelect'; | ||
import AsyncSelect from '../../../javascripts/components/AsyncSelect'; | ||
import { shallow } from 'enzyme'; | ||
import { describe, it } from 'mocha'; | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
|
||
describe('DatabaseSelect', () => { | ||
describe('AsyncSelect', () => { | ||
const mockedProps = { | ||
actions: {}, | ||
dataEndpoint: '/slicemodelview/api/read', | ||
onChange: sinon.spy(), | ||
mutator: () => {}, | ||
}; | ||
it('is valid element', () => { | ||
expect( | ||
React.isValidElement(<DatabaseSelect {...mockedProps} />) | ||
React.isValidElement(<AsyncSelect {...mockedProps} />) | ||
).to.equal(true); | ||
}); | ||
|
||
it('has one select', () => { | ||
const wrapper = shallow( | ||
<DatabaseSelect {...mockedProps} /> | ||
<AsyncSelect {...mockedProps} /> | ||
); | ||
expect(wrapper.find(Select)).to.have.length(1); | ||
}); | ||
|
||
it('calls onChange on select change', () => { | ||
const onChange = sinon.spy(); | ||
const wrapper = shallow( | ||
<DatabaseSelect onChange={onChange} /> | ||
<AsyncSelect {...mockedProps} /> | ||
); | ||
wrapper.find(Select).simulate('change', { value: 1 }); | ||
expect(onChange).to.have.property('callCount', 1); | ||
expect(mockedProps.onChange).to.have.property('callCount', 1); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,14 +18,8 @@ describe('QuerySearch', () => { | |
}); | ||
const wrapper = shallow(<QuerySearch {...mockedProps} />); | ||
|
||
it('should have four Select', () => { | ||
expect(wrapper.find(Select)).to.have.length(4); | ||
}); | ||
|
||
it('updates userId on user selects change', () => { | ||
wrapper.find('[name="select-user"]') | ||
.simulate('change', { value: 1 }); | ||
expect(wrapper.state().userId).to.equal(1); | ||
it('should have three Select', () => { | ||
expect(wrapper.find(Select)).to.have.length(3); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this assert 3 or 4 selects? it was 4 before this change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. after we switched the user select to AsyncSelect it became 3 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok cool. we should update the |
||
}); | ||
|
||
it('updates fromTime on user selects from time', () => { | ||
|
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.
what do we show when the select is loading data?
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.
isLoading is true when loading the data, Select will render a spinner