UNPKG

solrkit

Version:

![Build Status](https://travis-ci.org/garysieling/solrkit.svg?branch=master) ![Dependencies](https://david-dm.org/garysieling/solrkit/status.svg)

89 lines 3.76 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var React = require("react"); var react_1 = require("react"); var semantic_ui_react_1 = require("semantic-ui-react"); var lodash_1 = require("lodash"); var SearchBox = (function (_super) { __extends(SearchBox, _super); function SearchBox() { var _this = _super.call(this) || this; _this.state = { query: '', shouldBeOpen: false, searchEditedByUser: false }; _this.onKeyUp = _this.onKeyUp.bind(_this); _this.onBlur = _this.onBlur.bind(_this); _this.onChangeQuery = _this.onChangeQuery.bind(_this); _this.onSelectTypeahead = _this.onSelectTypeahead.bind(_this); return _this; } SearchBox.prototype.onKeyUp = function (event) { event.preventDefault(); if (event.keyCode === 13) { this.onDoSearch(this.state.query); this.setState({ shouldBeOpen: false }); } else if (event.keyCode === 27) { this.setState({ shouldBeOpen: false }); } }; SearchBox.prototype.onChangeQuery = function (e, data) { this.setState({ query: data.value + '', shouldBeOpen: (data.value || '').length > 0, searchEditedByUser: true }); }; SearchBox.prototype.onSelectTypeahead = function (e, data) { this.setState({ query: data.result.title, shouldBeOpen: false, searchEditedByUser: true }); this.onDoSearch(data.value + ''); }; SearchBox.prototype.onDoSearch = function (value) { var query = { query: value, start: 0 }; if (this.props.transition) { this.props.transition(query); } else { this.context.transition(query); } }; SearchBox.prototype.onBlur = function () { this.setState({ shouldBeOpen: false }); }; SearchBox.prototype.render = function () { var self = this; var query = (self.state.searchEditedByUser ? self.state.query : self.context.searchState ? (self.context.searchState.query) : '') || ''; var lc = query.toLowerCase(); var filteredSearches = lodash_1.take((self.props.sampleSearches || []).filter(function (search) { return search.indexOf(lc) === 0; }), 5).map(function (title) { return { title: title }; }); return (React.createElement(semantic_ui_react_1.Search, { className: "full", id: "searchBox", open: this.state.shouldBeOpen, loading: this.props.loading, onResultSelect: self.onSelectTypeahead, onSearchChange: self.onChangeQuery, results: filteredSearches, input: { fluid: true }, value: query, showNoResults: false, fluid: true, onKeyUp: self.onKeyUp, onBlur: self.onBlur, placeholder: this.props.placeholder, size: "large" })); }; SearchBox.contextTypes = { searchState: react_1.PropTypes.object, transition: react_1.PropTypes.func }; return SearchBox; }(React.Component)); exports.SearchBox = SearchBox; //# sourceMappingURL=SearchBox.js.map