-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(React-components): For generation of icons, use factory patt…
…ern naming (#4883) * Rename icon factory * Update DeleteDomainObjectCommand.ts * Fixes * Update IconFactory.tsx
- Loading branch information
1 parent
816feaf
commit 03ad7e4
Showing
10 changed files
with
47 additions
and
38 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
33 changes: 33 additions & 0 deletions
33
react-components/src/components/Architecture/Factories/IconFactory.tsx
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/*! | ||
* Copyright 2024 Cognite AS | ||
*/ | ||
|
||
import { type JSX, type FC } from 'react'; | ||
import { type IconName } from '../../../architecture/base/utilities/IconName'; | ||
import { type IconProps } from '@cognite/cogs.js'; | ||
import { DefaultIcons } from './DefaultIcons'; | ||
|
||
export type IconType = FC<IconProps>; | ||
|
||
const DefaultIcon = (_iconProps: IconProps): JSX.Element => <></>; | ||
|
||
export class IconFactory { | ||
private static readonly _icons = new Map<IconName, IconType>(DefaultIcons); | ||
public static install(iconName: IconName, iconType: IconType): void { | ||
IconFactory._icons.set(iconName, iconType); | ||
} | ||
|
||
public static getIcon(iconName: IconName): IconType { | ||
if (iconName === undefined) { | ||
return DefaultIcon; | ||
} | ||
return IconFactory._icons.get(iconName) ?? DefaultIcon; | ||
} | ||
} | ||
|
||
type IconComponentProps = IconProps & { iconName: IconName }; | ||
|
||
export const IconComponent = ({ iconName, ...rest }: IconComponentProps): JSX.Element => { | ||
const Icon = IconFactory.getIcon(iconName); | ||
return <Icon {...rest} />; | ||
}; |
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