UNPKG

@atlaskit/global-search

Version:

A cross-product search component (batteries included)

67 lines (58 loc) 1.84 kB
import _extends from "@babel/runtime/helpers/extends"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import React from 'react'; import uuid from 'uuid/v4'; const 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 class WrapperComponent extends React.Component { constructor(...args) { super(...args); _defineProperty(this, "searchSessionId", null); } render() { return /*#__PURE__*/React.createElement(SearchSessionContext.Consumer, null, ({ searchSessionId }) => { if (!this.searchSessionId) { this.searchSessionId = searchSessionId || uuid(); } return /*#__PURE__*/React.createElement(Component, _extends({}, this.props, { searchSessionId: this.searchSessionId })); }); } }; } /** * 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. */ export default class SearchSessionProvider extends React.Component { constructor(...args) { super(...args); _defineProperty(this, "state", { searchSessionId: uuid() }); } render() { const { children } = this.props; const { searchSessionId } = this.state; return /*#__PURE__*/React.createElement(SearchSessionContext.Provider, { value: { searchSessionId } }, children); } }