react-bootstrap-typeahead
Version:
React typeahead with Bootstrap styling
74 lines (61 loc) • 3.43 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
import React, { useEffect, useRef } from 'react';
import { TypeaheadContext } from './Context';
import { defaultSelectHint, getHintText, getInputProps, getInputText, getIsOnlyResult, isFunction, pick } from '../utils';
var inputPropKeys = ['activeIndex', 'disabled', 'id', 'inputRef', 'isFocused', 'isMenuShown', 'multiple', 'onBlur', 'onChange', 'onClick', 'onFocus', 'onKeyDown', 'placeholder'];
var propKeys = ['activeIndex', 'hideMenu', 'isMenuShown', 'labelKey', 'onClear', 'onHide', 'onRemove', 'results', 'selected', 'text', 'toggleMenu'];
var contextKeys = ['activeIndex', 'id', 'initialItem', 'inputNode', 'onActiveItemChange', 'onAdd', 'onInitialItemChange', 'onMenuItemClick', 'setItem'];
var TypeaheadManager = function TypeaheadManager(props) {
var allowNew = props.allowNew,
children = props.children,
initialItem = props.initialItem,
isMenuShown = props.isMenuShown,
onAdd = props.onAdd,
onInitialItemChange = props.onInitialItemChange,
onKeyDown = props.onKeyDown,
onMenuToggle = props.onMenuToggle,
results = props.results,
selectHint = props.selectHint;
var hintText = getHintText(props);
useEffect(function () {
// Clear the initial item when there are no results.
if (!(allowNew || results.length)) {
onInitialItemChange();
}
});
var isInitialRender = useRef(true);
useEffect(function () {
if (isInitialRender.current) {
isInitialRender.current = false;
return;
}
onMenuToggle(isMenuShown);
}, [isMenuShown, onMenuToggle]);
var handleKeyDown = function handleKeyDown(e) {
onKeyDown(e);
if (!initialItem) {
return;
}
var addOnlyResult = e.key === 'Enter' && getIsOnlyResult(props);
var shouldSelectHint = hintText && defaultSelectHint(e, selectHint);
if (addOnlyResult || shouldSelectHint) {
onAdd(initialItem);
}
};
var childProps = _objectSpread(_objectSpread({}, pick(props, propKeys)), {}, {
getInputProps: getInputProps(_objectSpread(_objectSpread({}, pick(props, inputPropKeys)), {}, {
onKeyDown: handleKeyDown,
value: getInputText(props)
}))
});
var contextValue = _objectSpread(_objectSpread({}, pick(props, contextKeys)), {}, {
hintText: hintText,
isOnlyResult: getIsOnlyResult(props)
});
return /*#__PURE__*/React.createElement(TypeaheadContext.Provider, {
value: contextValue
}, isFunction(children) ? children(childProps) : children);
};
export default TypeaheadManager;