Skip to content

Commit a579f5e

Browse files
Resolve lib Circular Dependency (#1957)
* resolves circular dependency in lib * export isComponentType
1 parent bbc812a commit a579f5e

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

packages/lib/src/consoleOnce.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import once from 'lodash/once';
2+
3+
export const consoleOnce = {
4+
error: once(console.error),
5+
warn: once(console.warn),
6+
log: once(console.log),
7+
};

packages/lib/src/index.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import isObject from 'lodash/isObject';
2-
import once from 'lodash/once';
31
import * as typeIs from './typeIs';
42
import createUniqueClassName from './createUniqueClassName';
53
import getNodeTextContent from './getNodeTextContent';
64
import DarkModeProps, { Theme } from './DarkModeProps';
75
import getTheme from './getTheme';
86
import { allEqual } from './allEqual';
9-
export { validateChildren } from './validateChildren';
7+
export { validateChildren, isComponentType } from './validateChildren';
108
export { createSyntheticEvent } from './createSyntheticEvent';
9+
export { consoleOnce } from './consoleOnce';
1110

1211
export {
1312
type ExtendedComponentProps,
@@ -87,23 +86,6 @@ export type OneOf<T1, T2> =
8786
| (T1 & Partial<Record<Exclude<keyof T2, keyof T1>, never>>)
8887
| (T2 & Partial<Record<Exclude<keyof T1, keyof T2>, never>>);
8988

90-
/** Helper type to check if element is a specific React Component */
91-
export function isComponentType<
92-
T extends React.ReactElement = React.ReactElement,
93-
>(element: React.ReactNode, displayName: string): element is T {
94-
return (
95-
element != null &&
96-
typeof element === 'object' &&
97-
'type' in element &&
98-
((element.type as any).displayName === displayName ||
99-
// TODO: temp solution; Components using InferredPolymorphic have a displayName inside render.
100-
// https://jira.mongodb.org/browse/LG-3232
101-
(isObject(element.type as any) &&
102-
'render' in (element.type as any) &&
103-
(element.type as any).render?.displayName === displayName))
104-
);
105-
}
106-
10789
/**
10890
* Utility for making it easier to couple a React Component to a css selector.
10991
* Useful when writing css selectors that rely on interactivity, i.e. :hover.
@@ -220,9 +202,3 @@ export type RecursivePartial<T> = {
220202
export function enforceExhaustive(value: never): never {
221203
throw Error(`Received unhandled value: ${value}`);
222204
}
223-
224-
export const consoleOnce = {
225-
error: once(console.error),
226-
warn: once(console.warn),
227-
log: once(console.log),
228-
};

packages/lib/src/validateChildren.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
1+
import isObject from 'lodash/isObject';
12
import isUndefined from 'lodash/isUndefined';
23
import React, { ReactElement } from 'react';
3-
import { consoleOnce, isComponentType } from '.';
4+
import { consoleOnce } from '.';
5+
6+
/** Helper type to check if element is a specific React Component */
7+
export function isComponentType<
8+
T extends React.ReactElement = React.ReactElement,
9+
>(element: React.ReactNode, displayName: string): element is T {
10+
return (
11+
element != null &&
12+
typeof element === 'object' &&
13+
'type' in element &&
14+
((element.type as any).displayName === displayName ||
15+
// TODO: temp solution; Components using InferredPolymorphic have a displayName inside render.
16+
// https://jira.mongodb.org/browse/LG-3232
17+
(isObject(element.type as any) &&
18+
'render' in (element.type as any) &&
19+
(element.type as any).render?.displayName === displayName))
20+
);
21+
}
422

523
/**
624
* Filters children down to a restricted set of component types.

0 commit comments

Comments
 (0)