W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
表示兩種相互對立的狀態(tài)間的切換,多用于觸發(fā)「開/關(guān)」。
綁定value
到一個Boolean
類型的變量。可以使用onText
屬性與offText
屬性來設(shè)置開關(guān)的文字描述,使用onColor
屬性與offColor
屬性來設(shè)置開關(guān)的背景色。
render() {
return (
<div>
<Switch
value={true}
onText=""
offText="">
</Switch>
<Switch
value={true}
onColor="#13ce66"
offColor="#ff4949">
</Switch>
</div>
)
}
設(shè)置onValue
和offValue
屬性,接受Boolean
, String
或Number
類型的值。
constructor(props) {
super(props);
this.state = {
value: 100,
}
}
render() {
return (
<Tooltip
placement="top"
content={
<div>Switch value: {this.state.value}</div>
}
>
<Switch
value={this.state.value}
onColor="#13ce66"
offColor="#ff4949"
onValue={100}
offValue={0}
onChange={(value)=>{this.setState({value: value})}}
>
</Switch>
</Tooltip>
)
}
設(shè)置disabled
屬性,接受一個Boolean
,設(shè)置true
即可禁用。
render() {
return (
<div>
<Switch
value={true}
onText=""
offText=""
disabled>
</Switch>
<Switch
value={true}
disabled>
</Switch>
</div>
)
}
設(shè)置allowFocus
屬性,接受一個Boolean
,設(shè)置true
即可激活。
render() {
return (
<div>
<Switch
allowFocus={true}
onFocus={()=>console.log('focus')}
onBlur={()=>console.log('blur')}
>
</Switch>
</div>
)
}
參數(shù) | 說明 | 類型 | 可選值 | 默認(rèn)值 |
---|---|---|---|---|
disabled | 是否禁用 | boolean | — | false |
width | switch 的寬度(像素) | number | — | 58(有文字)/ 46(無文字) |
onIconClass | switch 打開時所顯示圖標(biāo)的類名, 設(shè)置此項會忽略 onText |
string | — | — |
offIconClass | switch 關(guān)閉時所顯示圖標(biāo)的類名, 設(shè)置此項會忽略 offText |
string | — | — |
onText | switch 打開時的文字 | string | — | ON |
offText | switch 關(guān)閉時的文字 | string | — | OFF |
onValue | switch 打開時的值 | boolean / string / number | — | true |
offValue | switch 關(guān)閉時的值 | boolean / string / number | — | false |
onColor | switch 打開時的背景色 | string | — | #20A0FF |
offColor | switch 關(guān)閉時的背景色 | string | — | #C0CCDA |
name | switch 對應(yīng)的 name 屬性 | string | — | — |
allowFocus | 允許 switch 觸發(fā) focus 和 blur 事件 | boolean | boolean | — |
事件名稱 | 說明 | 回調(diào)參數(shù) |
---|---|---|
onChange | switch 狀態(tài)發(fā)生變化時的回調(diào)函數(shù) | value |
onBlur | switch 失去焦點時觸發(fā),僅當(dāng) allow-focus 為 true 時生效 |
event: Event |
onFocus | switch 獲得焦點時觸發(fā),僅當(dāng) allow-focus 為 true 時生效 |
event: Event |
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: