Skip to content

Commit 9886e69

Browse files
authored
feat: add types, change published directory (#10)
BREAKING CHANGE: remove lib directory
1 parent 7b5bd50 commit 9886e69

File tree

7 files changed

+1120
-30
lines changed

7 files changed

+1120
-30
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ after_script:
88
- yarn codecov
99

1010
after_success:
11+
- yarn run build
1112
- yarn travis-deploy-once "yarn semantic-release"
1213

1314
cache: yarn

package.json

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"name": "react-context-toolbox",
33
"version": "1.2.3",
44
"main": "lib/index.js",
5-
"modules": "es/index.js",
6-
"jsnext:main": "es/index.js",
5+
"module": "lib/es/index.js",
6+
"types": "types/index.d.ts",
77
"repository": {
88
"type": "git",
99
"url": "https://github.com/4Catalyzer/react-context-toolbox.git"
@@ -14,12 +14,15 @@
1414
"tdd": "jest --watch",
1515
"test": "npm run lint && jest",
1616
"testonly": "jest",
17-
"build:es": "babel src -d es --env-name esm --ignore **/__tests__ --delete-dir-on-start",
18-
"build:lib": "babel src -d lib --ignore **/__tests__ --delete-dir-on-start",
17+
"build:es": "babel src -d lib/es --env-name esm --ignore **/__tests__ ",
18+
"build:lib":
19+
"babel src -d lib --ignore **/__tests__ --delete-dir-on-start ",
1920
"build": "npm run build:lib && npm run build:es",
2021
"prepublishOnly": "yarn run build",
21-
"lint": "eslint . && prettier --list-different --ignore-path .eslintignore '**/*.{json,css,md}'",
22-
"format": "eslint . --fix && prettier --write --ignore-path .eslintignore '**/*.{json,css,md}'",
22+
"lint":
23+
"eslint . && prettier --list-different --ignore-path .eslintignore '**/*.{json,css,md}'",
24+
"format":
25+
"eslint . --fix && prettier --write --ignore-path .eslintignore '**/*.{json,css,md}'",
2326
"precommit": "lint-staged"
2427
},
2528
"publishConfig": {
@@ -31,38 +34,32 @@
3134
"trailingComma": "all"
3235
},
3336
"lint-staged": {
34-
"*.js": [
35-
"eslint --fix",
36-
"git add"
37-
],
37+
"*.js": ["eslint --fix", "git add"],
3838
"*.{json,css,md}": [
3939
"prettier --write --ignore-path .eslintignore",
4040
"git add"
4141
]
4242
},
4343
"jest": {
44-
"roots": [
45-
"<rootDir>/test"
46-
],
44+
"roots": ["<rootDir>/test"],
4745
"testEnvironment": "jsdom",
48-
"setupFiles": [
49-
"<rootDir>/test/index.js"
50-
]
46+
"setupFiles": ["<rootDir>/test/index.js"]
5147
},
5248
"release": {
53-
"extends": [
54-
"@4c/semantic-release-config"
55-
]
49+
"extends": ["@4c/semantic-release-config"],
50+
"pkgRoot": "lib"
5651
},
5752
"devDependencies": {
5853
"@4c/babel-preset-4catalyzer": "^1.0.0",
5954
"@4c/semantic-release-config": "^1.0.2",
6055
"@babel/cli": "^7.0.0-beta.39",
6156
"@babel/core": "^7.0.0-beta.39",
57+
"@types/react": "^16.7.8",
6258
"babel-core": "bridge",
6359
"babel-eslint": "^8.2.1",
6460
"babel-jest": "^22.4.3",
6561
"codecov": "^3.0.2",
62+
"dtslint": "^0.3.0",
6663
"enzyme": "^3.7.0",
6764
"enzyme-adapter-react-16": "^1.6.0",
6865
"eslint": "^4.16.0",

types/index.d.ts

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
declare module 'react-context-toolbox/forwardRef' {
2+
import * as React from 'react';
3+
4+
interface ForwardRefOptions<TProps> {
5+
displayName?: string;
6+
propTypes?: React.ValidationMap<TProps>;
7+
defaultProps?: Partial<TProps>;
8+
allowFallback?: boolean;
9+
}
10+
11+
function forwardRef<TRef, TProps>(
12+
renderFn: (
13+
props: TProps & { children?: React.ReactNode },
14+
ref: React.Ref<TRef> | null,
15+
) => React.ReactElement<any> | null,
16+
options?: ForwardRefOptions<TProps>,
17+
): React.ForwardRefExoticComponent<
18+
React.PropsWithRef<TProps> & React.RefAttributes<TRef>
19+
>;
20+
21+
export default forwardRef;
22+
}
23+
24+
declare module 'react-context-toolbox/mapContextToProps' {
25+
import * as React from 'react';
26+
27+
type ElementType = React.ComponentType<any> | keyof JSX.IntrinsicElements;
28+
29+
type Omit<T, U> = Pick<T, Exclude<keyof T, keyof U>>;
30+
31+
type GetProps<C> = C extends React.ComponentType<infer P> ? P : never;
32+
33+
interface ContextInjectedComponent<TComponent, TInjectedProps, TExtraProps>
34+
extends React.ForwardRefExoticComponent<
35+
Omit<GetProps<TComponent>, TInjectedProps> & TExtraProps
36+
> {}
37+
38+
// Single Context
39+
function mapContextToProps<TComponent, TContext, TContextProps, TOwnProps>(
40+
context: React.Context<TContext>,
41+
mapToProps: (ctxValue: TContext, props: TOwnProps) => TContextProps,
42+
Component: TComponent,
43+
): ContextInjectedComponent<TComponent, TContextProps, TOwnProps>;
44+
function mapContextToProps<TContext, TContextProps, TOwnProps>(
45+
context: React.Context<TContext>,
46+
mapToProps: (ctxValue: TContext, props: TOwnProps) => TContextProps,
47+
): <TComponent>(
48+
component: TComponent,
49+
) => ContextInjectedComponent<TComponent, TContextProps, TOwnProps>;
50+
51+
function mapContextToProps<TComponent, TContext, TContextProps, TOwnProps>(
52+
context: [React.Context<TContext>],
53+
mapToProps: (ctxValue: TContext, props: TOwnProps) => TContextProps,
54+
Component: TComponent,
55+
): ContextInjectedComponent<TComponent, TContextProps, TOwnProps>;
56+
function mapContextToProps<TContext, TContextProps, TOwnProps>(
57+
context: [React.Context<TContext>],
58+
mapToProps: (ctxValue: TContext, props: TOwnProps) => TContextProps,
59+
): <TComponent>(
60+
component: TComponent,
61+
) => ContextInjectedComponent<TComponent, TContextProps, TOwnProps>;
62+
63+
// 2 Contexts
64+
function mapContextToProps<
65+
TComponent,
66+
TContext1,
67+
TContext2,
68+
TContextProps,
69+
TOwnProps
70+
>(
71+
context: [React.Context<TContext1>, React.Context<TContext2>],
72+
mapToProps: (
73+
c1: TContext1,
74+
c2: TContext2,
75+
props: TOwnProps,
76+
) => TContextProps,
77+
Component: TComponent,
78+
): ContextInjectedComponent<TComponent, TContextProps, TOwnProps>;
79+
function mapContextToProps<TContext1, TContext2, TContextProps, TOwnProps>(
80+
context: [React.Context<TContext1>, React.Context<TContext2>],
81+
mapToProps: (
82+
c1: TContext1,
83+
c2: TContext2,
84+
props: TOwnProps,
85+
) => TContextProps,
86+
): <TComponent>(
87+
component: TComponent,
88+
) => ContextInjectedComponent<TComponent, TContextProps, TOwnProps>;
89+
90+
// 3 Contexts
91+
function mapContextToProps<
92+
TComponent,
93+
TContext1,
94+
TContext2,
95+
TContext3,
96+
TContextProps,
97+
TOwnProps
98+
>(
99+
context: [
100+
React.Context<TContext1>,
101+
React.Context<TContext2>,
102+
React.Context<TContext3>
103+
],
104+
mapToProps: (
105+
c1: TContext1,
106+
c2: TContext2,
107+
c3: TContext3,
108+
props: TOwnProps,
109+
) => TContextProps,
110+
Component: TComponent,
111+
): ContextInjectedComponent<TComponent, TContextProps, TOwnProps>;
112+
function mapContextToProps<
113+
TContext1,
114+
TContext2,
115+
TContext3,
116+
TContextProps,
117+
TOwnProps
118+
>(
119+
context: [
120+
React.Context<TContext1>,
121+
React.Context<TContext2>,
122+
React.Context<TContext3>
123+
],
124+
mapToProps: (
125+
c1: TContext1,
126+
c2: TContext2,
127+
c3: TContext3,
128+
props: TOwnProps,
129+
) => TContextProps,
130+
): <TComponent>(
131+
component: TComponent,
132+
) => ContextInjectedComponent<TComponent, TContextProps, TOwnProps>;
133+
134+
// 4 Contexts
135+
function mapContextToProps<
136+
TComponent,
137+
TContext1,
138+
TContext2,
139+
TContext3,
140+
TContext4,
141+
TContextProps,
142+
TOwnProps
143+
>(
144+
context: [
145+
React.Context<TContext1>,
146+
React.Context<TContext2>,
147+
React.Context<TContext3>,
148+
React.Context<TContext4>
149+
],
150+
mapToProps: (
151+
c1: TContext1,
152+
c2: TContext2,
153+
c3: TContext3,
154+
c4: TContext4,
155+
props: TOwnProps,
156+
) => TContextProps,
157+
Component: TComponent,
158+
): ContextInjectedComponent<TComponent, TContextProps, TOwnProps>;
159+
function mapContextToProps<
160+
TContext1,
161+
TContext2,
162+
TContext3,
163+
TContext4,
164+
TContextProps,
165+
TOwnProps
166+
>(
167+
context: [
168+
React.Context<TContext1>,
169+
React.Context<TContext2>,
170+
React.Context<TContext3>,
171+
React.Context<TContext4>
172+
],
173+
mapToProps: (
174+
c1: TContext1,
175+
c2: TContext2,
176+
c3: TContext3,
177+
c4: TContext4,
178+
props: TOwnProps,
179+
) => TContextProps,
180+
): <TComponent>(
181+
component: TComponent,
182+
) => ContextInjectedComponent<TComponent, TContextProps, TOwnProps>;
183+
184+
export default mapContextToProps;
185+
}

types/test.tsx

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// TypeScript Version: 3.0
2+
3+
import * as React from 'react';
4+
5+
import forwardRef from 'react-context-toolbox/forwardRef';
6+
import mapContextToProps from 'react-context-toolbox/mapContextToProps';
7+
8+
const Foo = (props: {
9+
bar: boolean;
10+
innerRef?: React.Ref<HTMLDivElement>;
11+
}) => <div ref={props.innerRef} />;
12+
13+
const ForwardedFoo = forwardRef(
14+
(props: { bar: boolean }, ref: React.Ref<HTMLDivElement>) => (
15+
<Foo innerRef={ref} bar={props.bar} />
16+
),
17+
);
18+
19+
<ForwardedFoo bar={false} />;
20+
<ForwardedFoo bar={false} ref={React.createRef()} />;
21+
22+
// $ExpectError
23+
<Foo bar={false} ref={React.createRef()} />;
24+
25+
const Context1 = React.createContext<boolean>(false);
26+
27+
const MappedFoo1 = mapContextToProps(
28+
Context1,
29+
value => ({
30+
bar: value,
31+
}),
32+
Foo,
33+
);
34+
35+
<MappedFoo1 />;
36+
37+
// $ExpectError
38+
<MappedFoo1 bar={false} />;
39+
40+
const MappedFoo2 = mapContextToProps(
41+
Context1,
42+
(value, props: { bar?: boolean }) => ({
43+
bar: props.bar == null ? value : props.bar,
44+
}),
45+
Foo,
46+
);
47+
48+
<MappedFoo2 />;
49+
50+
<MappedFoo2 bar={false} />;
51+
52+
// $ExpectError
53+
<MappedFoo2 bar="foo" />;
54+
55+
const Curried = mapContextToProps(
56+
Context1,
57+
(value, props: { bar?: boolean }) => ({
58+
bar: props.bar == null ? value : props.bar,
59+
}),
60+
);
61+
62+
const MappedFoo3 = Curried(Foo);
63+
64+
<MappedFoo3 />;
65+
66+
<MappedFoo3 bar={false} />;
67+
68+
const Context2 = React.createContext<string>('baz');
69+
const Context3 = React.createContext<string>('baz');
70+
const Context4 = React.createContext<string>('baz');
71+
const Context5 = React.createContext<string>('baz');
72+
73+
const MultiFoo = (props: {
74+
v1: boolean;
75+
v2?: string;
76+
v3?: string;
77+
v4?: string;
78+
v5?: string;
79+
}) => <div />;
80+
81+
const Foo2Context = mapContextToProps(
82+
[Context1, Context2],
83+
(v1, v2) => ({ v1, v2 }),
84+
MultiFoo,
85+
);
86+
87+
<Foo2Context />;
88+
89+
const Foo3Context = mapContextToProps(
90+
[Context1, Context2, Context3],
91+
(v1, v2, v3) => ({ v1, v2, v3 }),
92+
MultiFoo,
93+
);
94+
95+
<Foo3Context />;
96+
97+
const Foo4Context = mapContextToProps(
98+
[Context1, Context2, Context3, Context4],
99+
(v1, v2, v3, v4) => ({ v1, v2, v3, v4 }),
100+
MultiFoo,
101+
);
102+
103+
<Foo4Context />;

types/tsconfig.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": "../",
4+
"forceConsistentCasingInFileNames": true,
5+
"jsx": "react",
6+
"lib": ["dom", "es2016", "esnext.asynciterable"],
7+
"module": "commonjs",
8+
"noEmit": true,
9+
"noImplicitAny": true,
10+
"noImplicitThis": true,
11+
"strict": true,
12+
"strictNullChecks": true,
13+
"strictFunctionTypes": true,
14+
"target": "es5",
15+
"typeRoots": ["../"],
16+
"types": []
17+
},
18+
"include": ["./*.ts", "./*.tsx"]
19+
}

0 commit comments

Comments
 (0)