0

I'm creating a component for each svg icons. For that i'm providing the following properties for it,

export interface IconProperties{
  width?: string | number;
  height?: string | number;
  size?: "small" | "medium" | "large";
  onClick?: React.MouseEventHandler<SVGElement>;
  disabled?: boolean;
}

Now to create Component for each icon, I'm doing

import * as React from "react";
import Attachment from "../../svg/attach.svg";
const AttachmentIcon = (props: IconProperties) => {
  return (
    <div>
      <Attachment
        width={props.width}
        height={props.height}
        onClick={props.onClick}
        style={
         {props.size}
        }
      />
    </div>
  );
};

export default AttachmentIcon;

User will call icon component in the following way :

import * as React from "react";
import AttachmentIcon from "./iconComponents/AttachmentIcon";

const onClickOperation = (): void => { 
  alert("Icon Clicked!");
};

const DemoApp = () => {
  return (
    <div>
      <AttachmentIcon
        width="60px"
        height="60px"
        onClick={onClickOperation}
        disabled={true}
        size={"small"}
      />
    </div>
  );
};

export default DemoApp;

Here my properties is same for all the icons and I have to keep on repeating the same lines of code for each icon to make it as a component. How do I avoid duplicating same code for every icon.

4
  • Looks like Attachment is already a component. Why do you need that wrapper? Commented May 4, 2020 at 6:56
  • @Aleksey L Like Attachment, I have 50+ icons to be converted into Components Commented May 4, 2020 at 6:57
  • Of course it is possible to create a factory which will do all the boilerplate. The question is why can't you use Attachment directly, what's the point of this wrapper? Commented May 4, 2020 at 7:46
  • I can use Attachment directly, but I have to add custom properties to it (like size, onClick etc). That's the reason I'm trying out to create a wrapper on top of it Commented May 4, 2020 at 8:02

2 Answers 2

1

You can create a factory method to generate Component.

import * as React from "react";
import Attachment from "../../svg/attach.svg";
import Email from "../../svg/email.svg";

const createSvgIcon = Component: React.Component<any> => (props: IconProperties) => {
  return (
    <div>
      <Component
        width={props.width}
        height={props.height}
        onClick={props.onClick}
        style={
         {props.size}
        }
      />
    </div>
  );
};

export const EmailIcon = createSvgIcon(Email)

const AttachmentIcon = createSvgIcon(Attachment)
export default AttachmentIcon;
Sign up to request clarification or add additional context in comments.

Comments

0

in AttachmentIcon.module.css

 .iconStyle{
            width: 60px;
            height: 60px;
            disabled:true;
            size:small;
    }

in Js file

import classes from './AttachmentIcon.module.css';
<AttachmentIcon className={classes.iconStyle} onClick={onClickOperation} />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.