reactjs - 带 Icon 组件的按钮

标签 reactjs

我有一个<Button />组件和<Icon/>组件。

我尝试实现一个带有图标的按钮。

Button.jsx故事:

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>
    ))

效果很好,但我希望带有图标的 Button 的 api 是-

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

我怎样才能实现这一目标?

Icon.jsx故事:

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>
    ));

<Button />组件:

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>
  );

<Icon />组件:

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>
    )
};

如有任何帮助,我们将不胜感激。

最佳答案

    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>
       );
    }

关于reactjs - 带 Icon 组件的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52697730/

相关文章:

javascript - 如何从 Node js项目中的文件夹中递归导出所有内容?

javascript - 在 React 中映射时出现意外标记

javascript - 意外的 token React-Redux

javascript - 动态添加按钮到div ReactJS?

javascript - 未捕获的类型错误 : Cannot read property 'props' of null in React

javascript - 在我的 HTML 代码中看不到 div 内的元素?

javascript - 处理 "cannot read property of null"错误

reactjs - 如何更改 Material ui 中选项卡指示器的高度

validation - 使用 redux-form 我在输入第一个字符后失去焦点

reactjs - React + Redux 在模块/域之间共享操作