Skip to content

Commit 6e69ad1

Browse files
committed
feat: add displayName to forwardRef
1 parent d942e91 commit 6e69ad1

File tree

6 files changed

+29
-48
lines changed

6 files changed

+29
-48
lines changed

package.json

+11-14
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
"testonly": "jest",
1717
"test:types": "dtslint types",
1818
"build:es": "babel src -d lib/es --env-name esm --ignore **/__tests__ ",
19-
"build:lib": "babel src -d lib --ignore **/__tests__ --delete-dir-on-start ",
20-
"build": "npm run build:lib && npm run build:es && cpy types/*.d.ts lib && cpy types/*.d.ts lib/es",
19+
"build:lib":
20+
"babel src -d lib --ignore **/__tests__ --delete-dir-on-start ",
21+
"build":
22+
"npm run build:lib && npm run build:es && cpy types/*.d.ts lib && cpy types/*.d.ts lib/es",
2123
"prepublishOnly": "yarn run build",
22-
"lint": "eslint . && prettier --list-different --ignore-path .eslintignore '**/*.{json,css,md}'",
23-
"format": "eslint . --fix && prettier --write --ignore-path .eslintignore '**/*.{json,css,md}'",
24+
"lint":
25+
"eslint . && prettier --list-different --ignore-path .eslintignore '**/*.{json,css,md}'",
26+
"format":
27+
"eslint . --fix && prettier --write --ignore-path .eslintignore '**/*.{json,css,md}'",
2428
"precommit": "lint-staged"
2529
},
2630
"publishConfig": {
@@ -32,25 +36,18 @@
3236
"trailingComma": "all"
3337
},
3438
"lint-staged": {
35-
"*.js": [
36-
"eslint --fix",
37-
"git add"
38-
],
39+
"*.js": ["eslint --fix", "git add"],
3940
"*.{json,css,md}": [
4041
"prettier --write --ignore-path .eslintignore",
4142
"git add"
4243
]
4344
},
4445
"jest": {
4546
"testEnvironment": "jsdom",
46-
"setupFiles": [
47-
"<rootDir>/test/index.js"
48-
]
47+
"setupFiles": ["<rootDir>/test/index.js"]
4948
},
5049
"release": {
51-
"extends": [
52-
"@4c/semantic-release-config"
53-
],
50+
"extends": ["@4c/semantic-release-config"],
5451
"pkgRoot": "lib"
5552
},
5653
"devDependencies": {

src/forwardRef.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@ export default function forwardRef(
1010
} = {},
1111
) {
1212
const render = (props, ref) => renderFn(props, ref);
13-
render.displayName = displayName;
1413

15-
if (React.forwardRef || !allowFallback)
16-
return Object.assign(React.forwardRef(render), {
14+
return Object.assign(
15+
React.forwardRef || !allowFallback
16+
? React.forwardRef(render)
17+
: props => render(props, null),
18+
{
19+
displayName,
1720
propTypes,
1821
defaultProps,
19-
});
20-
21-
return Object.assign(props => render(props, null), {
22-
displayName,
23-
propTypes,
24-
defaultProps,
25-
});
22+
},
23+
);
2624
}

test/forwardRef.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ describe('forwardRef', () => {
1010
return null;
1111
});
1212

13-
expect(mount(<Foo />).find('ForwardRef(Foo)')).toHaveLength(1);
13+
expect(mount(<Foo />).find('Foo')).toHaveLength(1);
1414
});
1515
});

test/mapContextToProps.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ describe('mapContextToProps', () => {
122122
'div',
123123
);
124124

125-
expect(Mapper.render.displayName).toEqual('ContextTransform(div)');
125+
expect(Mapper.displayName).toEqual('ContextTransform(div)');
126126
});
127127

128128
it('should add custom displayName', () => {
@@ -135,7 +135,7 @@ describe('mapContextToProps', () => {
135135
'div',
136136
);
137137

138-
expect(Mapper.render.displayName).toEqual('WithIntl');
138+
expect(Mapper.displayName).toEqual('WithIntl');
139139
});
140140

141141
it('should not warn about Contexts', () => {

types/mapContextToProps.d.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
declare module 'react-context-toolbox/mapContextToProps' {
44
import * as React from 'react';
55

6-
type ElementType = React.ComponentType<any> | keyof JSX.IntrinsicElements;
7-
86
type Omit<T, U> = Pick<T, Exclude<keyof T, keyof U>>;
97

108
type GetProps<C> = C extends React.ComponentType<infer P> ? P : never;
119

12-
interface ContextInjectedComponent<TComponent, TInjectedProps, TExtraProps>
10+
export interface ContextInjectedComponent<
11+
TComponent,
12+
TInjectedProps,
13+
TExtraProps
14+
>
1315
extends React.ForwardRefExoticComponent<
1416
Omit<GetProps<TComponent>, TInjectedProps> & TExtraProps
1517
> {}

types/tslint.json

+2-18
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,7 @@
11
{
22
"extends": "dtslint/dtslint.json",
33
"rules": {
4-
"array-type": false,
5-
"semicolon": false,
6-
"whitespace": [
7-
true,
8-
"check-branch",
9-
"check-decl",
10-
"check-operator",
11-
"check-module",
12-
"check-rest-spread",
13-
"check-type",
14-
"check-typecast",
15-
"check-type-operator",
16-
"check-preblock"
17-
],
18-
19-
"no-unnecessary-generics": false,
20-
"no-unnecessary-class": false,
21-
"no-single-declare-module": false
4+
"no-single-declare-module": false,
5+
"strict-export-declare-modifiers": false
226
}
237
}

0 commit comments

Comments
 (0)