Skip to content

Commit

Permalink
Merge pull request #185 from seznam/select-display-name
Browse files Browse the repository at this point in the history
fix: displayName of components wrapped with select
  • Loading branch information
mjancarik authored Jan 9, 2025
2 parents 9f95f11 + 7e0c93b commit af6a555
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-cups-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ima/plugin-select": patch
---

Fix displayName value for components wrapped with `select` HOC. This should resolve the issue with `Component.displayName` being `withContext(undefined)`.
33 changes: 32 additions & 1 deletion packages/plugin-select/src/select/__tests__/SelectSpec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getContextValue, renderWithContext } from '@ima/testing-library';
import { PureComponent, createRef } from 'react';
import { PureComponent, createRef, memo, forwardRef } from 'react';

import forwardedSelect, {
createStateSelector,
Expand Down Expand Up @@ -159,6 +159,37 @@ describe('plugin-select:', () => {
expect(container.firstChild).toMatchSnapshot();
});

it('should set displayName for class component', async () => {
const EnhancedComponent = select(...selectorMethods)(Component);

expect(EnhancedComponent.displayName).toBe('withContext(Component)');
});

it('should set displayName for functional component', async () => {
const FunctionalComponent = () => {
return <div />;
};
const EnhancedComponent = select(...selectorMethods)(FunctionalComponent);

expect(EnhancedComponent.displayName).toBe(
'withContext(FunctionalComponent)'
);
});

it('should set displayName for memoized component', async () => {
const MemoComponent = memo(Component);
const EnhancedComponent = select(...selectorMethods)(MemoComponent);

expect(EnhancedComponent.displayName).toBe('withContext(Component)');
});

it('should set displayName for forwarded component', async () => {
const ForwardedComponent = forwardRef(Component);
const EnhancedComponent = select(...selectorMethods)(ForwardedComponent);

expect(EnhancedComponent.displayName).toBe('withContext(Component)');
});

it('should render component with extraProps modifies by ownProps', async () => {
let EnhancedComponent = select(
...selectorMethods,
Expand Down
19 changes: 17 additions & 2 deletions packages/plugin-select/src/select/select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function select(...selectors) {
return <Component {...restProps} {...state} ref={forwardedRef} />;
};

const componentName = Component.displayName || Component.name;
const componentName = getReactComponentName(Component);
WithContext.displayName = `withContext(${componentName})`;
hoistStaticMethod(WithContext, Component);

Expand Down Expand Up @@ -120,7 +120,7 @@ export default function forwardedSelect(...selectors) {
return <SelectState {...props} forwardedRef={ref} />;
};

const name = Component.displayName || Component.name;
const name = getReactComponentName(Component);
forwardRef.displayName = `select(${name})`;

return hoistStaticMethod(reactForwardRef(forwardRef), Component);
Expand Down Expand Up @@ -179,3 +179,18 @@ export function createStateSelector(...selectors) {

return createSelector(derivedState, passStateOnChange);
}

/**
* Finds display name from the provided component.
* @param {Function|Object} Component
* @returns {string}
*/
function getReactComponentName(Component) {
return Component.displayName ||
Component.name ||
Component.type?.displayName || // React.memo
Component.type?.name ||
Component.render?.displayName || // React.forwardRef
Component.render?.name ||
'Component';
}

0 comments on commit af6a555

Please sign in to comment.