React implements the sample code of Radio component

React implements the sample code of Radio component

This article aims to use the clearest structure to implement the basic functions of some components. I hope to learn and make progress together with you.

Effect display:

Test components:

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}>Pay with balance</Radio>
         <Radio value={2}>Use WeChat Pay</Radio>
      </RadioGroup>
      <Button onClick={()=>{
             console.log("The current selection is: "+this.state.active)
           }}>Next</Button>
     </div>
      )
  }
}
export default Test;

RadioGroup:

import React, { Component } from 'react';
class RadioGroup extends Component {
  handleActiveChange(value) {
    console.log(`${value} is selected`)
    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;
    }
  }
}

This is the end of this article about the sample code of implementing Radio component in react. For more relevant content about implementing Radio component in react, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • React error boundary component processing
  • Detailed explanation of react setState
  • Tips for writing concise React components
  • Example of using setInterval function in React
  • Andrew Ng's machine learning exercise: SVM support vector machine

<<:  Detailed explanation of the use and differences of various lock mechanisms in Linux

>>:  How to solve the Mysql transaction operation failure

Recommend

How to use Tencent slider verification code in Vue3+Vue-cli4 project

Introduction: Compared with traditional image ver...

Detailed process of SpringBoot integrating Docker

Table of contents 1. Demo Project 1.1 Interface P...

Example code for implementing the "plus sign" effect with CSS

To achieve the plus sign effect shown below: To a...

Vue implements mobile phone verification code login

This article shares the specific code of Vue to i...

Detailed Example of MySQL curdate() Function

MySQL CURDATE Function Introduction If used in a ...

nginx proxy_cache batch cache clearing script introduction

Preface: I used the official nginx proxy_cache as...

Nginx try_files directive usage examples

Nginx's configuration syntax is flexible and ...

Examples of correct use of maps in WeChat mini programs

Table of contents Preface 1. Preparation 2. Actua...

The difference between method=post/get in Form

Form provides two ways of data transmission - get ...

Four methods of using JS to determine data types

Table of contents Preface 1. typeof 2. instanceof...

Detailed explanation of semiotics in Html/CSS

Based on theories such as Saussure's philosop...

JavaScript to achieve a simple message board case

Use Javascript to implement a message board examp...

A brief discussion on several ways to pass parameters in react routing

The first parameter passing method is dynamic rou...

How to completely uninstall mysql under CentOS

This article records the complete uninstallation ...

Two box models in web pages (W3C box model, IE box model)

There are two types of web page box models: 1: Sta...