All files / packages/react-tao/src withContext.js

77.78% Statements 7/9
75% Branches 3/4
50% Functions 2/4
77.78% Lines 7/9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26          16x     16x 12x 4x           4x       8x   4x      
import React from 'react';
 
import createContextHandler from './createContextHandler';
 
export default function withContext(tao, handler, defaultValue) {
  Iif (typeof handler !== 'function') {
    throw new Error('withContext `handler` must be a function');
  }
  const WrappingContext = createContextHandler(tao, handler, defaultValue);
  return ComponentToWrap => {
    const wrappedComponent = props => (
      <WrappingContext.Provider>
        <WrappingContext.Consumer>
          {/* value =>
            React.cloneElement(ComponentToWrap, { data: value, ...props })
          */}
          {value => <ComponentToWrap data={value} {...props} />}
        </WrappingContext.Consumer>
      </WrappingContext.Provider>
    );
    wrappedComponent.displayName = `withContext(${ComponentToWrap.displayName ||
      ComponentToWrap.name}`;
    return wrappedComponent;
  };
}