简体   繁体   English

如何在 react.js 中创建我们自己的按钮组件,如 antdesign

[英]How to create our own Button Component like antdesign in react.js

I want to create my own Components So I had a look on antDesign which is one of the most popular library for React.js.我想创建自己的组件所以我看了一下 antDesign,它是 React.js 最流行的库之一。

My question is how can I create my own customised Button component without using any CSS libraries or Frameworks like (Bootstrap, SemanticUI, etc) in react.js like how antDesign team have created it.我的问题是,如何在不使用任何 CSS 库或框架(如 react.js 中的(Bootstrap、SemanticUI 等))的情况下创建自己的自定义按钮组件,就像 antDesign 团队如何创建它一样。

Any help would be appreciated with some explanation and an example.通过一些解释和示例,我们将不胜感激。

You can make a common component for Button as follow:您可以为 Button 创建一个通用组件,如下所示:

import React from 'react';
import styled from 'styled-components';

const PrimaryButton = styled.button`
    color: #fff;
    background-color: #1890ff;
    border-color: #1890ff;
    text-shadow: 0 -1px 0 rgba(0,0,0,0.12);
    -webkit-box-shadow: 0 2px 0 rgba(0,0,0,0.045);
    box-shadow: 0 2px 0 rgba(0,0,0,0.045);
`

const Button = (props) => {
    return (
        <PrimaryButton>
            {props.text}
        </PrimaryButton>
    );
};

export default Button;

Here, You can get props from a parent component and design can be done accordingly.在这里,您可以从父组件中获取道具,并且可以相应地进行设计。 Here in example, I have used styled-components as it provides a usage for css according to props.在此示例中,我使用了样式组件,因为它根据道具提供了 css 的用法。 I have just showed here for primary button.我刚刚在这里展示了主按钮。

From a calling components,从调用组件,

<Button type='primary' text='Reset' size={10} disabled={false} icon={'imgUrl'} shape='round' />

You can also pass many more things except shown in example.除了示例中显示的内容外,您还可以传递更多内容。

If you want to make a component simply make a function and use that function wherever required.如果您想制作一个组件,只需制作一个 function 并在需要的地方使用该 function。

ex:前任:

// buttonComponent.js
export default buttonComponent = () => {
  <button>Hi I am a button</button>
}

thats all you created your component, now can call this component in your code and use it.这就是您创建组件的全部内容,现在可以在您的代码中调用该组件并使用它。

But I guess this is not exactly what you want.但我想这不是你想要的。 There are some factor needs to be kept in mind before making a component mostly when making some complex components.主要是在制作一些复杂的组件时,在制作组件之前需要牢记一些因素。 you can do that by asking some question to yourself.你可以通过问自己一些问题来做到这一点。

lets take an example of button only, there are certain things a button component must do or have.让我们只举一个按钮的例子,按钮组件必须做或拥有某些事情。

  • What happens when some one click on the button?当有人点击按钮时会发生什么? (sounds dump but probably the most important one) (听起来很垃圾,但可能是最重要的一个)
  • Does user can change the text of the button, whats the default text of the button?用户是否可以更改按钮的文本,按钮的默认文本是什么?
  • Does the user can call their own function on click of the button?用户是否可以通过单击按钮调用自己的 function?
  • What's the default style of the button?按钮的默认样式是什么?
  • Does the user can disable the button?用户是否可以禁用该按钮?
  • Does the button can include icons too?按钮也可以包含图标吗?
  • Does user can change the styles of the button?用户可以更改按钮的 styles 吗?
  • How can the user change the default styles by overriding the styles in css or by passing it in props?用户如何通过覆盖 css 中的 styles 或通过道具传递来更改默认 styles?
  • ....etc ....ETC

Keeping those in mind for now let's say we are making a small button component with the following two features:暂时记住这些,假设我们正在制作一个具有以下两个功能的小按钮组件:

  • User can click on button and call their own function and if not provided then nothing will happen.用户可以单击按钮并调用自己的 function,如果不提供,则不会发生任何事情。
  • User can change the default style of the button by props or by overriding the css.用户可以通过 props 或覆盖 css 来更改按钮的默认样式。

then the code will be:那么代码将是:

// MyButtonComponent.js

export default MyButtonComponent = ({userFunction, userStyle, children}) => {
  <button type="button" onClick={userFunction} style={userStyle} className="my-button-compnonet">
    {children}
  </button>
}

// MyButtonComponent.css
.my-button-component {
  color: #fff;
  background-color: #007bff;
  border-color: #007bff;
  display: inline-block;
  font-weight: 400;
  text-align: center;
  vertical-align: middle;
  border: 1px solid transparent;
  padding: .375rem .75rem;
  font-size: 1rem;
  line-height: 1.5;
  border-radius: .25rem;
}

that's all our small button component is done.这就是我们所有的小按钮组件完成了。

now to call this component现在调用这个组件

// mainCompnonet.js

.....
/* callback function on button click */
myFunction = (e) => {
 alert("yeah my button component on click");
}

/* override the default style through the props */
myStyle = {
  borderRadius: 1rem;
}
.....
<MyButtonComponent userFunction={myFunction} userStyle={myStyle}>
 Hi This is my component Button 
</MyButtonComponent>
......

// mainCompnonet.js
.my-button-component {
  color: #000;  /* overrides our component style through css */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM