Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {useMemo, useCallback} from 'react';
  2. import {BlackPortal, BlackPortalProps} from 'react-native-portal';
  3. import {NavigationContext} from 'react-navigation';
  4. import {Log} from 'common/Log';
  5. import {unique} from 'common/HelperFunctions';
  6.  
  7. type NitroxBlackPortalProps = {
  8.   /** Pass map array of contexts to proxy them into portal's scope */
  9.   contexts?: React.Context<any>[];
  10. } & BlackPortalProps;
  11. type ContextValues = any[];
  12.  
  13. const NitroxBlackPortal: React.FC<NitroxBlackPortalProps> = ({
  14.   name,
  15.   contexts,
  16.   children
  17. }) => {
  18.   const allContexts = useMemo(() => unique([
  19.     ...(contexts || []),
  20.     NavigationContext
  21.   ]), [contexts]);
  22.  
  23.   const providers = (values: ContextValues) =>
  24.     allContexts
  25.       .reduce((child, Context, contextIndex) => {
  26.         Log.debug(NitroxBlackPortal.name, `Rendering ${Context.displayName} with value of ${values[contextIndex]}. Total values: ${values.length}.`);
  27.         return (
  28.           <Context.Provider value={values[contextIndex]}>
  29.             {child}
  30.           </Context.Provider>
  31.         );
  32.       }, children);
  33.  
  34.   const renderProviders = (values: ContextValues) => (
  35.     <BlackPortal name={name}>
  36.       {providers(values)}
  37.     </BlackPortal>
  38.   );
  39.  
  40.   const consumers = allContexts
  41.     .reduce((renderChild, Context) => {
  42.       // eslint-disable-next-line react/display-name
  43.       return contextValues => (
  44.         <Context.Consumer>
  45.           {value => renderChild([value, ...contextValues])}
  46.         </Context.Consumer>
  47.       );
  48.     }, renderProviders);
  49.  
  50.   return consumers([]);
  51. };
  52.  
  53. export default NitroxBlackPortal;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement