Search results

Set the disabled state in React RadioButton component

RadioButton component can be enabled/disabled by giving disabled property. To disable RadioButton component, the disabled property can be set as true.

The following example illustrates how to disable a radio button and the selected one is displayed using change event.

Source
Preview
app.tsx
index.html
index.css
app.jsx
import { enableRipple } from '@syncfusion/ej2-base';
import { RadioButtonComponent, ChangeEventArgs } from '@syncfusion/ej2-react-buttons';
import * as React from 'react';
import * as ReactDom from 'react-dom';

enableRipple(true);

// To render RadioButton.
class App extends React.Component<{}, {}> {
    public radioInstance1: RadioButtonComponent;
    public radioInstance2: RadioButtonComponent;
    public radioInstance3: RadioButtonComponent;
    public changeOption1: any = () => {
      (document.getElementById('text') as HTMLElement).innerText = 'Selected : ' + this.radioInstance1.label;
    }
  
    public changeOption2: any = () => {
        (document.getElementById('text') as HTMLElement).innerText = 'Selected : ' + this.radioInstance2.label;
    }
  
    public changeOption3: any = () => {
        (document.getElementById('text') as HTMLElement).innerText = 'Selected : ' + this.radioInstance3.label;
    }
  
    public render() {
      return (
        <ul>
        <li><div id="text">Selected : Option 1</div></li>
        <li><RadioButtonComponent label="Option 1" name="default"  checked={true} change={this.changeOption1} ref={radio1 => this.radioInstance1 = radio1 as RadioButtonComponent}/></li>
        <li><RadioButtonComponent label="Option 2" name="default" disabled={true} change={this.changeOption2} ref={radio2 => this.radioInstance2 = radio2 as RadioButtonComponent}/></li>
        <li><RadioButtonComponent label="Option 3" name="default" change={this.changeOption3} ref={radio3 => this.radioInstance3 = radio3 as RadioButtonComponent}/></li>
        </ul>
      );
    }
  }
ReactDom.render(<App />, document.getElementById('radio-button'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Button</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
	<link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
        <div id='radio-button'>
            <div id='loader'>Loading....</div>
        </div>
</body>

</html>
#loader {
	color: #008cff;
	height: 40px;
	left: 45%;
	position: absolute;
	top: 45%;
	width: 30%;
}

.e-radio-wrap {
  margin-top: 18px;
}

li {
  list-style: none;
  margin: 25px auto;
}

#text {
  font-size: 16px;
}
import { enableRipple } from '@syncfusion/ej2-base';
import { RadioButtonComponent } from '@syncfusion/ej2-react-buttons';
import * as React from 'react';
import * as ReactDom from 'react-dom';
enableRipple(true);
// To render RadioButton.
class App extends React.Component {
    constructor() {
        super(...arguments);
        this.changeOption1 = () => {
            document.getElementById('text').innerText = 'Selected : ' + this.radioInstance1.label;
        };
        this.changeOption2 = () => {
            document.getElementById('text').innerText = 'Selected : ' + this.radioInstance2.label;
        };
        this.changeOption3 = () => {
            document.getElementById('text').innerText = 'Selected : ' + this.radioInstance3.label;
        };
    }
    render() {
        return (<ul>
        <li><div id="text">Selected : Option 1</div></li>
        <li><RadioButtonComponent label="Option 1" name="default" checked={true} change={this.changeOption1} ref={radio1 => this.radioInstance1 = radio1}/></li>
        <li><RadioButtonComponent label="Option 2" name="default" disabled={true} change={this.changeOption2} ref={radio2 => this.radioInstance2 = radio2}/></li>
        <li><RadioButtonComponent label="Option 3" name="default" change={this.changeOption3} ref={radio3 => this.radioInstance3 = radio3}/></li>
        </ul>);
    }
}
ReactDom.render(<App />, document.getElementById('radio-button'));