手写antd按钮组件

通过模仿antd按钮组件的实现,可以扩展自己自定义组件的能力。

1. 分析按钮组件

Button组件有几大属性:

  • type: default,primary,link
  • danger: 布尔值
  • size: small, middle, large
  • icon: 图标

2. 编写按钮组件

创建文件夹JKAntd,并在里面创建JKButton文件夹,分别在JKButton文件夹下面创建JKButton.jsx,JKButton.css, 然后再JKAntd创建index.js

jsx
import classNames from 'classnames'

import './JKButton.css'

import PropTypes from 'prop-types'

function JKButton({type, danger, size, icon}) {

  const btnStyle =classNames({

    'jk-ant-btn': true,

    'jk-ant-btn-danger': danger,

    [`jk-ant-btn-${type}`]: true,

    [`jk-ant-btn-${size}`]: true,

  })



  return (

    <button className={btnStyle}>{icon && <span>{icon}</span>}按钮</button>

  )

}



JKButton.defaultProps = {

  type: 'default',

  danger: false,

  size: 'middle',

  icon: null

}

// 限制传值的类型

JKButton.propTypes = {

  type: PropTypes.string,

  danger: PropTypes.bool,

  size: PropTypes.oneOf(['small', 'middle', 'large']),

  icon: PropTypes.element,

}



export default JKButton;
css
.jk-ant-btn{

    font-size: 14px;

    line-height: 1.5714285714285714;

    height: 32px;

    padding: 4px 15px;

    border-radius: 6px;

    outline: none;

    position: relative;

    display: inline-flex;

    gap: 8px;

    align-items: center;

    justify-content: center;

    font-weight: 400;

    white-space: nowrap;

    text-align: center;

    background-image: none;

    background: transparent;

    border: 1px solid transparent;

    cursor: pointer;

    transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);

    user-select: none;

    touch-action: manipulation;

    color: rgba(0, 0, 0, 0.88);

}



.jk-ant-btn-default{

    border-color: #d9d9d9;

    background: #ffffff;

    color: rgba(0, 0, 0, 0.88);

    box-shadow: 0 2px 0 rgba(0, 0, 0, 0.02);

}



.jk-ant-btn-primary{

    color: #fff;

    background: #1677ff;

    box-shadow: 0 2px 0 rgba(5, 145, 255, 0.1);

}

.jk-ant-btn-danger{

    color: #fff;

    background: #ff4d4f;

}

.jk-ant-btn-large{

    font-size: 16px;

    line-height: 1.5;

    height: 40px;

    padding: 7px 15px;

    border-radius: 8px;

}



.jk-ant-btn-small{

    font-size: 14px;

    line-height: 1.5714285714285714;

    height: 24px;

    padding: 0px 7px;

    border-radius: 4px;

}
jsx
import { Button, Row, Space } from 'antd'

import { PlusCircleFilled } from '@ant-design/icons'

import { JKButton } from './JKAntd'



function App() {

  return (

    <>

      <Row>

        <Space>

          <Button type="default" >按钮</Button>

          <Button type="primary" >按钮</Button>

          <Button type="primary" danger>按钮</Button>

          <Button type="primary" size='small'>按钮</Button>

          <Button type="primary" size='large'>按钮</Button>

          <Button type="primary" icon={<PlusCircleFilled/>}>按钮</Button>

        </Space>

      </Row>

      <br/>

      <br/>

      <Row>

        <Space>

          <JKButton type="default" >按钮</JKButton>

          <JKButton type="primary" >按钮</JKButton>

          <JKButton type="primary" danger>按钮</JKButton>

          <JKButton type="primary" size='small'>按钮</JKButton>

          <JKButton type="primary" size='large'>按钮</JKButton>

          <JKButton type="primary" icon={<PlusCircleFilled/>}>按钮</JKButton>

        </Space>

      </Row>

    </>

  )

}

export default App
js
import JKButton from './JKButton/JKButton.jsx'



export  {JKButton}
执行效果: ![Alt text](/web/react/images/Snipaste_20241224_213734.png)

评论