UNPKG

react-autosuggest

Version:

WAI-ARIA compliant React autosuggest component

851 lines (704 loc) 34.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _arrays = _interopRequireDefault(require("shallow-equal/arrays")); var _Autowhatever = _interopRequireDefault(require("./Autowhatever")); var _theme = require("./theme"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _typeof(obj) { "@babel/helpers - typeof"; 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); } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } 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); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var alwaysTrue = function alwaysTrue() { return true; }; var defaultShouldRenderSuggestions = function defaultShouldRenderSuggestions(value) { return value.trim().length > 0; }; var defaultRenderSuggestionsContainer = function defaultRenderSuggestionsContainer(_ref) { var containerProps = _ref.containerProps, children = _ref.children; return /*#__PURE__*/_react["default"].createElement("div", containerProps, children); }; var REASON_SUGGESTIONS_REVEALED = 'suggestions-revealed'; var REASON_SUGGESTIONS_UPDATED = 'suggestions-updated'; var REASON_SUGGESTION_SELECTED = 'suggestion-selected'; var REASON_INPUT_FOCUSED = 'input-focused'; var REASON_INPUT_CHANGED = 'input-changed'; var REASON_INPUT_BLURRED = 'input-blurred'; var REASON_ESCAPE_PRESSED = 'escape-pressed'; var Autosuggest = /*#__PURE__*/function (_Component) { _inherits(Autosuggest, _Component); var _super = _createSuper(Autosuggest); function Autosuggest(_ref2) { var _this; var _alwaysRenderSuggestions = _ref2.alwaysRenderSuggestions; _classCallCheck(this, Autosuggest); _this = _super.call(this); _defineProperty(_assertThisInitialized(_this), "onDocumentMouseDown", function (event) { _this.justClickedOnSuggestionsContainer = false; var node = event.detail && event.detail.target || // This is for testing only. Please show me a better way to emulate this. event.target; while (node !== null && node !== document) { if (node.getAttribute && node.getAttribute('data-suggestion-index') !== null) { // Suggestion was clicked return; } if (node === _this.suggestionsContainer) { // Something else inside suggestions container was clicked _this.justClickedOnSuggestionsContainer = true; return; } node = node.parentNode; } }); _defineProperty(_assertThisInitialized(_this), "storeAutowhateverRef", function (autowhatever) { if (autowhatever !== null) { _this.autowhatever = autowhatever; } }); _defineProperty(_assertThisInitialized(_this), "onSuggestionMouseEnter", function (event, _ref3) { var sectionIndex = _ref3.sectionIndex, itemIndex = _ref3.itemIndex; _this.updateHighlightedSuggestion(sectionIndex, itemIndex); if (event.target === _this.pressedSuggestion) { _this.justSelectedSuggestion = true; } _this.justMouseEntered = true; setTimeout(function () { _this.justMouseEntered = false; }); }); _defineProperty(_assertThisInitialized(_this), "highlightFirstSuggestion", function () { _this.updateHighlightedSuggestion(_this.props.multiSection ? 0 : null, 0); }); _defineProperty(_assertThisInitialized(_this), "onDocumentMouseUp", function () { if (_this.pressedSuggestion && !_this.justSelectedSuggestion) { _this.input.focus(); } _this.pressedSuggestion = null; }); _defineProperty(_assertThisInitialized(_this), "onSuggestionMouseDown", function (event) { // Checking if this.justSelectedSuggestion is already true to not duplicate touch events in chrome // See: https://github.com/facebook/react/issues/9809#issuecomment-413978405 if (!_this.justSelectedSuggestion) { _this.justSelectedSuggestion = true; _this.pressedSuggestion = event.target; } }); _defineProperty(_assertThisInitialized(_this), "onSuggestionsClearRequested", function () { var onSuggestionsClearRequested = _this.props.onSuggestionsClearRequested; onSuggestionsClearRequested && onSuggestionsClearRequested(); }); _defineProperty(_assertThisInitialized(_this), "onSuggestionSelected", function (event, data) { var _this$props = _this.props, alwaysRenderSuggestions = _this$props.alwaysRenderSuggestions, onSuggestionSelected = _this$props.onSuggestionSelected, onSuggestionsFetchRequested = _this$props.onSuggestionsFetchRequested; onSuggestionSelected && onSuggestionSelected(event, data); var keepSuggestionsOnSelect = _this.props.shouldKeepSuggestionsOnSelect(data.suggestion); if (alwaysRenderSuggestions || keepSuggestionsOnSelect) { onSuggestionsFetchRequested({ value: data.suggestionValue, reason: REASON_SUGGESTION_SELECTED }); } else { _this.onSuggestionsClearRequested(); } _this.resetHighlightedSuggestion(); }); _defineProperty(_assertThisInitialized(_this), "onSuggestionClick", function (event) { var _this$props2 = _this.props, alwaysRenderSuggestions = _this$props2.alwaysRenderSuggestions, focusInputOnSuggestionClick = _this$props2.focusInputOnSuggestionClick; var _this$getSuggestionIn = _this.getSuggestionIndices(_this.findSuggestionElement(event.target)), sectionIndex = _this$getSuggestionIn.sectionIndex, suggestionIndex = _this$getSuggestionIn.suggestionIndex; var clickedSuggestion = _this.getSuggestion(sectionIndex, suggestionIndex); var clickedSuggestionValue = _this.props.getSuggestionValue(clickedSuggestion); _this.maybeCallOnChange(event, clickedSuggestionValue, 'click'); _this.onSuggestionSelected(event, { suggestion: clickedSuggestion, suggestionValue: clickedSuggestionValue, suggestionIndex: suggestionIndex, sectionIndex: sectionIndex, method: 'click' }); var keepSuggestionsOnSelect = _this.props.shouldKeepSuggestionsOnSelect(clickedSuggestion); if (!(alwaysRenderSuggestions || keepSuggestionsOnSelect)) { _this.closeSuggestions(); } if (focusInputOnSuggestionClick === true) { _this.input.focus(); } else { _this.onBlur(); } setTimeout(function () { _this.justSelectedSuggestion = false; }); }); _defineProperty(_assertThisInitialized(_this), "onBlur", function () { var _this$props3 = _this.props, inputProps = _this$props3.inputProps, shouldRenderSuggestions = _this$props3.shouldRenderSuggestions; var value = inputProps.value, onBlur = inputProps.onBlur; var highlightedSuggestion = _this.getHighlightedSuggestion(); var shouldRender = shouldRenderSuggestions(value, REASON_INPUT_BLURRED); _this.setState({ isFocused: false, highlightedSectionIndex: null, highlightedSuggestionIndex: null, highlightedSuggestion: null, valueBeforeUpDown: null, isCollapsed: !shouldRender }); onBlur && onBlur(_this.blurEvent, { highlightedSuggestion: highlightedSuggestion }); }); _defineProperty(_assertThisInitialized(_this), "onSuggestionMouseLeave", function (event) { _this.resetHighlightedSuggestion(false); // shouldResetValueBeforeUpDown if (_this.justSelectedSuggestion && event.target === _this.pressedSuggestion) { _this.justSelectedSuggestion = false; } }); _defineProperty(_assertThisInitialized(_this), "onSuggestionTouchStart", function () { _this.justSelectedSuggestion = true; // todo: event.preventDefault when https://github.com/facebook/react/issues/2043 // todo: gets released so onSuggestionMouseDown won't fire in chrome }); _defineProperty(_assertThisInitialized(_this), "onSuggestionTouchMove", function () { _this.justSelectedSuggestion = false; _this.pressedSuggestion = null; _this.input.focus(); }); _defineProperty(_assertThisInitialized(_this), "itemProps", function (_ref4) { var sectionIndex = _ref4.sectionIndex, itemIndex = _ref4.itemIndex; return { 'data-section-index': sectionIndex, 'data-suggestion-index': itemIndex, onMouseEnter: _this.onSuggestionMouseEnter, onMouseLeave: _this.onSuggestionMouseLeave, onMouseDown: _this.onSuggestionMouseDown, onTouchStart: _this.onSuggestionTouchStart, onTouchMove: _this.onSuggestionTouchMove, onClick: _this.onSuggestionClick }; }); _defineProperty(_assertThisInitialized(_this), "renderSuggestionsContainer", function (_ref5) { var containerProps = _ref5.containerProps, children = _ref5.children; var renderSuggestionsContainer = _this.props.renderSuggestionsContainer; return renderSuggestionsContainer({ containerProps: containerProps, children: children, query: _this.getQuery() }); }); _this.state = { isFocused: false, isCollapsed: !_alwaysRenderSuggestions, highlightedSectionIndex: null, highlightedSuggestionIndex: null, highlightedSuggestion: null, valueBeforeUpDown: null }; _this.justPressedUpDown = false; _this.justMouseEntered = false; _this.pressedSuggestion = null; return _this; } _createClass(Autosuggest, [{ key: "componentDidMount", value: function componentDidMount() { document.addEventListener('mousedown', this.onDocumentMouseDown); document.addEventListener('mouseup', this.onDocumentMouseUp); this.input = this.autowhatever.input; this.suggestionsContainer = this.autowhatever.itemsContainer; } // eslint-disable-next-line camelcase, react/sort-comp }, { key: "UNSAFE_componentWillReceiveProps", value: function UNSAFE_componentWillReceiveProps(nextProps) { // When highlightFirstSuggestion becomes deactivated, if the first suggestion was // set, we should reset the suggestion back to the unselected default state. var shouldResetHighlighting = this.state.highlightedSuggestionIndex === 0 && this.props.highlightFirstSuggestion && !nextProps.highlightFirstSuggestion; if ((0, _arrays["default"])(nextProps.suggestions, this.props.suggestions)) { if (nextProps.highlightFirstSuggestion && nextProps.suggestions.length > 0 && this.justPressedUpDown === false && this.justMouseEntered === false) { this.highlightFirstSuggestion(); } else if (shouldResetHighlighting) { this.resetHighlightedSuggestion(); } } else { if (this.willRenderSuggestions(nextProps, REASON_SUGGESTIONS_UPDATED)) { if (this.state.isCollapsed && !this.justSelectedSuggestion) { this.revealSuggestions(); } if (shouldResetHighlighting) { this.resetHighlightedSuggestion(); } } else { this.resetHighlightedSuggestion(); } } } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps, prevState) { var _this$props4 = this.props, suggestions = _this$props4.suggestions, onSuggestionHighlighted = _this$props4.onSuggestionHighlighted, highlightFirstSuggestion = _this$props4.highlightFirstSuggestion; if (!(0, _arrays["default"])(suggestions, prevProps.suggestions) && suggestions.length > 0 && highlightFirstSuggestion) { this.highlightFirstSuggestion(); return; } if (onSuggestionHighlighted) { var highlightedSuggestion = this.getHighlightedSuggestion(); var prevHighlightedSuggestion = prevState.highlightedSuggestion; if (highlightedSuggestion != prevHighlightedSuggestion) { onSuggestionHighlighted({ suggestion: highlightedSuggestion }); } } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { document.removeEventListener('mousedown', this.onDocumentMouseDown); document.removeEventListener('mouseup', this.onDocumentMouseUp); } }, { key: "updateHighlightedSuggestion", value: function updateHighlightedSuggestion(sectionIndex, suggestionIndex, prevValue) { var _this2 = this; this.setState(function (state) { var valueBeforeUpDown = state.valueBeforeUpDown; if (suggestionIndex === null) { valueBeforeUpDown = null; } else if (valueBeforeUpDown === null && typeof prevValue !== 'undefined') { valueBeforeUpDown = prevValue; } return { highlightedSectionIndex: sectionIndex, highlightedSuggestionIndex: suggestionIndex, highlightedSuggestion: suggestionIndex === null ? null : _this2.getSuggestion(sectionIndex, suggestionIndex), valueBeforeUpDown: valueBeforeUpDown }; }); } }, { key: "resetHighlightedSuggestion", value: function resetHighlightedSuggestion() { var shouldResetValueBeforeUpDown = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; this.setState(function (state) { var valueBeforeUpDown = state.valueBeforeUpDown; return { highlightedSectionIndex: null, highlightedSuggestionIndex: null, highlightedSuggestion: null, valueBeforeUpDown: shouldResetValueBeforeUpDown ? null : valueBeforeUpDown }; }); } }, { key: "revealSuggestions", value: function revealSuggestions() { this.setState({ isCollapsed: false }); } }, { key: "closeSuggestions", value: function closeSuggestions() { this.setState({ highlightedSectionIndex: null, highlightedSuggestionIndex: null, highlightedSuggestion: null, valueBeforeUpDown: null, isCollapsed: true }); } }, { key: "getSuggestion", value: function getSuggestion(sectionIndex, suggestionIndex) { var _this$props5 = this.props, suggestions = _this$props5.suggestions, multiSection = _this$props5.multiSection, getSectionSuggestions = _this$props5.getSectionSuggestions; if (multiSection) { return getSectionSuggestions(suggestions[sectionIndex])[suggestionIndex]; } return suggestions[suggestionIndex]; } }, { key: "getHighlightedSuggestion", value: function getHighlightedSuggestion() { var _this$state = this.state, highlightedSectionIndex = _this$state.highlightedSectionIndex, highlightedSuggestionIndex = _this$state.highlightedSuggestionIndex; if (highlightedSuggestionIndex === null) { return null; } return this.getSuggestion(highlightedSectionIndex, highlightedSuggestionIndex); } }, { key: "getSuggestionValueByIndex", value: function getSuggestionValueByIndex(sectionIndex, suggestionIndex) { var getSuggestionValue = this.props.getSuggestionValue; return getSuggestionValue(this.getSuggestion(sectionIndex, suggestionIndex)); } }, { key: "getSuggestionIndices", value: function getSuggestionIndices(suggestionElement) { var sectionIndex = suggestionElement.getAttribute('data-section-index'); var suggestionIndex = suggestionElement.getAttribute('data-suggestion-index'); return { sectionIndex: typeof sectionIndex === 'string' ? parseInt(sectionIndex, 10) : null, suggestionIndex: parseInt(suggestionIndex, 10) }; } }, { key: "findSuggestionElement", value: function findSuggestionElement(startNode) { var node = startNode; do { if (node.getAttribute && node.getAttribute('data-suggestion-index') !== null) { return node; } node = node.parentNode; } while (node !== null); console.error('Clicked element:', startNode); // eslint-disable-line no-console throw new Error("Couldn't find suggestion element"); } }, { key: "maybeCallOnChange", value: function maybeCallOnChange(event, newValue, method) { var _this$props$inputProp = this.props.inputProps, value = _this$props$inputProp.value, onChange = _this$props$inputProp.onChange; if (newValue !== value) { onChange(event, { newValue: newValue, method: method }); } } }, { key: "willRenderSuggestions", value: function willRenderSuggestions(props, reason) { var suggestions = props.suggestions, inputProps = props.inputProps, shouldRenderSuggestions = props.shouldRenderSuggestions; var value = inputProps.value; return suggestions.length > 0 && shouldRenderSuggestions(value, reason); } }, { key: "getQuery", value: function getQuery() { var inputProps = this.props.inputProps; var value = inputProps.value; var valueBeforeUpDown = this.state.valueBeforeUpDown; return (valueBeforeUpDown === null ? value : valueBeforeUpDown).trim(); } }, { key: "render", value: function render() { var _this3 = this; var _this$props6 = this.props, suggestions = _this$props6.suggestions, renderInputComponent = _this$props6.renderInputComponent, onSuggestionsFetchRequested = _this$props6.onSuggestionsFetchRequested, renderSuggestion = _this$props6.renderSuggestion, inputProps = _this$props6.inputProps, multiSection = _this$props6.multiSection, renderSectionTitle = _this$props6.renderSectionTitle, id = _this$props6.id, getSectionSuggestions = _this$props6.getSectionSuggestions, theme = _this$props6.theme, getSuggestionValue = _this$props6.getSuggestionValue, alwaysRenderSuggestions = _this$props6.alwaysRenderSuggestions, highlightFirstSuggestion = _this$props6.highlightFirstSuggestion, containerProps = _this$props6.containerProps; var _this$state2 = this.state, isFocused = _this$state2.isFocused, isCollapsed = _this$state2.isCollapsed, highlightedSectionIndex = _this$state2.highlightedSectionIndex, highlightedSuggestionIndex = _this$state2.highlightedSuggestionIndex, valueBeforeUpDown = _this$state2.valueBeforeUpDown; var shouldRenderSuggestions = alwaysRenderSuggestions ? alwaysTrue : this.props.shouldRenderSuggestions; var value = inputProps.value, _onFocus = inputProps.onFocus, _onKeyDown = inputProps.onKeyDown; var willRenderSuggestions = this.willRenderSuggestions(this.props, 'render'); var isOpen = alwaysRenderSuggestions || isFocused && !isCollapsed && willRenderSuggestions; var items = isOpen ? suggestions : []; var autowhateverInputProps = _objectSpread({}, inputProps, { onFocus: function onFocus(event) { if (!_this3.justSelectedSuggestion && !_this3.justClickedOnSuggestionsContainer) { var shouldRender = shouldRenderSuggestions(value, REASON_INPUT_FOCUSED); _this3.setState({ isFocused: true, isCollapsed: !shouldRender }); _onFocus && _onFocus(event); if (shouldRender) { onSuggestionsFetchRequested({ value: value, reason: REASON_INPUT_FOCUSED }); } } }, onBlur: function onBlur(event) { if (_this3.justClickedOnSuggestionsContainer) { _this3.input.focus(); return; } _this3.blurEvent = event; if (!_this3.justSelectedSuggestion) { _this3.onBlur(); _this3.onSuggestionsClearRequested(); } }, onChange: function onChange(event) { var value = event.target.value; var shouldRender = shouldRenderSuggestions(value, REASON_INPUT_CHANGED); _this3.maybeCallOnChange(event, value, 'type'); if (_this3.suggestionsContainer) { _this3.suggestionsContainer.scrollTop = 0; } _this3.setState(_objectSpread({}, highlightFirstSuggestion ? {} : { highlightedSectionIndex: null, highlightedSuggestionIndex: null, highlightedSuggestion: null }, { valueBeforeUpDown: null, isCollapsed: !shouldRender })); if (shouldRender) { onSuggestionsFetchRequested({ value: value, reason: REASON_INPUT_CHANGED }); } else { _this3.onSuggestionsClearRequested(); } }, onKeyDown: function onKeyDown(event, data) { var keyCode = event.keyCode; switch (keyCode) { case 40: // ArrowDown case 38: // ArrowUp if (isCollapsed) { if (shouldRenderSuggestions(value, REASON_SUGGESTIONS_REVEALED)) { onSuggestionsFetchRequested({ value: value, reason: REASON_SUGGESTIONS_REVEALED }); _this3.revealSuggestions(); event.preventDefault(); // We act on the key. } } else if (suggestions.length > 0) { var newHighlightedSectionIndex = data.newHighlightedSectionIndex, newHighlightedItemIndex = data.newHighlightedItemIndex; var newValue; if (newHighlightedItemIndex === null) { // valueBeforeUpDown can be null if, for example, user // hovers on the first suggestion and then pressed Up. // If that happens, use the original input value. newValue = valueBeforeUpDown === null ? value : valueBeforeUpDown; } else { newValue = _this3.getSuggestionValueByIndex(newHighlightedSectionIndex, newHighlightedItemIndex); } _this3.updateHighlightedSuggestion(newHighlightedSectionIndex, newHighlightedItemIndex, value); _this3.maybeCallOnChange(event, newValue, keyCode === 40 ? 'down' : 'up'); event.preventDefault(); // We act on the key. } _this3.justPressedUpDown = true; setTimeout(function () { _this3.justPressedUpDown = false; }); break; // Enter case 13: { // See #388 if (event.keyCode === 229) { break; } var highlightedSuggestion = _this3.getHighlightedSuggestion(); if (isOpen && !alwaysRenderSuggestions) { _this3.closeSuggestions(); } if (highlightedSuggestion != null) { event.preventDefault(); var _newValue = getSuggestionValue(highlightedSuggestion); _this3.maybeCallOnChange(event, _newValue, 'enter'); _this3.onSuggestionSelected(event, { suggestion: highlightedSuggestion, suggestionValue: _newValue, suggestionIndex: highlightedSuggestionIndex, sectionIndex: highlightedSectionIndex, method: 'enter' }); _this3.justSelectedSuggestion = true; setTimeout(function () { _this3.justSelectedSuggestion = false; }); } break; } // Escape case 27: { if (isOpen) { // If input.type === 'search', the browser clears the input // when Escape is pressed. We want to disable this default // behaviour so that, when suggestions are shown, we just hide // them, without clearing the input. event.preventDefault(); } var willCloseSuggestions = isOpen && !alwaysRenderSuggestions; if (valueBeforeUpDown === null) { // Didn't interact with Up/Down if (!willCloseSuggestions) { var _newValue2 = ''; _this3.maybeCallOnChange(event, _newValue2, 'escape'); if (shouldRenderSuggestions(_newValue2, REASON_ESCAPE_PRESSED)) { onSuggestionsFetchRequested({ value: _newValue2, reason: REASON_ESCAPE_PRESSED }); } else { _this3.onSuggestionsClearRequested(); } } } else { // Interacted with Up/Down _this3.maybeCallOnChange(event, valueBeforeUpDown, 'escape'); } if (willCloseSuggestions) { _this3.onSuggestionsClearRequested(); _this3.closeSuggestions(); } else { _this3.resetHighlightedSuggestion(); } break; } } _onKeyDown && _onKeyDown(event); } }); var renderSuggestionData = { query: this.getQuery() }; return /*#__PURE__*/_react["default"].createElement(_Autowhatever["default"], { multiSection: multiSection, items: items, renderInputComponent: renderInputComponent, renderItemsContainer: this.renderSuggestionsContainer, renderItem: renderSuggestion, renderItemData: renderSuggestionData, renderSectionTitle: renderSectionTitle, getSectionItems: getSectionSuggestions, highlightedSectionIndex: highlightedSectionIndex, highlightedItemIndex: highlightedSuggestionIndex, containerProps: containerProps, inputProps: autowhateverInputProps, itemProps: this.itemProps, theme: (0, _theme.mapToAutowhateverTheme)(theme), id: id, ref: this.storeAutowhateverRef }); } }]); return Autosuggest; }(_react.Component); exports["default"] = Autosuggest; _defineProperty(Autosuggest, "propTypes", { suggestions: _propTypes["default"].array.isRequired, onSuggestionsFetchRequested: function onSuggestionsFetchRequested(props, propName) { var onSuggestionsFetchRequested = props[propName]; if (typeof onSuggestionsFetchRequested !== 'function') { throw new Error("'onSuggestionsFetchRequested' must be implemented. See: https://github.com/moroshko/react-autosuggest#onSuggestionsFetchRequestedProp"); } }, onSuggestionsClearRequested: function onSuggestionsClearRequested(props, propName) { var onSuggestionsClearRequested = props[propName]; if (props.alwaysRenderSuggestions === false && typeof onSuggestionsClearRequested !== 'function') { throw new Error("'onSuggestionsClearRequested' must be implemented. See: https://github.com/moroshko/react-autosuggest#onSuggestionsClearRequestedProp"); } }, shouldKeepSuggestionsOnSelect: _propTypes["default"].func, onSuggestionSelected: _propTypes["default"].func, onSuggestionHighlighted: _propTypes["default"].func, renderInputComponent: _propTypes["default"].func, renderSuggestionsContainer: _propTypes["default"].func, getSuggestionValue: _propTypes["default"].func.isRequired, renderSuggestion: _propTypes["default"].func.isRequired, inputProps: function inputProps(props, propName) { var inputProps = props[propName]; if (!inputProps) { throw new Error("'inputProps' must be passed."); } if (!Object.prototype.hasOwnProperty.call(inputProps, 'value')) { throw new Error("'inputProps' must have 'value'."); } if (!Object.prototype.hasOwnProperty.call(inputProps, 'onChange')) { throw new Error("'inputProps' must have 'onChange'."); } }, shouldRenderSuggestions: _propTypes["default"].func, alwaysRenderSuggestions: _propTypes["default"].bool, multiSection: _propTypes["default"].bool, renderSectionTitle: function renderSectionTitle(props, propName) { var renderSectionTitle = props[propName]; if (props.multiSection === true && typeof renderSectionTitle !== 'function') { throw new Error("'renderSectionTitle' must be implemented. See: https://github.com/moroshko/react-autosuggest#renderSectionTitleProp"); } }, getSectionSuggestions: function getSectionSuggestions(props, propName) { var getSectionSuggestions = props[propName]; if (props.multiSection === true && typeof getSectionSuggestions !== 'function') { throw new Error("'getSectionSuggestions' must be implemented. See: https://github.com/moroshko/react-autosuggest#getSectionSuggestionsProp"); } }, focusInputOnSuggestionClick: _propTypes["default"].bool, highlightFirstSuggestion: _propTypes["default"].bool, theme: _propTypes["default"].object, id: _propTypes["default"].string, containerProps: _propTypes["default"].object // Arbitrary container props }); _defineProperty(Autosuggest, "defaultProps", { renderSuggestionsContainer: defaultRenderSuggestionsContainer, shouldRenderSuggestions: defaultShouldRenderSuggestions, alwaysRenderSuggestions: false, multiSection: false, shouldKeepSuggestionsOnSelect: function shouldKeepSuggestionsOnSelect() { return false; }, focusInputOnSuggestionClick: true, highlightFirstSuggestion: false, theme: _theme.defaultTheme, id: '1', containerProps: {} });