UNPKG

@atlaskit/global-search

Version:

A cross-product search component (batteries included)

114 lines (87 loc) 4.28 kB
import _extends from "@babel/runtime/helpers/extends"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } import React from 'react'; import uuid from 'uuid/v4'; var SearchSessionContext = /*#__PURE__*/React.createContext({ searchSessionId: undefined }); /** * Wraps a component and provides the component with a searchSessionId. * The searchSessionId will either be retrieved from the closest SearchSessionProvider or a new one * will be generated with the wrapped component is mounted. */ export function injectSearchSession(Component) { return /*#__PURE__*/function (_React$Component) { _inherits(WrapperComponent, _React$Component); var _super = _createSuper(WrapperComponent); function WrapperComponent() { var _this; _classCallCheck(this, WrapperComponent); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _defineProperty(_assertThisInitialized(_this), "searchSessionId", null); return _this; } _createClass(WrapperComponent, [{ key: "render", value: function render() { var _this2 = this; return /*#__PURE__*/React.createElement(SearchSessionContext.Consumer, null, function (_ref) { var searchSessionId = _ref.searchSessionId; if (!_this2.searchSessionId) { _this2.searchSessionId = searchSessionId || uuid(); } return /*#__PURE__*/React.createElement(Component, _extends({}, _this2.props, { searchSessionId: _this2.searchSessionId })); }); } }]); return WrapperComponent; }(React.Component); } /** * A search session context provider. * This provides all children wrapped with injectSearchSession with the same search session id. * Noted a new search session id is generated if and only if this component is mounted. */ var SearchSessionProvider = /*#__PURE__*/function (_React$Component2) { _inherits(SearchSessionProvider, _React$Component2); var _super2 = _createSuper(SearchSessionProvider); function SearchSessionProvider() { var _this3; _classCallCheck(this, SearchSessionProvider); for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } _this3 = _super2.call.apply(_super2, [this].concat(args)); _defineProperty(_assertThisInitialized(_this3), "state", { searchSessionId: uuid() }); return _this3; } _createClass(SearchSessionProvider, [{ key: "render", value: function render() { var children = this.props.children; var searchSessionId = this.state.searchSessionId; return /*#__PURE__*/React.createElement(SearchSessionContext.Provider, { value: { searchSessionId: searchSessionId } }, children); } }]); return SearchSessionProvider; }(React.Component); export { SearchSessionProvider as default };