React Evergreen is a popular front-end library with a set of React components for building beautiful products as this library is flexible, sensible defaults, and User friendly. Radio Component allows the user to select one option from a set or list of items. We can use the following approach in ReactJS to use the Evergreen Radio Component.
Radio Props:
- id: It is used to denote the id attribute of the radio.
- name: It is used to define the name attribute of the radio.
- label: It is used to denote the label of the radio.
- value: It is used to denote the value attribute of the radio.
- onChange: It is a callback function that is called when the state changes.
- disabled: The radio is disabled when this is set to true.
- checked: The radio is checked when this is set to true.
- size: It is used to denote the size of the radio circle.
- isRequired: The radio gets the required attribute when this is set to true.
- isInvalid: The aria-invalid attribute is true when this is set to true.
- appearance: It is used for the appearance of the checkbox.
RadioGroup Props:
- options: It is used to pass the options for the radios of the Radio Group.
- value: It is used to denote the selected item value when controlled.
- defaultValue: It is used to denote the default value of the Radio Group when uncontrolled.
- onChange: It is a callback function that is called when the state changes.
- label: It is used to denote the label of the radio.
- size: It is used to denote the size of the radio circle.
- isRequired: The radio gets the required attribute when this is set to true.
Creating React Application And Installing Module:
-
Step 1: Create a React application using the following command:
npx create-react-app foldername
-
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
-
Step 3: After creating the ReactJS application, Install the required module using the following command:
npm install evergreen-ui
Project Structure: It will look like the following.
Example: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.
App.js
import React from 'react' import { RadioGroup } from 'evergreen-ui' export default function App() { const [ageGroup, setAgeGroup] = React.useState( '0-10 years' ) return ( <div style={{ display: 'block' , width: 700, paddingLeft: 30 }}> <h4>ReactJS Evergreen Radio Component</h4> <RadioGroup label= "Please select your Age group" value={ageGroup} options={[ { label: '0-10 years' , value: '0-10 years' }, { label: '10-20 years' , value: '0-10 years' }, { label: '20-40 years' , value: '0-10 years' }, { label: '40-60 years' , value: '40-60 years' }, { label: 'Above 60 years' , value: ' Above 60 years' }, ]} onChange={e => setAgeGroup(e.target.value)} /> </div> ); } |
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Reference: https://evergreen.segment.com/components/radio