search-box.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. "use strict";
  6. define(function (require, exports, module) {
  7. const { DOM: dom, createClass, PropTypes } = require("devtools/client/shared/vendor/react");
  8. const { input } = dom;
  9. // For smooth incremental searching (in case the user is typing quickly).
  10. const searchDelay = 250;
  11. /**
  12. * This object represents a search box located at the
  13. * top right corner of the application.
  14. */
  15. let SearchBox = createClass({
  16. displayName: "SearchBox",
  17. propTypes: {
  18. actions: PropTypes.object,
  19. },
  20. onSearch: function (event) {
  21. let searchBox = event.target;
  22. let win = searchBox.ownerDocument.defaultView;
  23. if (this.searchTimeout) {
  24. win.clearTimeout(this.searchTimeout);
  25. }
  26. let callback = this.doSearch.bind(this, searchBox);
  27. this.searchTimeout = win.setTimeout(callback, searchDelay);
  28. },
  29. doSearch: function (searchBox) {
  30. this.props.actions.onSearch(searchBox.value);
  31. },
  32. render: function () {
  33. return (
  34. input({className: "searchBox",
  35. placeholder: Locale.$STR("jsonViewer.filterJSON"),
  36. onChange: this.onSearch})
  37. );
  38. },
  39. });
  40. // Exports from this module
  41. exports.SearchBox = SearchBox;
  42. });