Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button with Icon component

Tags:

reactjs

I have a <Button /> component and an <Icon/> component.

I try to implement a button with an icon.

The Button.jsx story:

import React from "react";
import { storiesOf } from "@storybook/react";
import Button from "../components/Button";
import Icon from "../components/Icon/Index";
import { iconTypes } from "../components/Icon/Index";

storiesOf("Button/Primary", module)
    .add("With icon", () => (
        <Button><Icon type={iconTypes.arrowRight}/></Button>
    ))

That works fine but I would like the api for a Button with an icon to be-

<Button icon={icons.arrow}>Click Me</Button>

How can I achieve that?

The Icon.jsx story:

import React from "react";
import { storiesOf } from "@storybook/react";
import Icon from "../components/Icon/Index";
import { iconTypes } from "../components/Icon/Index";    

storiesOf("Icon", module)
    .add("Arrow Right", () => (
        <Icon type={iconTypes.arrowRight}>
        </Icon>
    ))
    .add("Arrow Left", () => (
        <Icon type={iconTypes.arrowLeft}>
        </Icon>
    ));

The <Button /> component:

import React from 'react';
import { css, cx } from 'emotion';

import colors from '../styles/colors';

export default function Button({
  children,
  ...props
}) {
    const mergedStyles = cx(
  // css
  );
  // other css stuff
  return (
    <button {...props} disabled={disabled} className={mergedStyles}>
      {children}
    </button>
  );

And <Icon /> component:

import React from "react";
import { css } from 'emotion';

import ArrowRight from "./arrow-right2.svg";
import ArrowLeft from "./arrow-left2.svg";

export const iconTypes = {
    arrowRight: 'ARROW_RIGHT',
    arrowLeft: 'ARROW_LEFT',
}

const iconSrc = {
    ARROW_RIGHT: ArrowRight,
    ARROW_LEFT: ArrowLeft,
}


const circleStyles = css({
    width: 24,
    height: 24,
    borderRadius: "50%",
    backgroundColor: "#f7f7f9",
    display: "flex",
    justifyContent: "center"
});


export default function Icon({ type }) {
    return (
         <div className={circleStyles}>
            <img src={iconSrc[type]} />
        </div>
    )
};

Any help would be appreciated.

like image 973
user3378165 Avatar asked Oct 08 '18 07:10

user3378165


People also ask

How do I add a button with an icon in React?

To use an icon as a button the IconButton component is used which has a property named icon to specify the icon we want to use. React Suite Button Icon Components: IconButton: This component is used when we want to use an icon as a button, It has an icon property that is used to specify the icon of the Button.

Can an icon be a button?

An icon button is an icon that triggers some sort of action on the page. More accurately, technically speaking, an icon button is a button that contains an icon and no (visible) accompanying text. These buttons can be found in the majority of app and user interfaces today.

How do I add an icon to a material button?

Add icons to the start, center, or end of this button using the app:icon , app:iconPadding , app:iconTint , app:iconTintMode and app:iconGravity attributes. If a start-aligned icon is added to this button, please use a style like one of the ". Icon" styles specified in the default MaterialButton styles.

What is a button component?

The Button component lets you add standard HTML buttons to your project. Additional components you may be interested in: Other UI components. The Image Button component, which lets you set different images depending on the button state (up, hover over, down)


2 Answers

    import React from 'react';
    import {css, cx} from 'emotion';
    import colors from '../styles/colors';

    //import your ICON component & make sure your path is right
    import Icon from "./../Icon";

    export default function Button({
      children,
      disabled,
      icon,
      ...props
    }) {

      const mergedStyles = cx(//your css);

      return (
          <button {...props} disabled={disabled} className={mergedStyles}>

            // If icon prop is provided then render ICON component
            {icon && <Icon type={icon}/>}

            //Other children
            {children}

          </button>
       );
    }
like image 197
Juned Lanja Avatar answered Nov 15 '22 10:11

Juned Lanja


in render of Button, you can do something like that :

Button.js:

render(){
    const { icon } = this.props
    return(
        <Button>
            {icon && <Icon type={icon}/>}
        <Button>
    )
}
like image 44
Deve Avatar answered Nov 15 '22 09:11

Deve