Ant?Design?組件庫按鈕實現(xiàn)示例詳解
1 antd 之 Button API
antd
組件庫是基于 Ant Design 設(shè)計體系的 React UI 組件庫,antd
為 Web 應(yīng)用提供了豐富的基礎(chǔ) UI 組件,可以用于研發(fā)企業(yè)級中后臺產(chǎn)品。這篇咱們介紹 antd 組件庫之 按鈕。
按鈕 Button 是一個比較基礎(chǔ)的 UI 組件,一般在有交互的應(yīng)用中都會用到。
其 DOM 節(jié)點為 <Button>...</Button>
,antd 中的按鈕樣式豐富,可以通過設(shè)置 Button 的屬性來產(chǎn)生不同的 按鈕樣式。
這些可配置的屬性主要包括:type、shape、size、loading 等,詳細的這里我進行一個整理:
下面做一些實踐。
2 antd 之 Button 示例
先來看 type 屬性的六個簡單的按鈕,上代碼 (JavaScript的):
import { Button } from 'antd'; import React from 'react'; const App = () => ( <> <Button type="primary">Primary Button</Button> <Button>Default Button</Button> <Button type="dashed">Dashed Button</Button> <br /> <Button type="text">Text Button</Button> <Button type="link">Link Button</Button> <Button type="ghost">Ghost Button</Button> </> ); export default App;
來看效果:
接下來看一波帶 icon 圖標(biāo)的按鈕,上代碼:
import { SearchOutlined, PlayCircleOutlined, ZoomInOutlined, RedoOutlined, AndroidOutlined, AppleOutlined, WechatOutlined, EyeOutlined, ShareAltOutlined, MessageOutlined} from '@ant-design/icons'; import { Button, Tooltip } from 'antd'; import React from 'react'; const App = () => ( <> <Tooltip title="search"> <Button type="primary" shape="circle" icon={<PlayCircleOutlined />} /> </Tooltip> <Button type="primary" shape="circle"> A </Button> <Button type="primary" icon={<ZoomInOutlined />}> Search </Button> <Tooltip title="search"> <Button shape="circle" icon={<RedoOutlined />} /> </Tooltip> <Button icon={<AndroidOutlined />}>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 />} rel="external nofollow" rel="external nofollow" /> <br /> <br /> <Tooltip title="search"> <Button type="primary" shape="circle" icon={<AppleOutlined />} size="large" /> </Tooltip> <Button type="primary" shape="circle" size="large"> A </Button> <Button type="primary" icon={<WechatOutlined />} size="large"> Search </Button> <Tooltip title="search"> <Button shape="circle" icon={<SearchOutlined />} size="large" /> </Tooltip> <Button icon={<EyeOutlined />} size="large"> Search </Button> <br /> <Tooltip title="search"> <Button shape="circle" icon={<SearchOutlined />} size="large" /> </Tooltip> <Button icon={<ShareAltOutlined />} 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={<MessageOutlined />} size="large" rel="external nofollow" rel="external nofollow" /> </> ); export default App;
來看效果:
你應(yīng)該可以發(fā)現(xiàn),我這里用了很多不同的 icon 圖標(biāo),可以這么說:用 antd 的 Button 搭配 antd 的 Icon 圖標(biāo),幾乎能實現(xiàn)你想要的所有按鈕樣式。除了按鈕的圖標(biāo),上面的示例也演示了 按鈕 Size 的調(diào)整,通過 size
可以配置 large
和 small
, 分別對應(yīng)將按鈕設(shè)置為 大尺寸和 小尺寸,若不設(shè)置 size
, 則默認為中尺寸。
接著,我們來看按鈕的 disabled
屬性,意思即為按鈕處于不可用的狀態(tài),上代碼:
import { Button } from 'antd'; import React from 'react'; const App = () => ( <> <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;
來看效果:
還有一種按鈕是,點擊后服務(wù)器需要響應(yīng)一會兒,即加載狀態(tài),這個 Loding... 的狀態(tài)用 antd 的按鈕也可以展現(xiàn),上代碼:
import { PoweroffOutlined } from '@ant-design/icons'; import { Button, Space } from 'antd'; import React, { useState } from 'react'; 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 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;
來看效果:
好了,以上分享了 Ant Design 組件庫之按鈕。希望我的分享能對你的學(xué)習(xí)有一點幫助。
更多關(guān)于Ant Design 組件庫按鈕的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
ReactJS?應(yīng)用兼容ios9對標(biāo)ie11解決方案
這篇文章主要為大家介紹了ReactJS?應(yīng)用兼容ios9對標(biāo)ie11解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01React函數(shù)組件hook原理及構(gòu)建hook鏈表算法詳情
這篇文章主要介紹了React函數(shù)組件hook原理及構(gòu)建hook鏈表算法詳情,每一個?hook?函數(shù)都有對應(yīng)的?hook?對象保存狀態(tài)信息,更多詳細分析,需要的朋友可以參考一下2022-07-07