Skip to content

Commit

Permalink
Resolve lib Circular Dependency (#1957)
Browse files Browse the repository at this point in the history
* resolves circular dependency in lib

* export isComponentType
  • Loading branch information
TheSonOfThomp authored Aug 25, 2023
1 parent bbc812a commit a579f5e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
7 changes: 7 additions & 0 deletions packages/lib/src/consoleOnce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import once from 'lodash/once';

export const consoleOnce = {
error: once(console.error),
warn: once(console.warn),
log: once(console.log),
};
28 changes: 2 additions & 26 deletions packages/lib/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import isObject from 'lodash/isObject';
import once from 'lodash/once';
import * as typeIs from './typeIs';
import createUniqueClassName from './createUniqueClassName';
import getNodeTextContent from './getNodeTextContent';
import DarkModeProps, { Theme } from './DarkModeProps';
import getTheme from './getTheme';
import { allEqual } from './allEqual';
export { validateChildren } from './validateChildren';
export { validateChildren, isComponentType } from './validateChildren';
export { createSyntheticEvent } from './createSyntheticEvent';
export { consoleOnce } from './consoleOnce';

export {
type ExtendedComponentProps,
Expand Down Expand Up @@ -87,23 +86,6 @@ export type OneOf<T1, T2> =
| (T1 & Partial<Record<Exclude<keyof T2, keyof T1>, never>>)
| (T2 & Partial<Record<Exclude<keyof T1, keyof T2>, never>>);

/** Helper type to check if element is a specific React Component */
export function isComponentType<
T extends React.ReactElement = React.ReactElement,
>(element: React.ReactNode, displayName: string): element is T {
return (
element != null &&
typeof element === 'object' &&
'type' in element &&
((element.type as any).displayName === displayName ||
// TODO: temp solution; Components using InferredPolymorphic have a displayName inside render.
// https://jira.mongodb.org/browse/LG-3232
(isObject(element.type as any) &&
'render' in (element.type as any) &&
(element.type as any).render?.displayName === displayName))
);
}

/**
* Utility for making it easier to couple a React Component to a css selector.
* Useful when writing css selectors that rely on interactivity, i.e. :hover.
Expand Down Expand Up @@ -220,9 +202,3 @@ export type RecursivePartial<T> = {
export function enforceExhaustive(value: never): never {
throw Error(`Received unhandled value: ${value}`);
}

export const consoleOnce = {
error: once(console.error),
warn: once(console.warn),
log: once(console.log),
};
20 changes: 19 additions & 1 deletion packages/lib/src/validateChildren.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import isObject from 'lodash/isObject';
import isUndefined from 'lodash/isUndefined';
import React, { ReactElement } from 'react';
import { consoleOnce, isComponentType } from '.';
import { consoleOnce } from '.';

/** Helper type to check if element is a specific React Component */
export function isComponentType<
T extends React.ReactElement = React.ReactElement,
>(element: React.ReactNode, displayName: string): element is T {
return (
element != null &&
typeof element === 'object' &&
'type' in element &&
((element.type as any).displayName === displayName ||
// TODO: temp solution; Components using InferredPolymorphic have a displayName inside render.
// https://jira.mongodb.org/browse/LG-3232
(isObject(element.type as any) &&
'render' in (element.type as any) &&
(element.type as any).render?.displayName === displayName))
);
}

/**
* Filters children down to a restricted set of component types.
Expand Down

0 comments on commit a579f5e

Please sign in to comment.