自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Semantic-UI的React實(shí)現(xiàn)(三):基本元素組件

開發(fā) 開發(fā)工具
Semantic-UI中的基本元素均為純CSS類定義的組件,沒有js的操作,因此實(shí)現(xiàn)起來比較簡單。有了前面基礎(chǔ)類UiElement和輔助類PropsHelper的實(shí)現(xiàn),要實(shí)現(xiàn)一個(gè)基本元素組件非常輕松。

[[173784]]

Semantic-UI官方的React組件化已經(jīng)快要接近完成了,最近開放了官網(wǎng):http://react.semantic-ui.com/。從官網(wǎng)看,基本組件已經(jīng)基本完備,還有幾個(gè)Addon也在進(jìn)行中。

基本元素組件

Semantic-UI中的基本元素均為純CSS類定義的組件,沒有js的操作,因此實(shí)現(xiàn)起來比較簡單。有了前面基礎(chǔ)類UiElement和輔助類PropsHelper的實(shí)現(xiàn),要實(shí)現(xiàn)一個(gè)基本元素組件非常輕松。

以Button組件舉例。Button組件可以單獨(dú)存在,也可以作為組組件使用。另外Button組件也允許簡單的Animation存在,即一對(duì)顯示/隱藏的組件可以隨鼠標(biāo)狀態(tài)而切換。外部調(diào)用的大致形式為:

  1. <Button.Group size='small'
  2.  
  3. <Button primary onClick={this.handleClickBtn1}>按鍵1</Button> 
  4.  
  5. <Button color='blue' onClick={this.handleClickBtn2}>按鍵2</Button> 
  6.  
  7. <Button animated onClick={this.handleClickBtn3}> 
  8.  
  9. <Button.Content visible>按鍵3顯示內(nèi)容</Button> 
  10.  
  11. <Button.Content hidden>按鍵3隱藏內(nèi)容</Button> 
  12.  
  13. </Button> 
  14.  
  15. </Button.Group

 

調(diào)用方式實(shí)際上是很直觀的,屬性均作為props傳入到Button組件中,事件系統(tǒng)的回調(diào)方法也與普通方式并無二致。相對(duì)復(fù)雜的處理,是要整理所有組件的共通屬性,定義它們的類型和取值范圍。

Button

Button作為基本組件,有非常多常用的屬性。這些屬性在命名上,基本參照Semantic-UI的原有CSS類名,在Button.js中用常量PROP_TYPES來定義。

  1. const PROP_TYPES = [ 
  2.  
  3. 'primary''secondary''animated''labeled''basic''inverted''color'
  4.  
  5. 'size''fluid''active''disabled''loading''compact''circular''positive'
  6.  
  7. 'negative''floated''attached''iconed''dropdown' 
  8.  
  9. ]; 

 

組件根據(jù)PropsHelper的相關(guān)方法來生成propTypes定義,并且通過父類(UiElement)的createElementStyle方法來編輯和組裝所使用的CSS類。另外,還通過父類的getEventCallback方法,來聲明相關(guān)的事件系統(tǒng)回調(diào)處理。

  1. class Button extends UiElement { 
  2.    
  3.   // 類型定義 
  4.   static propTypes = { 
  5.     ...PropsHelper.createPropTypes(PROP_TYPES) 
  6.   }; 
  7.    
  8.   render() { 
  9.    
  10.     // 生成元素style 
  11.     let style = this.createElementStyle(this.props, PROP_TYPES, 'button'); 
  12.      
  13.     return ( 
  14.       <div id={this.props.id} className={style} {...this.getEventCallback()} tabIndex='0'
  15.         {this.props.children} 
  16.       </div> 
  17.     ); 
  18.   } 

 

Button.Group

與Button組件類似,Group組件也繼承于UiElement以生成其聲明的公有屬性對(duì)應(yīng)的CSS類。

  1. // 屬性定義 
  2. const GROUP_PROP_TYPES = [ 
  3.   'iconed''vertical''labeled''equalWidth''color'
  4. ]; 
  5.  
  6. /** 
  7.  * 按鍵組組件 
  8.  */ 
  9. class Group extends UiElement { 
  10.  
  11.   // 類型定義 
  12.   static propTypes = { 
  13.     ...PropsHelper.createPropTypes(GROUP_PROP_TYPES) 
  14.   }; 
  15.  
  16.   /** 
  17.    * 取得渲染內(nèi)容 
  18.    */ 
  19.   render() { 
  20.  
  21.     // 生成元素Style 
  22.     let style = this.createElementStyle(this.props, PROP_TYPES, 'buttons'); 
  23.  
  24.     return ( 
  25.       <div id={this.props.id} className={style} {...this.getEventCallback()}> 
  26.         {this.props.children} 
  27.       </div> 
  28.     ); 
  29.   } 

 

Button.Content

Content組件的實(shí)現(xiàn)更簡單,直接貼代碼。

  1. class Content extends React.Component { 
  2.  
  3.   static propTypes = { 
  4.     visible: React.PropTypes.bool 
  5.   }; 
  6.  
  7.   render() { 
  8.     return ( 
  9.       <div className={this.props.visible ? 'visible content' : 'hidden content'}> 
  10.         {this.props.children} 
  11.       </div> 
  12.     ) 
  13.   } 

 

其他組件

通過以上示例可以看出,有了UiElement和PropsHelper類的處理,基本元素組件的實(shí)現(xiàn)是非常簡單的。只需聲明組件所使用的屬性,并使用父類方法編輯和組裝CSS類即可。其他組件如Label,Icon,Image,Grid等,均沿同一思路封裝即可完成。

難點(diǎn)是什么?

在封裝基本元素組件的過程中,我感覺難點(diǎn)在于:

  1. 封裝和抽象元素的共通處理(目前已基本成型)
  2. 管理眾多組件的共通屬性(目前還在摸索中)

看過官方相關(guān)處理的源碼,感覺思路還是大體一致的,這點(diǎn)讓我感覺多了一些自信(๑•̀ㅂ•́)و✧

責(zé)任編輯:龐桂玉 來源: segmentfault
相關(guān)推薦

2016-10-18 21:45:53

Semantic-UIReactJavascript

2016-10-18 21:26:29

Semantic-UIReact架構(gòu)

2016-10-18 21:31:52

Semantic-UIReact構(gòu)造模塊

2020-10-18 21:41:34

軟件設(shè)計(jì)語言開發(fā)

2010-06-13 10:56:13

UML文獻(xiàn)

2012-03-15 10:04:06

移動(dòng)web

2018-01-23 08:24:57

HTTPS服務(wù)器加密

2020-12-11 09:38:49

Shell編程開發(fā)

2012-12-24 08:50:21

iOSUnity3D

2010-09-03 12:55:15

CSSblockinline

2009-07-01 15:08:50

JSP指令和腳本元素

2022-05-11 07:50:15

React UI組件庫前端

2023-05-31 07:29:46

2009-06-25 13:03:48

JSF的UI組件

2021-06-21 15:49:39

React動(dòng)效組件

2020-10-21 08:38:47

React源碼

2023-07-30 14:56:42

ReactJavaScript開發(fā)

2019-07-20 23:30:48

開發(fā)技能代碼

2023-12-28 07:39:58

C#項(xiàng)目框架

2023-09-05 20:17:18

typescriptPropTypesreact
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)