What is the best way to display a checkbox with text inside using Reactjs?

I am using an Ant Design checkbox in my project.

This is how my code looks:

<Checkbox checked={this.state.D} onChange={(e)=> this.setState({ D: e.target.checked})}>D</Checkbox>
<Checkbox checked={this.state.L} onChange={(e)=> this.setState({ L: e.target.checked})}>L</Checkbox>
<Checkbox checked={this.state.M} onChange={(e)=> this.setState({ M: e.target.checked})}>M</Checkbox>
<Checkbox checked={this.state.M1} onChange={(e)=> this.setState({ M1: e.target.checked})}>M</Checkbox>
<Checkbox checked={this.state.J} onChange={(e)=> this.setState({ J: e.target.checked})}>J</Checkbox>

When I run the code, I see this output:

https://i.sstatic.net/XL9LN.png

However, I would like it to display text inside the checkboxes, similar to this:

https://i.sstatic.net/auKyv.png

Could you please advise me on how to achieve this desired rendering?

Answer №1

By placing text within the Checkbox element in antd, it is similar to using a Radio.Group.

<Radio.Group defaultValue="a" size="large">
  <Radio.Button value="a">Hangzhou</Radio.Button>
  <Radio.Button value="b">Shanghai</Radio.Button>
  <Radio.Button value="c">Beijing</Radio.Button>
  <Radio.Button value="d">Chengdu</Radio.Button>
</Radio.Group>;

https://i.sstatic.net/eWSsY.png

If you wish to use "Round Buttons" like in the example, you will need to create a custom component using <Button shape="circle"/> and handle its clicked state.

https://i.sstatic.net/sRn0b.png

It's worth noting that creating such a component goes beyond the scope of this inquiry, even though it's quite straightforward.

Demo:

https://codesandbox.io/s/antdstart-4sdyp?fontsize=14

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

From traditional relational databases to MongoDB/Mongoose database design

Recently, I ventured into using mongoDB and mongoose for a new node.js application. Coming from a background of relational databases, I find it challenging to adjust to the mongoDB/noSQL approach, particularly concerning denormalization and the absence of ...

Angular 9 - Auto-adjust the height of the text box as the user types, returning to its original size when no longer in focus

I need a solution where an input field's height adjusts to display the user's entry, then returns to normal size when they click away. It should function similar to this example image: https://i.stack.imgur.com/yKcik.png Once the user enters th ...

Is it better to choose "_this" over "self" in JavaScript?

The Airbnb JavaScript Style Guide recommends using "_this" when saving a reference to the object, as seen in their Style Guide. // bad function() { var self = this; return function() { console.log(self); }; } // bad function() { var that = th ...

What steps can be taken to address issues with the counting system in a lootbox opening

I am currently developing a virtual loot box simulator and have encountered a challenging issue. My goal is to track the quantity of Normals, Legendaries, Epics, and Rares obtained by the user. However, I am facing a problem where instead of updating the c ...

The paragraph tag remains unchanged

I am currently working on developing a feature that saves high scores using local storage. The project I'm working on is a quiz application which displays the top 5 scores at the end, regardless of whether the quiz was completed or not. However, I&apo ...

Is there a way to retrieve the previous value in an input field's onChange event?

I am working with inputs in a React project and have assigned a function to their onChange event. While I have been able to access the current value, I am now looking for a way to retrieve the previous value as well. The reason I need the old value is bec ...

Adding a class to the body element in an HTML file using AngularJS

Greetings! Currently I am in the process of developing a web application using AngularJS SPA, which supports both English and Arabic languages. I am facing an issue with adding RTL and LTR properties by applying classes to the body HTML element in my app.j ...

What is the method for detecting when the Enter key has been pressed in a field?

I often have several fields to fill out, with the user typically pressing 'enter' on one of the two main ones. I'd like to determine which field 'enter' was pressed on, but I'm not very familiar with JavaScript. Can anyone gui ...

"Partially loaded" when document is ready

Is there a way for me to trigger a function once the element identified by #container has finished loading in the DOM? Instead of waiting for the entire DOM to load using document.ready(), I'd like to start populating #container right after it's ...

Issue: Catching errors in proxy function calls

I am currently using Vue 3 along with the latest Quasar Framework. To simplify my API calls, I created an Api class as a wrapper for Axios with various methods such as get, post, etc. Now, I need to intercept these method calls. In order to achieve this ...

Utilizing Material UI to create a versatile and adaptable grid layout

Is there a way to set the container direction as "row" above md size screens, and "column" below md size screens? How can I achieve this? <Grid container direction = "row"(in large screens)/direction = "column"(in small screens)> I have attempted th ...

What is the reason for updating only one out of three charts using react-chartjs-2?

Recently, I came across an issue with a modal that includes a text field for recording numerical values. These recorded values are then passed through a loop to populate an array based on the input. Subsequently, these values get updated in 3 different gra ...

The CSS Menu is not displaying correctly on Internet Explorer 8

My horizontal menu appears distorted on IE8 (and possibly older versions) as shown in the first image. However, there are no issues with the latest versions of Safari, Firefox, and Chrome as depicted in the second image. Any suggestions on why this is happ ...

The Page is Not Able to Scroll

Occasionally, my app stops allowing scrolling of the entire page length. Everything will be working fine for a while, but then out of nowhere, the page suddenly becomes un-scrollable and you can only interact with the section currently visible on the scree ...

What is the process for deploying AppBar Material UI?

As a newcomer to React Material UI, I am facing a challenge while trying to deploy an AppBar. I am unsure of how to include child components within this NavBar. My objective is to deploy the AppBar upon clicking the three-horizontal-lines menu button. Belo ...

The default value is not displayed in the Angular dropdown menu

When using regular html select menus, if you create an option element with selected and disabled attributes and provide text for that option, the text will be displayed by default in the select menu. Below is a basic example of HTML code: <select name= ...

How can I make a Bootstrap video embed fill the entire width of the screen?

I'm struggling to integrate a YouTube video into my webpage with Bootstrap, but I want it to span the entire width of the page. Here's the HTML code: <iframe class="embed-responsive-item youtube" src="https://www.youtube.com/em ...

Data not being displayed in dynamic <table>

Need help creating a dynamic table that displays data inserted in a specific format. {headers: ["header1", "header2"], rows: [["data1", "data2"], ["moredata", "wowoso muchdata"]]} The 'rows' consists of arrays where each array represents a row ...

Improving the efficiency of JSON data retrieval in JavaScript

I possess a hefty 10MB JSON file with a structured layout comprising 10k entries: { entry_1: { description: "...", offset: "...", value: "...", fields: { field_1: { offset: "...", description: "...", ...

Is there a way to include an image in a serialized file?

What is the method to include image_form into form in Django? form - form.serialize() image_form - image $('#id_submit').click(function(e) { e.preventDefault(); var form = $('form').serialize(); image_form = $("#id_image")[0].f ...