This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/cor 959/dynamic icons (#4361)
* Get an icon dynamically * feat: make dynamic icon work including typings * fix: code cleanup * feat: extend type info in icon build * feat: add eye icon Co-authored-by: VWSCoronaDashboard18 <[email protected]> Co-authored-by: VWSCoronaDashboard24 <[email protected]>
- Loading branch information
1 parent
0b05b3a
commit a5c39fd
Showing
5 changed files
with
61 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import * as allIcons from '@corona-dashboard/icons'; | ||
import { iconName2filename, IconProps } from '@corona-dashboard/icons'; | ||
import React from 'react'; | ||
import { isDefined } from 'ts-is-present'; | ||
import { assert } from '~/utils/assert'; | ||
|
||
type IconName = keyof typeof iconName2filename; | ||
type IconCollection = Record<IconName, React.ComponentType>; | ||
|
||
interface DynamicIconProps extends IconProps { | ||
name: IconName; | ||
} | ||
|
||
/** | ||
* Returns an icon component by the name of the icon | ||
* Throws when requested component was not found | ||
*/ | ||
function getIconByName(name: IconName) { | ||
const icons: IconCollection = allIcons; | ||
const DynamicIcon = icons[name]; | ||
|
||
assert( | ||
isDefined(DynamicIcon), | ||
`[${getIconByName.name}] Icon with name "${name}" does not exist` | ||
); | ||
|
||
return DynamicIcon; | ||
} | ||
|
||
/** | ||
* Renders an icon component by the name of the icon | ||
*/ | ||
function DynamicIcon({ name, ...otherProps }: DynamicIconProps) { | ||
const Icon = getIconByName(name); | ||
return <Icon {...otherProps} />; | ||
} | ||
|
||
export default DynamicIcon; |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.