ant-design/components/button/demo/loading.md

77 lines
1.8 KiB
Markdown
Raw Normal View History

2016-03-31 09:40:55 +08:00
---
order: 4
title:
zh-CN: 加载中状态
en-US: Loading
2016-03-31 09:40:55 +08:00
---
2015-06-17 21:29:58 +08:00
## zh-CN
2015-12-28 13:57:02 +08:00
添加 `loading` 属性即可让按钮处于加载状态,最后两个按钮演示点击后进入加载状态。
## en-US
2016-04-25 11:04:56 +08:00
A loading indicator can be added to a button by setting the `loading` property on the `Button`.
```tsx
2019-12-19 11:51:12 +08:00
import { PoweroffOutlined } from '@ant-design/icons';
2022-05-23 14:37:16 +08:00
import { Button, Space } from 'antd';
import React, { useState } from 'react';
2015-09-27 16:30:35 +08:00
const App: React.FC = () => {
const [loadings, setLoadings] = useState<boolean[]>([]);
const enterLoading = (index: number) => {
setLoadings(prevLoadings => {
const newLoadings = [...prevLoadings];
2020-06-02 12:54:22 +08:00
newLoadings[index] = true;
return newLoadings;
});
setTimeout(() => {
setLoadings(prevLoadings => {
const newLoadings = [...prevLoadings];
2020-06-02 12:54:22 +08:00
newLoadings[index] = false;
return newLoadings;
2020-06-02 12:54:22 +08:00
});
}, 6000);
2019-05-07 14:57:32 +08:00
};
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>
2015-07-29 22:23:55 +08:00
<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;
2019-05-07 14:57:32 +08:00
```