react实现Radio组件的示例代码

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了react实现Radio组件的示例代码脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文旨在用最清楚的结构去实现一些组件的基本功能。希望和大家一起学习,共同进步

效果展示:

测试组件:

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("此时选中的是:"+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 &#63; 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;
    }
  }
}

到此这篇关于react实现Radio组件的示例代码的文章就介绍到这了,更多相关react实现Radio组件内容请搜索脚本宝典以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本宝典!

脚本宝典总结

以上是脚本宝典为你收集整理的react实现Radio组件的示例代码全部内容,希望文章能够帮你解决react实现Radio组件的示例代码所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:react