MyAutocomplete.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _react = _interopRequireWildcard(require("react"));
  7. var _reactMaterialize = require("react-materialize");
  8. require("./MyAutocomplete.css");
  9. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  10. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  11. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  12. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  13. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  14. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  15. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  16. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  17. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
  18. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  19. var MyAutocomplete =
  20. /*#__PURE__*/
  21. function (_React$Component) {
  22. _inherits(MyAutocomplete, _React$Component);
  23. function MyAutocomplete(props) {
  24. var _this;
  25. _classCallCheck(this, MyAutocomplete);
  26. _this = _possibleConstructorReturn(this, _getPrototypeOf(MyAutocomplete).call(this, props));
  27. _this.state = {
  28. autoComplete: false,
  29. value: props.value || '',
  30. selected: -1
  31. };
  32. return _this;
  33. }
  34. _createClass(MyAutocomplete, [{
  35. key: "componentWillReceiveProps",
  36. value: function componentWillReceiveProps(props) {
  37. this.search(this.state.value, props);
  38. }
  39. }, {
  40. key: "select",
  41. value: function select(item) {
  42. var onComplete = this.props.onComplete;
  43. onComplete && onComplete(this.state.value, item);
  44. }
  45. }, {
  46. key: "search",
  47. value: function search(term, props) {
  48. if (term.length < 2) {
  49. this.setState({
  50. autoComplete: false
  51. });
  52. return;
  53. }
  54. var data = props.data,
  55. options = props.options;
  56. var values = Object.keys(data);
  57. values.sort(function (a, b) {
  58. return a.localeCompare(b);
  59. });
  60. values = values.map(function (item) {
  61. return item.toUpperCase();
  62. });
  63. var segments = term.toUpperCase().split(' ');
  64. var dataset = Object.keys(data).reduce(function (res, a) {
  65. res[a.toUpperCase()] = data[a];
  66. return res;
  67. }, {});
  68. var correct = values.filter(function (item) {
  69. return segments.reduce(function (res, seg) {
  70. return res && item.includes(seg);
  71. }, true);
  72. });
  73. correct = correct.slice(0, this.props.options.limit);
  74. this.setState({
  75. autoComplete: correct.length > 0,
  76. autoCompleteData: correct,
  77. data: dataset
  78. });
  79. }
  80. }, {
  81. key: "change",
  82. value: function change(e) {
  83. var term = e.target.value;
  84. var onChange = this.props.onChange;
  85. this.search(term, this.props);
  86. onChange && onChange(e);
  87. this.setState({
  88. value: term
  89. });
  90. }
  91. }, {
  92. key: "keyHandler",
  93. value: function keyHandler(e) {
  94. console.log(e.keyCode);
  95. if (e.keyCode === 40) {
  96. this.setState({
  97. selected: this.state.selected < this.state.autoCompleteData.length - 1 ? this.state.selected + 1 : this.state.autoCompleteData.length - 1
  98. });
  99. } else if (e.keyCode === 38) {
  100. this.setState({
  101. selected: this.state.selected > 0 ? this.state.selected - 1 : 0
  102. });
  103. } else if (e.key === "Enter") {
  104. e.preventDefault();
  105. if (this.state.selected > -1) {
  106. this.select(this.state.data[this.state.autoCompleteData[this.state.selected]]);
  107. } else {
  108. this.select({
  109. key: this.state.value
  110. });
  111. }
  112. }
  113. }
  114. }, {
  115. key: "render",
  116. value: function render() {
  117. var _this2 = this;
  118. return _react.default.createElement("div", {
  119. className: 'MyAutocomplete ' + this.props.className
  120. }, _react.default.createElement("form", {
  121. autoComplete: "off",
  122. style: {
  123. flexGrow: 1
  124. }
  125. }, _react.default.createElement("input", {
  126. onChange: function onChange(e) {
  127. return _this2.change(e);
  128. },
  129. value: this.state.value,
  130. placeholder: this.props.placeholder || 'Search',
  131. className: "autocomplete-options-fa98bb6c-92e7-abe2-025b-b9f8e505ff98",
  132. onKeyDown: function onKeyDown(e) {
  133. return _this2.keyHandler(e);
  134. }
  135. })), this.state.autoComplete ? _react.default.createElement("ul", {
  136. id: "autocomplete-options-d483f2ec-fa7d-1905-b56c-81047362fa8e",
  137. className: "autocomplete-content dropdown-content " + (this.state.autoComplete ? "myautocomplete-visible" : ""),
  138. tabindex: "0"
  139. }, this.state.autoCompleteData.map(function (item, indx) {
  140. return _react.default.createElement("li", {
  141. key: item,
  142. onClick: function onClick() {
  143. return _this2.select(_this2.state.data[item]);
  144. },
  145. className: _this2.state.selected === indx ? "active" : "",
  146. onMouseEnter: function onMouseEnter() {
  147. return _this2.setState({
  148. selected: -1
  149. });
  150. }
  151. }, _this2.state.data[item] && _react.default.createElement("img", {
  152. src: _this2.state.data[item].image,
  153. class: "right circle"
  154. }), _react.default.createElement("span", null, item));
  155. })) : null);
  156. }
  157. }]);
  158. return MyAutocomplete;
  159. }(_react.default.Component);
  160. var _default = MyAutocomplete;
  161. exports.default = _default;