Skip to content

Commit

Permalink
fix: wrapIcon has type error on Ref (#648)
Browse files Browse the repository at this point in the history
* fix

* do change without prettier

* more type fix
  • Loading branch information
YuanboXue-Amber authored Sep 20, 2023
1 parent 9b8ea7a commit 7b0f70d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
19 changes: 12 additions & 7 deletions packages/react-icons/src/utils/FluentIconsProps.types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import * as React from "react";

export type FluentIconsProps<TBaseAttributes extends (React.SVGAttributes<SVGElement> | React.HTMLAttributes<HTMLElement>) = React.SVGAttributes<SVGElement>>
= TBaseAttributes & React.RefAttributes<HTMLElement> & {
primaryFill?: string
className?: string
filled?: boolean
title?: string
}
export type FluentIconsProps<
TBaseAttributes extends
| React.SVGAttributes<SVGElement>
| React.HTMLAttributes<HTMLElement> = React.SVGAttributes<SVGElement>,
TRefType extends HTMLElement | SVGSVGElement = SVGSVGElement
> = TBaseAttributes &
React.RefAttributes<TRefType> & {
primaryFill?: string;
className?: string;
filled?: boolean;
title?: string;
};
6 changes: 3 additions & 3 deletions packages/react-icons/src/utils/fonts/createFluentFontIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ export type CreateFluentFontIconOptions = {
flipInRtl?: boolean;
}

export function createFluentFontIcon(displayName: string, codepoint: string, font: FontFile, fontSize?: number, options?: CreateFluentFontIconOptions): React.FC<FluentIconsProps<React.HTMLAttributes<HTMLElement>>> & { codepoint: string} {
const Component: React.FC<FluentIconsProps<React.HTMLAttributes<HTMLElement>>> & { codepoint: string} = (props) => {
export function createFluentFontIcon(displayName: string, codepoint: string, font: FontFile, fontSize?: number, options?: CreateFluentFontIconOptions): React.FC<FluentIconsProps<React.HTMLAttributes<HTMLElement>, HTMLElement>> & { codepoint: string} {
const Component: React.FC<FluentIconsProps<React.HTMLAttributes<HTMLElement>, HTMLElement>> & { codepoint: string} = (props) => {
useStaticStyles();
const styles = useRootStyles();
const className = mergeClasses(styles.root, styles[font], props.className);
const state = useIconState<React.HTMLAttributes<HTMLElement>>({...props, className}, { flipInRtl: options?.flipInRtl });
const state = useIconState<React.HTMLAttributes<HTMLElement>, HTMLElement>({...props, className}, { flipInRtl: options?.flipInRtl });


// We want to keep the same API surface as the SVG icons, so translate `primaryFill` to `color`
Expand Down
12 changes: 10 additions & 2 deletions packages/react-icons/src/utils/useIconState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ export type UseIconStateOptions = {
flipInRtl?: boolean;
}

export const useIconState = <TBaseAttributes extends (React.SVGAttributes<SVGElement> | React.HTMLAttributes<HTMLElement>) = React.SVGAttributes<SVGElement>>(props: FluentIconsProps<TBaseAttributes>, options?: UseIconStateOptions): Omit<FluentIconsProps<TBaseAttributes>, 'primaryFill'> => {
export const useIconState = <
TBaseAttributes extends
| React.SVGAttributes<SVGElement>
| React.HTMLAttributes<HTMLElement> = React.SVGAttributes<SVGElement>,
TRefType extends HTMLElement | SVGSVGElement = SVGSVGElement,
>(
props: FluentIconsProps<TBaseAttributes, TRefType>,
options?: UseIconStateOptions,
): Omit<FluentIconsProps<TBaseAttributes, TRefType>, 'primaryFill'> => {
const { title, primaryFill = "currentColor", ...rest } = props;
const state = {
...rest,
title: undefined,
fill: primaryFill
} as Omit<FluentIconsProps<TBaseAttributes>, 'primaryFill'>;
} as Omit<FluentIconsProps<TBaseAttributes, TRefType>, 'primaryFill'>;

const styles = useRootStyles();
const iconContext = useIconContext();
Expand Down
3 changes: 2 additions & 1 deletion packages/react-icons/src/utils/wrapIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useIconState } from "./useIconState";
import { CreateFluentIconOptions, FluentIcon } from "./createFluentIcon";

const wrapIcon = (Icon: (iconProps: FluentIconsProps) => JSX.Element, displayName?: string, options?: CreateFluentIconOptions) => {
const WrappedIcon = React.forwardRef((props: FluentIconsProps, ref: React.Ref<HTMLElement>) => {
const WrappedIcon = React.forwardRef((props: FluentIconsProps, ref: FluentIconsProps["ref"]) => {
const state = {
...useIconState(props, { flipInRtl: options?.flipInRtl }),
ref
Expand All @@ -14,5 +14,6 @@ const wrapIcon = (Icon: (iconProps: FluentIconsProps) => JSX.Element, displayNam
WrappedIcon.displayName = displayName;
return WrappedIcon;
}


export default wrapIcon;

0 comments on commit 7b0f70d

Please sign in to comment.