Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Create RadioGroup with RadioButtons using React with TypeScript

Tiempo de lectura: 2 minutos

Let’s create a RadioGroup component composed of RadioButtons.

First, we create the RadioGroup component.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import React, { useState } from 'react';
import RadioGroup from './RadioGroup'; // Adjust the path according to your file structure
import RadioButton from './RadioButton'; // Adjust the path according to your file structure
const YourComponent = () => {
const [gender, setGender] = useState(""); // State to handle gender selection
return (
<RadioGroup
title={"Example for DevCodeLight"}
radioButtons={[
<RadioButton
key={"Male"}
id={"Male"}
label={"Male"}
value={"Male"}
checked={gender === "Male"}
onChange={(event) => {
const newValue = event.target.value;
setGender(newValue); // Update the state with the new selection
}}
/>,
<RadioButton
key={"Female"}
id={"Female"}
label={"Female"}
value={"Female"}
checked={gender === "Female"}
onChange={(event) => {
const newValue = event.target.value;
setGender(newValue); // Update the state with the new selection
}}
/>
]}
/>
);
};
export default YourComponent;
import React, { useState } from 'react'; import RadioGroup from './RadioGroup'; // Adjust the path according to your file structure import RadioButton from './RadioButton'; // Adjust the path according to your file structure const YourComponent = () => { const [gender, setGender] = useState(""); // State to handle gender selection return ( <RadioGroup title={"Example for DevCodeLight"} radioButtons={[ <RadioButton key={"Male"} id={"Male"} label={"Male"} value={"Male"} checked={gender === "Male"} onChange={(event) => { const newValue = event.target.value; setGender(newValue); // Update the state with the new selection }} />, <RadioButton key={"Female"} id={"Female"} label={"Female"} value={"Female"} checked={gender === "Female"} onChange={(event) => { const newValue = event.target.value; setGender(newValue); // Update the state with the new selection }} /> ]} /> ); }; export default YourComponent;
import React, { useState } from 'react';
import RadioGroup from './RadioGroup'; // Adjust the path according to your file structure
import RadioButton from './RadioButton'; // Adjust the path according to your file structure

const YourComponent = () => {
  const [gender, setGender] = useState(""); // State to handle gender selection

  return (
    <RadioGroup
      title={"Example for DevCodeLight"}
      radioButtons={[
        <RadioButton
          key={"Male"}
          id={"Male"}
          label={"Male"}
          value={"Male"}
          checked={gender === "Male"}
          onChange={(event) => {
            const newValue = event.target.value;
            setGender(newValue); // Update the state with the new selection
          }}
        />,
        <RadioButton
          key={"Female"}
          id={"Female"}
          label={"Female"}
          value={"Female"}
          checked={gender === "Female"}
          onChange={(event) => {
            const newValue = event.target.value;
            setGender(newValue); // Update the state with the new selection
          }}
        />
      ]}
    />
  );
};

export default YourComponent;

Now, let’s create a RadioButton component as shown below:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import React from 'react';
import { rojoerror } from '../../constants/Colors'; // Adjust the path according to your file structure
interface RadioButtonProps {
id: string;
label: string;
value: string;
checked: boolean;
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
showError?: boolean; // New property to show error
}
function RadioButton({ id, label, value, onChange, checked, showError }: RadioButtonProps) {
const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
onChange(event);
};
return (
<div>
<input
type="radio"
id={id}
value={value}
checked={checked}
onChange={handleInputChange}
style={{
borderColor: showError ? rojoerror : 'initial',
color: showError ? rojoerror : 'initial',
fontWeight: showError ? 'bold' : 'normal'
}}
/>
<label htmlFor={id} style={{ color: showError ? rojoerror : 'initial' }}>{label}</label>
</div>
);
}
export default RadioButton;
import React from 'react'; import { rojoerror } from '../../constants/Colors'; // Adjust the path according to your file structure interface RadioButtonProps { id: string; label: string; value: string; checked: boolean; onChange: (e: React.ChangeEvent<HTMLInputElement>) => void; showError?: boolean; // New property to show error } function RadioButton({ id, label, value, onChange, checked, showError }: RadioButtonProps) { const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => { onChange(event); }; return ( <div> <input type="radio" id={id} value={value} checked={checked} onChange={handleInputChange} style={{ borderColor: showError ? rojoerror : 'initial', color: showError ? rojoerror : 'initial', fontWeight: showError ? 'bold' : 'normal' }} /> <label htmlFor={id} style={{ color: showError ? rojoerror : 'initial' }}>{label}</label> </div> ); } export default RadioButton;
import React from 'react';
import { rojoerror } from '../../constants/Colors'; // Adjust the path according to your file structure

interface RadioButtonProps {
  id: string;
  label: string;
  value: string;
  checked: boolean;
  onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
  showError?: boolean; // New property to show error
}

function RadioButton({ id, label, value, onChange, checked, showError }: RadioButtonProps) {
  const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    onChange(event);
  };

  return (
    <div>
      <input
        type="radio"
        id={id}
        value={value}
        checked={checked}
        onChange={handleInputChange}
        style={{
          borderColor: showError ? rojoerror : 'initial',
          color: showError ? rojoerror : 'initial',
          fontWeight: showError ? 'bold' : 'normal'
        }}
      />
      <label htmlFor={id} style={{ color: showError ? rojoerror : 'initial' }}>{label}</label>
    </div>
  );
}

export default RadioButton;

Next, let’s use the components as follows:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import React, { useState } from 'react';
import RadioGroup from './RadioGroup'; // Adjust the path according to your file structure
import RadioButton from './RadioButton'; // Adjust the path according to your file structure
const YourComponent = () => {
const [gender, setGender] = useState(""); // State to handle gender selection
return (
<RadioGroup
title={"Example for DevCodeLight"}
radioButtons={[
<RadioButton
key={"Male"}
id={"Male"}
label={"Male"}
value={"Male"}
checked={gender === "Male"}
onChange={(event) => {
const newValue = event.target.value;
setGender(newValue); // Update the state with the new selection
}}
/>,
<RadioButton
key={"Female"}
id={"Female"}
label={"Female"}
value={"Female"}
checked={gender === "Female"}
onChange={(event) => {
const newValue = event.target.value;
setGender(newValue); // Update the state with the new selection
}}
/>
]}
/>
);
};
export default YourComponent;
import React, { useState } from 'react'; import RadioGroup from './RadioGroup'; // Adjust the path according to your file structure import RadioButton from './RadioButton'; // Adjust the path according to your file structure const YourComponent = () => { const [gender, setGender] = useState(""); // State to handle gender selection return ( <RadioGroup title={"Example for DevCodeLight"} radioButtons={[ <RadioButton key={"Male"} id={"Male"} label={"Male"} value={"Male"} checked={gender === "Male"} onChange={(event) => { const newValue = event.target.value; setGender(newValue); // Update the state with the new selection }} />, <RadioButton key={"Female"} id={"Female"} label={"Female"} value={"Female"} checked={gender === "Female"} onChange={(event) => { const newValue = event.target.value; setGender(newValue); // Update the state with the new selection }} /> ]} /> ); }; export default YourComponent;
import React, { useState } from 'react';
import RadioGroup from './RadioGroup'; // Adjust the path according to your file structure
import RadioButton from './RadioButton'; // Adjust the path according to your file structure

const YourComponent = () => {
  const [gender, setGender] = useState(""); // State to handle gender selection

  return (
    <RadioGroup
      title={"Example for DevCodeLight"}
      radioButtons={[
        <RadioButton
          key={"Male"}
          id={"Male"}
          label={"Male"}
          value={"Male"}
          checked={gender === "Male"}
          onChange={(event) => {
            const newValue = event.target.value;
            setGender(newValue); // Update the state with the new selection
          }}
        />,
        <RadioButton
          key={"Female"}
          id={"Female"}
          label={"Female"}
          value={"Female"}
          checked={gender === "Female"}
          onChange={(event) => {
            const newValue = event.target.value;
            setGender(newValue); // Update the state with the new selection
          }}
        />
      ]}
    />
  );
};

export default YourComponent;

Finally, here’s the result:

I hope this helps you. Have a great day!

0

Leave a Comment