react實(shí)現(xiàn)Radio組件的示例代碼
本文旨在用最清楚的結(jié)構(gòu)去實(shí)現(xiàn)一些組件的基本功能。希望和大家一起學(xué)習(xí),共同進(jìn)步
效果展示:

測(cè)試組件:
class Test extends Component {
constructor(props) {
super(props)
this.state = {
active:1
}
}
onGroupChange(value) {
this.setState({
active: value
})
}
render() {
return (
<div>
<RadioGroup onChange={this.onGroupChange.bind(this)} active={this.state.active}>
<Radio value={1}>使用余額支付</Radio>
<Radio value={2}>使用微信支付</Radio>
</RadioGroup>
<Button onClick={()=>{
console.log("此時(shí)選中的是:"+this.state.active)
}}>下一步</Button>
</div>
)
}
}
export default Test;
RadioGroup:
import React, { Component } from 'react';
class RadioGroup extends Component {
handleActiveChange(value) {
console.log(`${value}被選中了`)
this.props.onChange(value)
}
render() {
return (
<div>
{
React.Children.map(this.props.children, child => {
let isActive = this.props.active === child.props.value ? true : false
return React.cloneElement(child, {
label: child.props.children,
value: child.props.value,
active: isActive,
onClick: this.handleActiveChange.bind(this)
})
})
}
</div>
)
}
}
export default RadioGroup;
Radio.jsx:
import React, { Component } from 'react';
import "./radio.scss"
class Radio extends Component {
render() {
return (
<div className="radio-wrap" onClick={this.props.onClick.bind(this,this.props.value)}>
<div className="left">
<div className={`circle ${this.props.active === true ? 'active' : ''} `}>
<div className="fork"></div>
</div>
<div className="label">{this.props.label}</div>
</div>
</div>
)
}
}
export default Radio;
Radio.scss:
.radio-wrap {
height: 40px;
background-color: #ffffff;
display: flex;
align-items: center;
padding: 0px 30px;
&:active {
background-color: rgb(221, 221, 221);
}
.left {
display: inline-block;
.circle {
display: inline-block;
height: 22px;
width: 22px;
box-sizing: border-box;
border: 1px solid #c5c9cd;
border-radius: 50%;
background-color: #ffffff;
position: relative;
}
.active{
background-color: #1eb94a;
.fork {
height: 12px;
width: 5px;
border-right: 1.5px solid #ffffff;
border-bottom: 1.5px solid #ffffff;
position: absolute;
top: 40%;
left: 50%;
transform: translate(-50%, -50%) rotate(45deg);
}
}
.label {
vertical-align: top;
margin-left: 10px;
display: inline-block;
height: 22px;
line-height: 22px;
font-size: 14px;
}
}
}
到此這篇關(guān)于react實(shí)現(xiàn)Radio組件的示例代碼的文章就介紹到這了,更多相關(guān)react實(shí)現(xiàn)Radio組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
react ant-design Select組件下拉框map不顯示的解決
這篇文章主要介紹了react ant-design Select組件下拉框map不顯示的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
react循環(huán)數(shù)據(jù)(列表)的實(shí)現(xiàn)
這篇文章主要介紹了react循環(huán)數(shù)據(jù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
ReactNative中使用Redux架構(gòu)總結(jié)
本篇文章主要介紹了ReactNative中使用Redux架構(gòu)總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
React報(bào)錯(cuò)信息之Expected?an?assignment?or?function?call?and?
這篇文章主要介紹了React報(bào)錯(cuò)之Expected?an?assignment?or?function?call?and?instead?saw?an?expression,下面有兩個(gè)示例來(lái)展示錯(cuò)誤是如何產(chǎn)生的,需要的朋友可以參考下2022-08-08
40行代碼把Vue3的響應(yīng)式集成進(jìn)React做狀態(tài)管理
這篇文章主要介紹了40行代碼把Vue3的響應(yīng)式集成進(jìn)React做狀態(tài)管理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
React中的render何時(shí)執(zhí)行過(guò)程
這篇文章主要介紹了React中的render何時(shí)執(zhí)行過(guò)程,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04

