-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pagination): add async pagination
- Loading branch information
1 parent
b3db918
commit f34d3e4
Showing
6 changed files
with
233 additions
and
14 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
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,93 @@ | ||
import React from 'react'; | ||
import Select from 'react-select'; | ||
import fetch from 'isomorphic-fetch'; | ||
|
||
|
||
const GithubUsersPagination = React.createClass({ | ||
displayName: 'GithubUsersPagination', | ||
propTypes: { | ||
label: React.PropTypes.string, | ||
}, | ||
getInitialState () { | ||
return { | ||
backspaceRemoves: true, | ||
multi: true | ||
}; | ||
}, | ||
onChange (value) { | ||
this.setState({ | ||
value: value, | ||
}); | ||
}, | ||
switchToMulti () { | ||
this.setState({ | ||
multi: true, | ||
value: [this.state.value], | ||
}); | ||
}, | ||
switchToSingle () { | ||
this.setState({ | ||
multi: false, | ||
value: this.state.value ? this.state.value[0] : null | ||
}); | ||
}, | ||
getUsers (input, page) { | ||
if (!input) { | ||
return Promise.resolve({ options: [] }); | ||
} | ||
|
||
return fetch(`https://api.github.com/search/users?q=${input}&page=${page}`) | ||
.then((response) => response.json()) | ||
.then((json) => { | ||
return { options: json.items }; | ||
}); | ||
}, | ||
gotoUser (value, event) { | ||
window.open(value.html_url); | ||
}, | ||
toggleBackspaceRemoves () { | ||
this.setState({ | ||
backspaceRemoves: !this.state.backspaceRemoves | ||
}); | ||
}, | ||
toggleCreatable () { | ||
this.setState({ | ||
creatable: !this.state.creatable | ||
}); | ||
}, | ||
render () { | ||
const AsyncComponent = this.state.creatable | ||
? Select.AsyncCreatable | ||
: Select.Async; | ||
|
||
return ( | ||
<div className="section"> | ||
<h3 className="section-heading">{this.props.label}</h3> | ||
<AsyncComponent multi={this.state.multi} value={this.state.value} onChange={this.onChange} onValueClick={this.gotoUser} valueKey="id" labelKey="login" loadOptions={this.getUsers} pagination backspaceRemoves={this.state.backspaceRemoves} /> | ||
<div className="checkbox-list"> | ||
<label className="checkbox"> | ||
<input type="radio" className="checkbox-control" checked={this.state.multi} onChange={this.switchToMulti}/> | ||
<span className="checkbox-label">Multiselect</span> | ||
</label> | ||
<label className="checkbox"> | ||
<input type="radio" className="checkbox-control" checked={!this.state.multi} onChange={this.switchToSingle}/> | ||
<span className="checkbox-label">Single Value</span> | ||
</label> | ||
</div> | ||
<div className="checkbox-list"> | ||
<label className="checkbox"> | ||
<input type="checkbox" className="checkbox-control" checked={this.state.creatable} onChange={this.toggleCreatable} /> | ||
<span className="checkbox-label">Creatable?</span> | ||
</label> | ||
<label className="checkbox"> | ||
<input type="checkbox" className="checkbox-control" checked={this.state.backspaceRemoves} onChange={this.toggleBackspaceRemoves} /> | ||
<span className="checkbox-label">Backspace Removes?</span> | ||
</label> | ||
</div> | ||
<div className="hint">This example uses fetch.js for showing Async options with Promises</div> | ||
</div> | ||
); | ||
} | ||
}); | ||
|
||
module.exports = GithubUsersPagination; |
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