Dynamic icons not possible? #51
-
in the old import Icon, { IconProps } from "phosphor-react";
import React, {
FC,
ForwardRefExoticComponent,
InputHTMLAttributes,
RefAttributes,
} from "react";
interface Props {
leadingIcon?: keyof typeof Icon;
}
export const Input: FC<Props> = ({ leadingIcon }) => (
<>
{leadingIcon && (
<div>
{(
Icon[leadingIcon] as ForwardRefExoticComponent<
IconProps & RefAttributes<SVGSVGElement>
>
)?.({})}
</div>
)}
<input {...props} />
</>
); but with |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
You can absolutely render dynamic icons with the current version, and it should behave the same. Here is your example working with We never exposed a import Icon from "phosphor-react"; is not inherently valid code in either version. What is probably happening is the your bundler or compiler supports synthetic default imports (TypeScript exposes an option for this, A more compatible way to do this is to simply Also, note that we do provide an import * as Phosphor from "@phosphor-icons/react";
...
Phosphor[leadingIcon] as Phosphor.Icon |
Beta Was this translation helpful? Give feedback.
Got the storybook running. Only change that's needed is
import * as Phosphor from "@phosphor-icons/react";
. As I said, the lib does not have adefault
export, only named exports. You want to treat it like it does, so you need to use theimport * as
syntax to construct one.