Search results

Set the disabled state in React RadioButton component

25 Jan 2023 / 1 minute to read

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.jsx
index.html
index.css
app.tsx
Copied to clipboard
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.
function App() {
    let radioInstance1;
    let radioInstance2;
    let radioInstance3;
    function changeOption1() {
        document.getElementById('text').innerText = 'Selected : ' + radioInstance1.label;
    }
    function changeOption2() {
        document.getElementById('text').innerText = 'Selected : ' + radioInstance2.label;
    }
    function changeOption3() {
        document.getElementById('text').innerText = 'Selected : ' + radioInstance3.label;
    }
    return (<ul>
      <li><div id="text">Selected : Option 1</div></li>
      <li><RadioButtonComponent label="Option 1" name="default" checked={true} change={changeOption1} ref={radio1 => radioInstance1 = radio1}/></li>
      <li><RadioButtonComponent label="Option 2" name="default" disabled={true} change={changeOption2} ref={radio2 => radioInstance2 = radio2}/></li>
      <li><RadioButtonComponent label="Option 3" name="default" change={changeOption3} ref={radio3 => radioInstance3 = radio3}/></li>
      </ul>);
}
export default App;
ReactDom.render(<App />, document.getElementById('radio-button'));
Copied to clipboard
<!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/20.4.38/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/20.4.38/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>
Copied to clipboard
#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;
}
Copied to clipboard
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.
function App() {
    let radioInstance1: RadioButtonComponent;
    let radioInstance2: RadioButtonComponent;
    let radioInstance3: RadioButtonComponent;
    function changeOption1(): void {
      (document.getElementById('text') as HTMLElement).innerText = 'Selected : ' + radioInstance1.label;
    }
  
    function changeOption2(): void {
        (document.getElementById('text') as HTMLElement).innerText = 'Selected : ' + radioInstance2.label;
    }
  
    function changeOption3(): void {
        (document.getElementById('text') as HTMLElement).innerText = 'Selected : ' + radioInstance3.label;
    }
  
    return (
      <ul>
      <li><div id="text">Selected : Option 1</div></li>
      <li><RadioButtonComponent label="Option 1" name="default"  checked={true} change={changeOption1} ref={radio1 => radioInstance1 = radio1 as RadioButtonComponent}/></li>
      <li><RadioButtonComponent label="Option 2" name="default" disabled={true} change={changeOption2} ref={radio2 => radioInstance2 = radio2 as RadioButtonComponent}/></li>
      <li><RadioButtonComponent label="Option 3" name="default" change={changeOption3} ref={radio3 => radioInstance3 = radio3 as RadioButtonComponent}/></li>
      </ul>
    );
  }
export default App;
ReactDom.render(<App />, document.getElementById('radio-button'));