From a579f5ef6f1888318e39719480e917574ac7eeff Mon Sep 17 00:00:00 2001 From: Adam Thompson <2414030+TheSonOfThomp@users.noreply.github.com> Date: Fri, 25 Aug 2023 10:56:21 -0400 Subject: [PATCH] Resolve `lib` Circular Dependency (#1957) * resolves circular dependency in lib * export isComponentType --- packages/lib/src/consoleOnce.ts | 7 +++++++ packages/lib/src/index.ts | 28 ++------------------------- packages/lib/src/validateChildren.tsx | 20 ++++++++++++++++++- 3 files changed, 28 insertions(+), 27 deletions(-) create mode 100644 packages/lib/src/consoleOnce.ts diff --git a/packages/lib/src/consoleOnce.ts b/packages/lib/src/consoleOnce.ts new file mode 100644 index 0000000000..0157a30216 --- /dev/null +++ b/packages/lib/src/consoleOnce.ts @@ -0,0 +1,7 @@ +import once from 'lodash/once'; + +export const consoleOnce = { + error: once(console.error), + warn: once(console.warn), + log: once(console.log), +}; diff --git a/packages/lib/src/index.ts b/packages/lib/src/index.ts index 5efc1a09b8..0f5818fb75 100644 --- a/packages/lib/src/index.ts +++ b/packages/lib/src/index.ts @@ -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, @@ -87,23 +86,6 @@ export type OneOf = | (T1 & Partial, never>>) | (T2 & Partial, 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. @@ -220,9 +202,3 @@ export type RecursivePartial = { 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), -}; diff --git a/packages/lib/src/validateChildren.tsx b/packages/lib/src/validateChildren.tsx index 2ec4a592ff..aac74b0cc8 100644 --- a/packages/lib/src/validateChildren.tsx +++ b/packages/lib/src/validateChildren.tsx @@ -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.