Skip to content
This repository has been archived by the owner on Jan 14, 2019. It is now read-only.

Commit

Permalink
Merge eac224e into 07dfeec
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien0102 committed May 15, 2018
2 parents 07dfeec + eac224e commit 9d7e8cd
Show file tree
Hide file tree
Showing 12 changed files with 5,649 additions and 569 deletions.
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": false,
"printWidth": 120,
"parser": "typescript",
"trailingComma": "es5"
}
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,21 @@ import { connectContext } from "react-connect-context"
// CHANGE ME TO CHANGE THE CONTEXT FOR THE WHOLE APP!
const COLOR_PASSED_THROUGH_CONTEXT = "red"

interface ContextValue {
color: string;
}

interface ContentProps {
myProp: string;
color: string;
}

class App extends React.Component {
render() {
return (
<div className="demo">
<Header>Welcome to my App!</Header>
<ConnectedContent myProp="THIS IS MY PROP, HI!">
<ConnectedContent myProp="THIS IS MY PROP, HI!" >
Hello! I've written this component so that Magical Context-based text appears after children!
</ConnectedContent>
</div>
Expand All @@ -42,25 +51,24 @@ class App extends React.Component {
}
// Presentational, nested components
const Header = ({ children }) => <h1>{children}</h1>
const Content = ({ children, color, myProp }) => (
const Header: React.SFC = ({ children }) => <h1>{children}</h1>
const Content: React.SFC<ContentProps> = ({ children, color, myProp }) => (
<div>
<p>{children}</p>
<div>
I have looked into context and I've seen the color is:
<span style={{ color }}>
{" "}
<strong>{color}</strong>
</span>! I also get my own props like <strong>{myProp}</strong>!
</div>
</div>
)

// Make a context.
const Context = React.createContext({ color: "red" })
const Context = React.createContext<ContextValue>({ color: "red" })

// Pass the consumer to our function.
const ConnectedContent = connectContext(Context.Consumer)(Content)
const ConnectedContent = connectContext<ContextValue, ContentProps>(Context.Consumer)(Content)

// Render things, wrapping all in the provider.
render(
Expand Down
21 changes: 0 additions & 21 deletions __tests__/__snapshots__/index.test.js.snap

This file was deleted.

33 changes: 33 additions & 0 deletions __tests__/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`connectContext Should use the custom mergeContextWithProps function if provided 1`] = `
<div>
<p>
Sup my frand
</p>
<div>
I have looked into context and I've seen the color is:
green
I also get my own props like
sup
!
</div>
</div>
`;

exports[`connectContext should map context to props while preserving existing props 1`] = `
<div>
<div>
<p>
Hello
</p>
<div>
I have looked into context and I've seen the color is:
green
I also get my own props like
test
!
</div>
</div>
</div>
`;
42 changes: 0 additions & 42 deletions __tests__/index.test.js

This file was deleted.

78 changes: 78 additions & 0 deletions __tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import * as React from "react"
import renderer from "react-test-renderer"
import { connectContext, MergeContextWithProps } from "../"
import "jest"

interface ContentProps {
color: string
myProp: string
}

interface ContextValue {
color: string
}

const Context = React.createContext<ContextValue>({ color: "red" })
const Content: React.SFC<ContentProps> = ({ children, color, myProp }) => (
<div>
<p>{children}</p>
<div>
I have looked into context and I've seen the color is: {color}
I also get my own props like {myProp}!
</div>
</div>
)

describe("connectContext", () => {
it("should map context to props while preserving existing props", () => {
const ConnectedContent = connectContext<ContextValue, ContentProps>(
Context.Consumer
)(Content)

const App = () => (
<div>
<ConnectedContent myProp="test">Hello</ConnectedContent>
</div>
)

const tree = renderer
.create(
<Context.Provider value={{ color: "green" }}>
<App />
</Context.Provider>
)
.toJSON()

expect(tree).toMatchSnapshot()
})

it("Should use the custom mergeContextWithProps function if provided", () => {
const mergeContextWithProps: MergeContextWithProps<
ContextValue,
ContentProps
> = (context, props) => {
return ({ ...props, ...context })
}

const ConnectedContent = connectContext(
Context.Consumer,
mergeContextWithProps
)(Content)

const AppWithPropsConflict = () => (
<ConnectedContent myProp="sup" color="red">
Sup my frand
</ConnectedContent>
)

const tree = renderer
.create(
<Context.Provider value={{ color: "green" }}>
<AppWithPropsConflict />
</Context.Provider>
)
.toJSON()

expect(tree).toMatchSnapshot()
})
})
12 changes: 0 additions & 12 deletions index.js

This file was deleted.

40 changes: 40 additions & 0 deletions index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as React from "react";

export type MergeContextWithProps<C, P> = (context: C, props: P) => C & P;

function defaultMergeFn<C, P>(context: C, props: P): C & P {
return Object.assign({}, context, props);
}

type Merge<L, R> =
/** Mandatory properties */
Pick<L, Exclude<keyof L, keyof R>>
/** Optional properties */
& Partial<R>

export function connectContext<C, P>(
ContextConsumer: React.Consumer<C>,
mergeContextWithProps: MergeContextWithProps<C, P> = defaultMergeFn
) {
return function(Component: React.SFC<P>) {
return function(props: Merge<P, C>) {
// TODO remove `as any` when is available in the react typing definition
if (!(ContextConsumer as any).currentValue as any instanceof Object) {
throw new Error(
`react-connect-context: the current value of the given context identifier is _not_ an object,
and therefore cannot be passed as props to ${
Component.name
}. Please check the value in the context
and try again.
More info: https://github.com/Contiamo/react-connect-context#gotchas`
);
}
return (
<ContextConsumer>
{context => <Component {...mergeContextWithProps(context, props as P)} />}
</ContextConsumer>
);
};
};
}
37 changes: 26 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "react-connect-context",
"version": "1.0.2",
"main": "lib/bundle.js",
"license": "MIT",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
"lib/bundle.js"
"lib/*"
],
"license": "MIT",
"author": "Tejas Kumar <tejas@contiamo.com>",
"repository": {
"url": "git@github.com:Contiamo/react-connect-context.git"
Expand All @@ -18,20 +19,34 @@
},
"scripts": {
"test": "jest",
"build": "jest && rollup index.js -c",
"build": "rimraf lib && tsc -d",
"ci": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
"prepublish": "npm run build"
"prepublish": "jest && npm run build"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-jest": "^22.4.0",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"@types/jest": "^22.2.3",
"@types/react": "^16.3.13",
"@types/react-dom": "^16.0.5",
"@types/react-test-renderer": "^16.0.1",
"coveralls": "^3.0.0",
"jest": "^22.4.0",
"react-test-renderer": "^16.3.0-alpha.1",
"regenerator-runtime": "^0.11.1",
"rollup": "^0.56.2",
"rollup-plugin-babel": "^3.0.3"
"rimraf": "^2.6.2",
"ts-jest": "^22.4.4",
"typescript": "^2.8.3"
},
"jest": {
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"transform": {
"^.+\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testMatch": [
"**/__tests__/*.+(ts|tsx|js)"
]
}
}

0 comments on commit 9d7e8cd

Please sign in to comment.