UNPKG

react-instantsearch-core

Version:
55 lines 3.79 kB
var _excluded = ["children", "fallbackComponent"]; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } import React, { Fragment } from 'react'; import { useDynamicWidgets } from "../connectors/useDynamicWidgets.js"; import { invariant } from "../lib/invariant.js"; import { warn } from "../lib/warn.js"; function DefaultFallbackComponent() { return null; } export function DynamicWidgets(_ref) { var children = _ref.children, _ref$fallbackComponen = _ref.fallbackComponent, Fallback = _ref$fallbackComponen === void 0 ? DefaultFallbackComponent : _ref$fallbackComponen, props = _objectWithoutProperties(_ref, _excluded); var FallbackComponent = React.useRef(Fallback); process.env.NODE_ENV === 'development' ? warn(Fallback === FallbackComponent.current, 'The `fallbackComponent` prop of `DynamicWidgets` changed between renders. Please provide a stable reference, as described in https://www.algolia.com/doc/api-reference/widgets/dynamic-facets/react/#widget-param-fallbackcomponent') : void 0; var _useDynamicWidgets = useDynamicWidgets(props, { $$widgetType: 'ais.dynamicWidgets' }), attributesToRender = _useDynamicWidgets.attributesToRender; var widgets = new Map(); React.Children.forEach(children, function (child) { var attribute = getWidgetAttribute(child); invariant(attribute !== undefined, "<DynamicWidgets> only supports InstantSearch widgets with an `attribute` or `attributes` prop."); widgets.set(attribute, child); }); return /*#__PURE__*/React.createElement(React.Fragment, null, attributesToRender.map(function (attribute) { return /*#__PURE__*/React.createElement(Fragment, { key: attribute }, widgets.get(attribute) || /*#__PURE__*/React.createElement(FallbackComponent.current, { attribute: attribute })); })); } function isReactElement(element) { return _typeof(element) === 'object' && element.props; } function getWidgetAttribute(element) { if (!isReactElement(element)) { return undefined; } if (element.props.attribute) { return element.props.attribute; } if (Array.isArray(element.props.attributes)) { return element.props.attributes[0]; } if (element.props.children) { invariant(React.Children.count(element.props.children) === 1, "<DynamicWidgets> only supports a single component in nested components. Make sure to not render multiple children in a parent component.\n\nExample of an unsupported scenario:\n\n```\n<DynamicWidgets>\n <MyComponent>\n <RefinementList attribute=\"brand\" />\n <Menu attribute=\"categories\" />\n </MyComponent>\n</DynamicWidgets>\n```\n"); return getWidgetAttribute(React.Children.only(element.props.children)); } return undefined; }