mirror of
https://github.com/ascribe/onion.git
synced 2025-01-03 18:35:09 +01:00
add threshold functionality to SearchBar
This commit is contained in:
parent
02eafaa80c
commit
c59f5267ce
@ -22,10 +22,10 @@ class PieceListActions {
|
|||||||
this.actions.updatePieceList({
|
this.actions.updatePieceList({
|
||||||
page,
|
page,
|
||||||
pageSize,
|
pageSize,
|
||||||
search,
|
|
||||||
orderBy,
|
orderBy,
|
||||||
orderAsc,
|
orderAsc,
|
||||||
filterBy,
|
filterBy,
|
||||||
|
search: '',
|
||||||
pieceList: [],
|
pieceList: [],
|
||||||
pieceListCount: -1,
|
pieceListCount: -1,
|
||||||
unfilteredPieceListCount: -1
|
unfilteredPieceListCount: -1
|
||||||
|
@ -11,6 +11,7 @@ let PieceListToolbar = React.createClass({
|
|||||||
propTypes: {
|
propTypes: {
|
||||||
className: React.PropTypes.string,
|
className: React.PropTypes.string,
|
||||||
searchFor: React.PropTypes.func,
|
searchFor: React.PropTypes.func,
|
||||||
|
searchQuery: React.PropTypes.string,
|
||||||
filterParams: React.PropTypes.arrayOf(
|
filterParams: React.PropTypes.arrayOf(
|
||||||
React.PropTypes.shape({
|
React.PropTypes.shape({
|
||||||
label: React.PropTypes.string,
|
label: React.PropTypes.string,
|
||||||
@ -61,7 +62,7 @@ let PieceListToolbar = React.createClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { className, children, searchFor } = this.props;
|
const { className, children, searchFor, searchQuery } = this.props;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={className}>
|
<div className={className}>
|
||||||
@ -74,7 +75,8 @@ let PieceListToolbar = React.createClass({
|
|||||||
<SearchBar
|
<SearchBar
|
||||||
className="pull-right search-bar ascribe-input-glyph"
|
className="pull-right search-bar ascribe-input-glyph"
|
||||||
searchFor={searchFor}
|
searchFor={searchFor}
|
||||||
threshold={1000}/>
|
searchQuery={searchQuery}
|
||||||
|
threshold={500}/>
|
||||||
<span className="pull-right">
|
<span className="pull-right">
|
||||||
{this.getOrderWidget()}
|
{this.getOrderWidget()}
|
||||||
{this.getFilterWidget()}
|
{this.getFilterWidget()}
|
||||||
|
@ -157,6 +157,7 @@ let PieceList = React.createClass({
|
|||||||
<PieceListToolbar
|
<PieceListToolbar
|
||||||
className="ascribe-piece-list-toolbar"
|
className="ascribe-piece-list-toolbar"
|
||||||
searchFor={this.searchFor}
|
searchFor={this.searchFor}
|
||||||
|
searchQuery={this.state.search}
|
||||||
filterParams={this.props.filterParams}
|
filterParams={this.props.filterParams}
|
||||||
orderParams={this.props.orderParams}
|
orderParams={this.props.orderParams}
|
||||||
filterBy={this.state.filterBy}
|
filterBy={this.state.filterBy}
|
||||||
|
@ -13,6 +13,8 @@ const { update } = React.addons;
|
|||||||
const SearchBar = React.createClass({
|
const SearchBar = React.createClass({
|
||||||
propTypes: {
|
propTypes: {
|
||||||
searchFor: func.isRequired,
|
searchFor: func.isRequired,
|
||||||
|
searchQuery: string.isRequired,
|
||||||
|
|
||||||
className: string,
|
className: string,
|
||||||
|
|
||||||
// in milliseconds
|
// in milliseconds
|
||||||
@ -22,35 +24,61 @@ const SearchBar = React.createClass({
|
|||||||
getInitialState() {
|
getInitialState() {
|
||||||
return {
|
return {
|
||||||
timers: [],
|
timers: [],
|
||||||
currentSearchQuery: ''
|
searchQuery: '',
|
||||||
|
searching: false
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
componentDidUpdate(prevProps) {
|
||||||
|
const searchQueryProps = this.props.searchQuery;
|
||||||
|
const searchQueryPrevProps = prevProps.searchQuery;
|
||||||
|
const searchQueryState = this.state.searchQuery;
|
||||||
|
const { searching } = this.state;
|
||||||
|
|
||||||
|
if(searching && (searchQueryProps && searchQueryState && searchQueryProps === searchQueryState || !searchQueryPrevProps && searchQueryProps === searchQueryState)) {
|
||||||
|
this.setState({ searching: false });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
startTimers(searchQuery) {
|
startTimers(searchQuery) {
|
||||||
const { timers } = this.state;
|
const { timers } = this.state;
|
||||||
const { threshold } = this.props;
|
const { threshold } = this.props;
|
||||||
const timer = {
|
const timer = {
|
||||||
searchQuery,
|
searchQuery,
|
||||||
cb: setTimeout(this.evaluateTimer, threshold)
|
cb: setTimeout(this.evaluateTimer(timers.length, searchQuery), threshold)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const newTimers = update(timers, {$push: [timer]});
|
const newTimers = update(timers, {$push: [timer]});
|
||||||
this.setState({ timers: newTimers });
|
this.setState({ timers: newTimers });
|
||||||
},
|
},
|
||||||
|
|
||||||
evaluateTimer() {
|
evaluateTimer(timerIndex, searchQuery) {
|
||||||
console.log(this);
|
return () => {
|
||||||
|
const { timers } = this.state;
|
||||||
|
|
||||||
|
if(timers.length <= timerIndex + 1) {
|
||||||
|
// clean the timers array
|
||||||
|
this.setState({ timers: [], searching: true }, () => {
|
||||||
|
// search for the query
|
||||||
|
this.props.searchFor(searchQuery);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
handleChange({ target: { value }}) {
|
handleChange({ target: { value }}) {
|
||||||
this.startTimers(value);
|
this.startTimers(value);
|
||||||
this.setState({ currentSearchQuery: value });
|
this.setState({ searchQuery: value });
|
||||||
},
|
},
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const searchIcon = <Glyphicon glyph='search' className="filter-glyph"/>;
|
let searchIcon = <Glyphicon glyph='search' className="filter-glyph"/>;
|
||||||
const { className } = this.props;
|
const { className } = this.props;
|
||||||
|
const { searching } = this.state;
|
||||||
|
|
||||||
|
if(searching) {
|
||||||
|
searchIcon = <span className="glyph-ascribe-spool-chunked ascribe-color spin"/>;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<span className={className}>
|
<span className={className}>
|
||||||
|
Loading…
Reference in New Issue
Block a user