按鈕用于開始一個即時操作。
標記了一個(或封裝一組)操作命令,響應(yīng)用戶點擊行為,觸發(fā)相應(yīng)的業(yè)務(wù)邏輯。
在 Ant Design 中我們提供了五種按鈕。
以及四種狀態(tài)屬性與上面配合使用。
import { Button, Space } from 'antd';
const App = () => (
<Space wrap>
<Button type="primary">Primary Button</Button>
<Button>Default Button</Button>
<Button type="dashed">Dashed Button</Button>
<Button type="text">Text Button</Button>
<Button type="link">Link Button</Button>
</Space>
);
export default App;
import { SearchOutlined } from '@ant-design/icons';
import { Button, Tooltip, Space } from 'antd';
const App = () => (
<Space direction="vertical">
<Space wrap>
<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>
<Tooltip title="search">
<Button shape="circle" icon={<SearchOutlined />} />
</Tooltip>
<Button icon={<SearchOutlined />}>Search</Button>
</Space>
<Space wrap>
<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 />} rel="external nofollow" target="_blank" />
</Space>
</Space>
);
export default App;
import { DownloadOutlined } from '@ant-design/icons';
import { Button, Radio, Space, Divider } from 'antd';
const App = () => {
const [size, setSize] = useState('large'); // default is 'middle'
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>
<Divider orientation="left" plain>
Preview
</Divider>
<Space direction="vertical">
<Space wrap>
<Button type="primary" size={size}>
Primary
</Button>
<Button size={size}>Default</Button>
<Button type="dashed" size={size}>
Dashed
</Button>
</Space>
<Button type="link" size={size}>
Link
</Button>
<Space wrap>
<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>
</Space>
</Space>
</>
);
};
export default App;
import { Button, Space } from 'antd';
const App = () => (
<Space direction="vertical">
<Space>
<Button type="primary">Primary</Button>
<Button type="primary" disabled>
Primary(disabled)
</Button>
</Space>
<Space>
<Button>Default</Button>
<Button disabled>Default(disabled)</Button>
</Space>
<Space>
<Button type="dashed">Dashed</Button>
<Button type="dashed" disabled>
Dashed(disabled)
</Button>
</Space>
<Space>
<Button type="text">Text</Button>
<Button type="text" disabled>
Text(disabled)
</Button>
</Space>
<Space>
<Button type="link">Link</Button>
<Button type="link" disabled>
Link(disabled)
</Button>
</Space>
<Space>
<Button type="primary" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >
Href Primary
</Button>
<Button type="primary" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" disabled>
Href Primary(disabled)
</Button>
</Space>
<Space>
<Button danger>Danger Default</Button>
<Button danger disabled>
Danger Default(disabled)
</Button>
</Space>
<Space>
<Button danger type="text">
Danger Text
</Button>
<Button danger type="text" disabled>
Danger Text(disabled)
</Button>
</Space>
<Space>
<Button type="link" danger>
Danger Link
</Button>
<Button type="link" danger disabled>
Danger Link(disabled)
</Button>
</Space>
<Space className="site-button-ghost-wrapper">
<Button ghost>Ghost</Button>
<Button ghost disabled>
Ghost(disabled)
</Button>
</Space>
</Space>
);
export default App;
import { PoweroffOutlined } from '@ant-design/icons';
import { Button, Space } from 'antd';
const App = () => {
const [loadings, setLoadings] = useState([]);
const enterLoading = (index) => {
setLoadings((prevLoadings) => {
const newLoadings = [...prevLoadings];
newLoadings[index] = true;
return newLoadings;
});
setTimeout(() => {
setLoadings((prevLoadings) => {
const newLoadings = [...prevLoadings];
newLoadings[index] = false;
return newLoadings;
});
}, 6000);
};
return (
<Space direction="vertical">
<Space wrap>
<Button type="primary" loading>
Loading
</Button>
<Button type="primary" size="small" loading>
Loading
</Button>
<Button type="primary" icon={<PoweroffOutlined />} loading />
</Space>
<Space wrap>
<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>
</Space>
);
};
export default App;
import { Button, Dropdown, Space } from 'antd';
const onMenuClick = (e) => {
console.log('click', e);
};
const items = [
{
key: '1',
label: '1st item',
},
{
key: '2',
label: '2nd item',
},
{
key: '3',
label: '3rd item',
},
];
const App = () => (
<Space direction="vertical">
<Button type="primary">primary</Button>
<Button>secondary</Button>
<Dropdown.Button
menu={{
items,
onClick: onMenuClick,
}}
>
Actions
</Dropdown.Button>
</Space>
);
export default App;
import { Button, Space } from 'antd';
const App = () => (
<Space className="site-button-ghost-wrapper" wrap>
<Button type="primary" ghost>
Primary
</Button>
<Button ghost>Default</Button>
<Button type="dashed" ghost>
Dashed
</Button>
<Button type="primary" danger ghost>
Danger
</Button>
</Space>
);
export default App;
import { Button, Dropdown, Space } from 'antd';
const onMenuClick = (e) => {
console.log('click', e);
};
const items = [
{
key: '1',
label: '1st item',
},
{
key: '2',
label: '2nd item',
},
{
key: '3',
label: '3rd item',
},
];
const App = () => (
<Space direction="vertical">
<Button type="primary">primary</Button>
<Button>secondary</Button>
<Dropdown.Button
menu={{
items,
onClick: onMenuClick,
}}
>
Actions
</Dropdown.Button>
</Space>
);
export default App;
import { Button, Space } from 'antd';
const App = () => (
<Space
direction="vertical"
style={{
width: '100%',
}}
>
<Button type="primary" block>
Primary
</Button>
<Button block>Default</Button>
<Button type="dashed" block>
Dashed
</Button>
<Button disabled block>
disabled
</Button>
<Button type="text" block>
text
</Button>
<Button type="link" block>
Link
</Button>
</Space>
);
export default App;
import { Button, Space } from 'antd';
const App = () => (
<Space wrap>
<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>
</Space>
);
export default App;
通用屬性參考:通用屬性
通過設(shè)置 Button 的屬性來產(chǎn)生不同的按鈕樣式,推薦順序為:type
-> shape
-> size
-> loading
-> disabled
。
按鈕的屬性說明如下:
屬性 | 說明 | 類型 | 默認值 | 版本 | ||||
---|---|---|---|---|---|---|---|---|
block | 將按鈕寬度調(diào)整為其父寬度的選項 | boolean | false | |||||
classNames | 語義化結(jié)構(gòu) class | Record |
- | 5.4.0 | ||||
danger | 設(shè)置危險按鈕 | boolean | false | |||||
disabled | 設(shè)置按鈕失效狀態(tài) | boolean | false | |||||
ghost | 幽靈屬性,使按鈕背景透明 | boolean | false | |||||
href | 點擊跳轉(zhuǎn)的地址,指定此屬性 button 的行為和 a 鏈接一致 | string | - | |||||
htmlType | 設(shè)置 button 原生的 type 值,可選值請參考 HTML 標準 |
string | button |
|||||
icon | 設(shè)置按鈕的圖標組件 | ReactNode | - | |||||
loading | 設(shè)置按鈕載入狀態(tài) | boolean \ | { delay: number } | false | ||||
shape | 設(shè)置按鈕形狀 | default \ |
circle \ |
round |
default |
|||
size | 設(shè)置按鈕大小 | large \ |
middle \ |
small |
middle |
|||
styles | 語義化結(jié)構(gòu) style | Record |
- | 5.4.0 | ||||
target | 相當于 a 鏈接的 target 屬性,href 存在時生效 | string | - | |||||
type | 設(shè)置按鈕類型 | primary \ |
dashed \ |
link \ |
text \ |
default |
default |
|
onClick | 點擊按鈕時的回調(diào) | (event: MouseEvent) => void | - |
支持原生 button 的其他所有屬性。
styles
和 classNames
屬性名稱 | 說明 | 版本 |
---|---|---|
icon | 設(shè)置圖標元素 | 5.5.0 |
根據(jù) Ant Design 設(shè)計規(guī)范要求,我們會在按鈕內(nèi)(文本按鈕和鏈接按鈕除外)只有兩個漢字時自動添加空格,如果你不需要這個特性,可以設(shè)置 ConfigProvider 的 autoInsertSpaceInButton
為 false
。
更多建議: