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)文章
axios請(qǐng)求響應(yīng)數(shù)據(jù)加解密封裝實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了axios請(qǐng)求響應(yīng)數(shù)據(jù)加解密封裝實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03react?事項(xiàng)懶加載的三種方法及使用場(chǎng)景
這篇文章主要介紹了react?事項(xiàng)懶加載的三種方法及使用場(chǎng)景,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07concent漸進(jìn)式重構(gòu)react應(yīng)用使用詳解
這篇文章主要為大家介紹了concent漸進(jìn)式重構(gòu)react應(yīng)用的使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11react實(shí)現(xiàn)阻止父容器滾動(dòng)
這篇文章主要介紹了react實(shí)現(xiàn)阻止父容器滾動(dòng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11react配置webpack-bundle-analyzer項(xiàng)目?jī)?yōu)化踩坑記錄
這篇文章主要介紹了react配置webpack-bundle-analyzer項(xiàng)目?jī)?yōu)化踩坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06react實(shí)現(xiàn)記錄拖動(dòng)排序
這篇文章主要介紹了react實(shí)現(xiàn)記錄拖動(dòng)排序的相關(guān)資料,需要的朋友可以參考下2023-07-07React?Native性能優(yōu)化紅寶書(shū)方案詳解
React?Native?是Facebook在React.js?Conf2015推出的開(kāi)源框架,使用React和應(yīng)用平臺(tái)的原生功能來(lái)構(gòu)建Android和iOS應(yīng)用,這篇文章主要介紹了React?Native性能優(yōu)化紅寶書(shū),需要的朋友可以參考下2024-06-06