-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from algolia/feature/indexSelector
feat: indexSelector widget
- Loading branch information
Showing
7 changed files
with
152 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var React = require('react'); | ||
|
||
class IndexSelector extends React.Component { | ||
handleChange(event) { | ||
this.props.setIndex(event.target.value).search(); | ||
} | ||
|
||
render() { | ||
var currentIndex = this.props.currentIndex; | ||
var indices = this.props.indices; | ||
var cssClass = this.props.cssClass; | ||
var selectId = this.props.containerId + '-select'; | ||
|
||
return ( | ||
<select | ||
id={selectId} | ||
className={cssClass} | ||
onChange={this.handleChange.bind(this)} | ||
value={currentIndex} | ||
> | ||
{indices.map(function(index) { | ||
return <option key={index.name} value={index.name}>{index.label}</option>; | ||
})} | ||
</select> | ||
); | ||
} | ||
} | ||
|
||
IndexSelector.propTypes = { | ||
containerId: React.PropTypes.string, | ||
cssClass: React.PropTypes.string, | ||
currentIndex: React.PropTypes.string, | ||
indices: React.PropTypes.array, | ||
setIndex: React.PropTypes.func | ||
}; | ||
|
||
module.exports = IndexSelector; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,51 @@ | ||
var React = require('react'); | ||
|
||
var findIndex = require('lodash/array/findIndex'); | ||
var utils = require('../lib/widget-utils.js'); | ||
|
||
/** | ||
* Instantiate a dropdown element to choose the current targeted index | ||
* @param {String|DOMElement} options.container Valid CSS Selector as a string or DOMElement | ||
* @param {Array} options.indices Array of objects defining the different indices to choose from. Each object must contain a `name` and `label` key. | ||
* @param {String} [options.cssClass] Class name(s) to be added to the generated select element | ||
* @return {Object} | ||
*/ | ||
function indexSelector({ | ||
container = null, | ||
cssClass = {}, | ||
indices = null | ||
}) { | ||
var IndexSelector = require('../components/IndexSelector'); | ||
var containerNode = utils.getContainerNode(container); | ||
|
||
var usage = 'Usage: indexSelector({container, indices[, cssClass]})'; | ||
if (container === null || indices === null) { | ||
throw new Error(usage); | ||
} | ||
|
||
return { | ||
init: function(state, helper) { | ||
var currentIndex = helper.getIndex(); | ||
var isIndexInList = findIndex(indices, {'name': currentIndex}) !== -1; | ||
if (!isIndexInList) { | ||
throw new Error('[stats]: Index ' + currentIndex + ' not present in `indices`'); | ||
} | ||
}, | ||
|
||
render: function(results, state, helper) { | ||
var containerId = containerNode.id; | ||
React.render( | ||
<IndexSelector | ||
containerId={containerId} | ||
cssClass={cssClass} | ||
currentIndex={helper.getIndex()} | ||
indices={indices} | ||
setIndex={helper.setIndex.bind(helper)} | ||
/>, | ||
containerNode | ||
); | ||
} | ||
}; | ||
} | ||
|
||
module.exports = indexSelector; |