Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DevTools] Improve HOC search UX #18802

Merged
merged 11 commits into from
May 15, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -626,4 +626,7 @@ exports[`Store should show the right display names for special component types 1
<MyComponent> [ForwardRef]
▾ <Suspense>
<MyComponent5>
<Baz> [withFoo][withBar]
<Baz> [Memo][withFoo][withBar]
<Baz> [ForwardRef][withFoo][withBar]
`;
13 changes: 13 additions & 0 deletions packages/react-devtools-shared/src/__tests__/store-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,16 @@ describe('Store', () => {
const MyComponent5 = (props, ref) => null;
const LazyComponent = React.lazy(() => fakeImport(MyComponent5));

const FakeHigherOrderComponent = () => null;
FakeHigherOrderComponent.displayName = 'withFoo(withBar(Baz))';

const MemoizedFakeHigherOrderComponent = React.memo(
FakeHigherOrderComponent,
);
const ForwardRefFakeHigherOrderComponent = React.forwardRef(
FakeHigherOrderComponent,
);

const App = () => (
<React.Fragment>
<MyComponent />
Expand All @@ -882,6 +892,9 @@ describe('Store', () => {
<React.Suspense fallback="Loading...">
<LazyComponent />
</React.Suspense>
<FakeHigherOrderComponent />
<MemoizedFakeHigherOrderComponent />
<ForwardRefFakeHigherOrderComponent />
</React.Fragment>
);

Expand Down
18 changes: 0 additions & 18 deletions packages/react-devtools-shared/src/devtools/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
* @flow
*/

import {
ElementTypeForwardRef,
ElementTypeMemo,
} from 'react-devtools-shared/src/types';

import type {Element} from './views/Components/types';
import type Store from './store';

Expand All @@ -30,19 +25,6 @@ export function printElement(element: Element, includeWeight: boolean = false) {
if (element.hocDisplayNames !== null) {
hocDisplayNames = [...element.hocDisplayNames];
}
if (element.type === ElementTypeMemo) {
if (hocDisplayNames === null) {
hocDisplayNames = ['Memo'];
} else {
hocDisplayNames.push('Memo');
}
} else if (element.type === ElementTypeForwardRef) {
if (hocDisplayNames === null) {
hocDisplayNames = ['ForwardRef'];
} else {
hocDisplayNames.push('ForwardRef');
}
}
bvaughn marked this conversation as resolved.
Show resolved Hide resolved

const hocs =
hocDisplayNames === null ? '' : ` [${hocDisplayNames.join('][')}]`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@

import * as React from 'react';
import {Fragment} from 'react';
import {
ElementTypeMemo,
ElementTypeForwardRef,
} from 'react-devtools-shared/src/types';
import styles from './Badge.css';

import type {ElementType} from 'react-devtools-shared/src/types';
Expand All @@ -21,35 +17,24 @@ type Props = {|
className?: string,
hocDisplayNames: Array<string> | null,
type: ElementType,
children: React$Node,
|};

export default function Badge({className, hocDisplayNames, type}: Props) {
let hocDisplayName = null;
export default function Badge({
className,
hocDisplayNames,
type,
children,
}: Props) {
let totalBadgeCount = 0;
let typeLabel = null;

if (hocDisplayNames !== null) {
hocDisplayName = hocDisplayNames[0];
totalBadgeCount += hocDisplayNames.length;
}

if (type === ElementTypeMemo) {
typeLabel = 'Memo';
totalBadgeCount++;
} else if (type === ElementTypeForwardRef) {
typeLabel = 'ForwardRef';
totalBadgeCount++;
}

if (hocDisplayNames === null && typeLabel === null) {
return null;
}

return (
<Fragment>
<div className={`${styles.Badge} ${className || ''}`}>
{hocDisplayName || typeLabel}
</div>
<div className={`${styles.Badge} ${className || ''}`}>{children}</div>
{totalBadgeCount > 1 && (
<div className={styles.ExtraLabel}>+{totalBadgeCount - 1}</div>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,17 @@ export default function ElementView({data, index, style}: Props) {
"
</Fragment>
)}
<Badge
className={styles.Badge}
hocDisplayNames={hocDisplayNames}
type={type}
/>
{hocDisplayNames !== null && hocDisplayNames.length > 0 ? (
<Badge
className={styles.Badge}
hocDisplayNames={hocDisplayNames}
type={type}>
<DisplayName
displayName={hocDisplayNames[0]}
id={((id: any): number)}
/>
</Badge>
) : null}
</div>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
*/

import * as React from 'react';
import {
ElementTypeForwardRef,
ElementTypeMemo,
} from 'react-devtools-shared/src/types';
import styles from './HocBadges.css';

import type {Element} from './types';
Expand All @@ -21,22 +17,14 @@ type Props = {|
|};

export default function HocBadges({element}: Props) {
const {hocDisplayNames, type} = ((element: any): Element);
const {hocDisplayNames} = ((element: any): Element);

let typeBadge = null;
if (type === ElementTypeMemo) {
typeBadge = 'Memo';
} else if (type === ElementTypeForwardRef) {
typeBadge = 'ForwardRef';
}

if (hocDisplayNames === null && typeBadge === null) {
if (hocDisplayNames === null) {
return null;
}

return (
<div className={styles.HocBadges}>
{typeBadge !== null && <div className={styles.Badge}>{typeBadge}</div>}
{hocDisplayNames !== null &&
hocDisplayNames.map(hocDisplayName => (
<div key={hocDisplayName} className={styles.Badge}>
Expand Down
14 changes: 14 additions & 0 deletions packages/react-devtools-shared/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,20 @@ export function separateDisplayNameAndHOCs(
break;
}

if (type === ElementTypeMemo) {
if (hocDisplayNames === null) {
hocDisplayNames = ['Memo'];
} else {
hocDisplayNames.unshift('Memo');
}
} else if (type === ElementTypeForwardRef) {
if (hocDisplayNames === null) {
hocDisplayNames = ['ForwardRef'];
} else {
hocDisplayNames.unshift('ForwardRef');
}
}

return [displayName, hocDisplayNames];
}

Expand Down