Button按钮

按钮用于开始一个即时操作。

何时使用#

标记了一个(或封装一组)操作命令,响应用户点击行为,触发相应的业务逻辑。

在 Ant Design 中我们提供了五种按钮。

  • 主按钮:用于主行动点,一个操作区域只能有一个主按钮。

  • 默认按钮:用于没有主次之分的一组行动点。

  • 虚线按钮:常用于添加操作。

  • 文本按钮:用于最次级的行动点。

  • 链接按钮:一般用于链接,即导航至某位置。

以及四种状态属性与上面配合使用。

  • 危险:删除/移动/修改权限等危险操作,一般需要二次确认。

  • 幽灵:用于背景色比较复杂的地方,常用在首页/产品页等展示场景。

  • 禁用:行动点不可用的时候,一般需要文案解释。

  • 加载中:用于异步操作等待反馈的时候,也可以避免多次提交。

完整设计指南

代码演示






LinkLinkLinkLinkLink
Dash LinkLarge Dash LinkDash LinkDash LinkDash Link

Fugiat tempor magna cupidatat exercitationEt veniam Lorem laborum et occaecat cupidatat commodo Non eiusmod excepteur eiusmod ea consectetur fugiat excepteur quis exercitation. Nostrud magna amet qui qui sint adipisicing. Consequat in anim tempor quis irure. Anim pariatur minim esse mollit irure ex nulla Lorem ut ad quis non dolor anim. Voluptate reprehenderit commodo laboris et reprehenderit aliquip sunt qui sint id. Cillum quis elit nulla dolore reprehenderit minim elit enim velit sint sit dolor.

按钮有七种类型:主按钮、次按钮、默认按钮、虚线按钮、文本按钮和链接按钮。主按钮在同一个操作区域最多出现一次。

expand codeexpand code
import { Button } from 'antd';
import React from 'react';
import { RemindHintIcon, RemindWarningIcon } from '@gd-uikit/icons';

const App: React.FC = () => (
  <>
    <Button type="primary">
      <RemindWarningIcon />
      Primary
    </Button>
    <Button type="primary" loading>
      Primary
    </Button>
    <Button type="primary" danger>
      Primary
    </Button>
    <Button type="primary" disabled>
      Primary
    </Button>
    <br />
    <Button type="secondary">Secondary</Button>
    <Button type="secondary" loading>
      Secondary
    </Button>
    <Button type="secondary" danger>
      Secondary
    </Button>
    <Button type="secondary" disabled>
      Secondary
    </Button>
    <br />
    <Button icon={<RemindWarningIcon />}>Default</Button>
    <Button loading>Default</Button>
    <Button danger>Default</Button>
    <Button disabled>Default</Button>
    <br />
    <Button type="dashed">Dashed</Button>
    <Button type="dashed" loading>
      Dashed
    </Button>
    <Button type="dashed" danger>
      Dashed
    </Button>
    <Button type="dashed" disabled>
      Dashed
    </Button>
    <br />
    <Button type="text">
      <RemindHintIcon />
      Text
    </Button>
    <Button type="text" loading>
      Text
    </Button>
    <Button type="text" danger>
      Text
    </Button>
    <Button type="text" disabled>
      Text
    </Button>
    <br />
    <Button type="link">Link</Button>
    <Button type="link" size="large">
      Link
    </Button>
    <Button type="link" loading>
      Link
    </Button>
    <Button type="link" danger>
      Link
    </Button>
    <Button type="link" disabled>
      Link
    </Button>
    <br />
    <Button type="dashlink">Dash Link</Button>
    <Button type="dashlink" size="large">
      Large Dash Link
    </Button>
    <Button type="dashlink" loading>
      Dash Link
    </Button>
    <Button type="dashlink" danger>
      Dash Link
    </Button>
    <Button type="dashlink" disabled>
      Dash Link
    </Button>

    <p>
      Fugiat tempor magna cupidatat exercitation
      <Button type="dashlink">
        Et veniam Lorem laborum et occaecat cupidatat commodo Non eiusmod excepteur eiusmod ea
        consectetur fugiat excepteur quis
      </Button> exercitation. Nostrud magna amet qui qui sint adipisicing. Consequat in anim tempor quis
      irure. Anim pariatur minim esse mollit irure ex nulla Lorem ut ad quis non dolor anim. Voluptate
      reprehenderit commodo laboris et reprehenderit aliquip sunt qui sint id. Cillum quis elit nulla
      dolore reprehenderit minim elit enim velit sint sit dolor.
    </p>
  </>
);

export default App;



Link

按钮有大、中、小三种尺寸。

通过设置 sizelarge small 分别把按钮设为大、小尺寸。若不设置 size,则尺寸为中。

expand codeexpand code
import { DownloadOutlined } from '@ant-design/icons';
import { Button, Radio } from 'antd';
import type { SizeType } from 'antd/es/config-provider/SizeContext';
import React, { useState } from 'react';

const App: React.FC = () => {
  const [size, setSize] = useState<SizeType>('large');

  return (
    <>
      <Radio.Group value={size} onChange={e => setSize(e.target.value)}>
        <Radio.Button value="large">Large</Radio.Button>
        <Radio.Button value="default">Default</Radio.Button>
        <Radio.Button value="small">Small</Radio.Button>
      </Radio.Group>
      <br />
      <br />
      <Button type="primary" size={size}>
        Primary
      </Button>
      <Button size={size}>Default</Button>
      <Button type="dashed" size={size}>
        Dashed
      </Button>
      <br />
      <Button type="link" size={size}>
        Link
      </Button>
      <br />
      <Button type="primary" icon={<DownloadOutlined />} size={size} />
      <Button type="primary" shape="circle" icon={<DownloadOutlined />} size={size} />
      <Button type="primary" shape="round" icon={<DownloadOutlined />} size={size} />
      <Button type="primary" shape="round" icon={<DownloadOutlined />} size={size}>
        Download
      </Button>
      <Button type="primary" icon={<DownloadOutlined />} size={size}>
        Download
      </Button>
    </>
  );
};

export default App;

添加 loading 属性即可让按钮处于加载状态,最后两个按钮演示点击后进入加载状态。

expand codeexpand code
import { PoweroffOutlined } from '@ant-design/icons';
import { Button, Space } from 'antd';
import React, { useState } from 'react';

const App: React.FC = () => {
  const [loadings, setLoadings] = useState<boolean[]>([]);

  const enterLoading = (index: number) => {
    setLoadings(prevLoadings => {
      const newLoadings = [...prevLoadings];
      newLoadings[index] = true;
      return newLoadings;
    });

    setTimeout(() => {
      setLoadings(prevLoadings => {
        const newLoadings = [...prevLoadings];
        newLoadings[index] = false;
        return newLoadings;
      });
    }, 6000);
  };

  return (
    <>
      <Space style={{ width: '100%' }}>
        <Button type="primary" loading>
          Loading
        </Button>
        <Button type="primary" size="small" loading>
          Loading
        </Button>
        <Button type="primary" icon={<PoweroffOutlined />} loading />
      </Space>

      <Space style={{ width: '100%' }}>
        <Button type="primary" loading={loadings[0]} onClick={() => enterLoading(0)}>
          Click me!
        </Button>
        <Button
          type="primary"
          icon={<PoweroffOutlined />}
          loading={loadings[1]}
          onClick={() => enterLoading(1)}
        >
          Click me!
        </Button>
        <Button
          type="primary"
          icon={<PoweroffOutlined />}
          loading={loadings[2]}
          onClick={() => enterLoading(2)}
        />
      </Space>
    </>
  );
};

export default App;

幽灵按钮将按钮的内容反色,背景变为透明,常用在有色背景上。

expand codeexpand code
import { Button } from 'antd';
import React from 'react';

const App: React.FC = () => (
  <div className="site-button-ghost-wrapper">
    <Button type="primary" ghost>
      Primary
    </Button>
    <Button ghost>Default</Button>
    <Button type="dashed" ghost>
      Dashed
    </Button>
    <Button type="primary" danger ghost>
      Danger
    </Button>
  </div>
);

export default App;
.site-button-ghost-wrapper {
  padding: 26px 16px 16px;
  background: rgb(190, 200, 200);
}
Link

block 属性将使按钮适合其父宽度。

expand codeexpand code
import { Button } from 'antd';
import React from 'react';

const App: React.FC = () => (
  <>
    <Button type="primary" block>
      Primary
    </Button>
    <Button block>Default</Button>
    <Button type="dashed" block>
      Dashed
    </Button>
    <Button type="link" block>
      Link
    </Button>
  </>
);

export default App;




当需要在 Button 内嵌入 Icon 时,可以设置 icon 属性,或者直接在 Button 内使用 Icon 组件。

如果想控制 Icon 具体的位置,只能直接使用 Icon 组件,而非 icon 属性。

expand codeexpand code
import { SearchOutlined } from '@ant-design/icons';
import { Button, Tooltip } from 'antd';
import React from 'react';
import { RemindHintIcon, RemindWarningIcon } from '@gd-uikit/icons';

const App: React.FC = () => (
  <>
    <Tooltip title="search">
      <Button type="primary" shape="circle" icon={<SearchOutlined />} />
    </Tooltip>
    <Button type="primary" shape="circle">
      A
    </Button>
    <Button type="primary" icon={<SearchOutlined />}>
      Search
    </Button>
    <Button type="secondary" shape="circle">
      <RemindHintIcon />
    </Button>
    <Button type="secondary">
      <RemindHintIcon />
    </Button>
    <Button type="secondary" icon={<RemindWarningIcon />} />
    <Tooltip title="search">
      <Button shape="circle" icon={<SearchOutlined />} />
    </Tooltip>
    <Button icon={<SearchOutlined />}>Search</Button>
    <br />
    <Tooltip title="search">
      <Button shape="circle" icon={<SearchOutlined />} />
    </Tooltip>
    <Button icon={<SearchOutlined />}>Search</Button>
    <Tooltip title="search">
      <Button type="dashed" shape="circle" icon={<SearchOutlined />} />
    </Tooltip>
    <Button type="dashed" icon={<SearchOutlined />}>
      Search
    </Button>
    <Button icon={<SearchOutlined />} href="https://www.google.com" />
    <br />
    <br />
    <Tooltip title="search">
      <Button type="primary" shape="circle" icon={<SearchOutlined />} size="large" />
    </Tooltip>
    <Button type="primary" shape="circle" size="large">
      A
    </Button>
    <Button type="primary" icon={<SearchOutlined />} size="large">
      Search
    </Button>
    <Tooltip title="search">
      <Button shape="circle" icon={<SearchOutlined />} size="large" />
    </Tooltip>
    <Button icon={<SearchOutlined />} size="large">
      Search
    </Button>
    <br />
    <Tooltip title="search">
      <Button shape="circle" icon={<SearchOutlined />} size="large" />
    </Tooltip>
    <Button icon={<SearchOutlined />} size="large">
      Search
    </Button>
    <Tooltip title="search">
      <Button type="dashed" shape="circle" icon={<SearchOutlined />} size="large" />
    </Tooltip>
    <Button type="dashed" icon={<SearchOutlined />} size="large">
      Search
    </Button>
    <Button icon={<SearchOutlined />} size="large" href="https://www.google.com" />
  </>
);

export default App;




LinkLink(disabled)


Danger LinkDanger Link(disabled)

添加 disabled 属性即可让按钮处于不可用状态,同时按钮样式也会改变。

expand codeexpand code
import { Button } from 'antd';
import React from 'react';

const App: React.FC = () => (
  <>
    <Button type="primary">Primary</Button>
    <Button type="primary" disabled>
      Primary(disabled)
    </Button>
    <br />
    <Button>Default</Button>
    <Button disabled>Default(disabled)</Button>
    <br />
    <Button type="dashed">Dashed</Button>
    <Button type="dashed" disabled>
      Dashed(disabled)
    </Button>
    <br />
    <Button type="text">Text</Button>
    <Button type="text" disabled>
      Text(disabled)
    </Button>
    <br />
    <Button type="link">Link</Button>
    <Button type="link" disabled>
      Link(disabled)
    </Button>
    <br />
    <Button danger>Danger Default</Button>
    <Button danger disabled>
      Danger Default(disabled)
    </Button>
    <br />
    <Button danger type="text">
      Danger Text
    </Button>
    <Button danger type="text" disabled>
      Danger Text(disabled)
    </Button>
    <br />
    <Button type="link" danger>
      Danger Link
    </Button>
    <Button type="link" danger disabled>
      Danger Link(disabled)
    </Button>
    <div className="site-button-ghost-wrapper">
      <Button ghost>Ghost</Button>
      <Button ghost disabled>
        Ghost(disabled)
      </Button>
    </div>
  </>
);

export default App;
.site-button-ghost-wrapper {
  padding: 8px 8px 0 8px;
  background: rgb(190, 200, 200);
}

按钮组合使用时,推荐使用 1 个主操作 + n 个次操作,3 个以上操作时把更多操作放到 Dropdown.Button 中组合使用。

expand codeexpand code
import type { MenuProps } from 'antd';
import { Button, Dropdown } from 'antd';
import React from 'react';

const onMenuClick: MenuProps['onClick'] = e => {
  console.log('click', e);
};

const items = [
  {
    key: '1',
    label: '1st item',
  },
  {
    key: '2',
    label: '2nd item',
  },
  {
    key: '3',
    label: '3rd item',
  },
];

const App: React.FC = () => (
  <>
    <Button type="primary">primary</Button>
    <Button>secondary</Button>
    <Dropdown.Button menu={{ items, onClick: onMenuClick }}>Actions</Dropdown.Button>
  </>
);

export default App;
Link

在 4.0 之后,危险成为一种按钮属性而不是按钮类型。

expand codeexpand code
import { Button } from 'antd';
import React from 'react';

const App: React.FC = () => (
  <>
    <Button type="primary" danger>
      Primary
    </Button>
    <Button danger>Default</Button>
    <Button type="dashed" danger>
      Dashed
    </Button>
    <Button type="text" danger>
      Text
    </Button>
    <Button type="link" danger>
      Link
    </Button>
  </>
);

export default App;

API#

通过设置 Button 的属性来产生不同的按钮样式,推荐顺序为:type -> shape -> size -> loading -> disabled

按钮的属性说明如下:

属性说明类型默认值版本
block将按钮宽度调整为其父宽度的选项booleanfalse
danger设置危险按钮booleanfalse
disabled按钮失效状态booleanfalse
ghost幽灵属性,使按钮背景透明booleanfalse
href点击跳转的地址,指定此属性 button 的行为和 a 链接一致string-
htmlType设置 button 原生的 type 值,可选值请参考 HTML 标准stringbutton
icon设置按钮的图标组件ReactNode-
loading设置按钮载入状态boolean | { delay: number }false
shape设置按钮形状default | circle | round'default'
size设置按钮大小large | middle | smallmiddle
target相当于 a 链接的 target 属性,href 存在时生效string-
type设置按钮类型primary | ghost | dashed | link | text | defaultdefault
onClick点击按钮时的回调(event) => void-

支持原生 button 的其他所有属性。

FAQ#

如何移除两个汉字之间的空格?#

根据 Ant Design 设计规范要求,我们会在按钮内(文本按钮和链接按钮除外)只有两个汉字时自动添加空格,如果你不需要这个特性,可以设置 ConfigProviderautoInsertSpaceInButtonfalse

移除两个汉字之间的空格

设计指引#

组件总览Icon图标