@exabytellc/utils
Version:
EB react utils to make everything a little easier!
35 lines (31 loc) • 1.19 kB
JavaScript
import React, { useMemo } from "react";
import PropTypes from "prop-types";
/**
* Provider component that wraps its children with multiple Bloc providers.
*
* @param {Object} props - The props for the provider.
* @param {Array} props.blocs - An array of Bloc components to wrap the children with.
* @param {React.ReactNode} props.children - The children components to be wrapped.
* @returns {React.ReactNode} - The wrapped children components.
*/
import { jsx as _jsx } from "react/jsx-runtime";
export default function BlocsProvider(_ref) {
let {
blocs,
children
} = _ref;
const blocsArr = useMemo(() => blocs?.reverse() ?? [], [blocs]);
// If no blocs are provided or the array is empty, return children as is
if (blocsArr.length == 0) return children;
// Reduce the blocs array to wrap children in each Bloc provider
return blocsArr.reduce((child, Bloc) => /*#__PURE__*/_jsx(Bloc.Provider, {
...(Bloc.props ?? {}),
children: child
}), children);
}
// Prop types for validation
BlocsProvider.propTypes = {
blocs: PropTypes.arrayOf(PropTypes.elementType),
// Array of Bloc components
children: PropTypes.node // Any valid React node
};