react-instantsearch-core
Version:
⚡ Lightning-fast search for React, by Algolia
55 lines • 3.52 kB
JavaScript
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(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
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;
}