generated from abelflopes/lerna-monorepo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: component identification with display name
- Loading branch information
1 parent
b5b5031
commit b182afb
Showing
6 changed files
with
44 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,26 @@ | ||
import React from "react"; | ||
|
||
export const DISPLAY_NAME_ATTRIBUTE = "_react-ck-display-name"; | ||
|
||
export const DISPLAY_NAMES = { | ||
SELECT_OPTION: "select-option", | ||
LAYER: "layer", | ||
ICON: "icon", | ||
}; | ||
|
||
export function getDisplayName(el: React.ReactNode): string | undefined { | ||
if (!React.isValidElement(el)) return undefined; | ||
else if (typeof el.type === "string") return el.type; | ||
else if (typeof el.type !== "string" && "name" in el.type) return el.type.name; | ||
return undefined; | ||
|
||
let displayName: string | undefined = undefined; | ||
|
||
if (typeof el.type === "string") { | ||
displayName = el.type; | ||
} else if (typeof el.type === "function") { | ||
const functionProperties = Object.fromEntries(Object.entries(el.type)); | ||
const r: unknown = functionProperties[DISPLAY_NAME_ATTRIBUTE]; | ||
if (typeof r !== "string" && r !== undefined) throw new Error("Unknown component name type"); | ||
displayName = r; | ||
} | ||
|
||
return displayName; | ||
} |