Adding a checkbox to menu items in a ReactJS application can be accomplished by utilizing specific components

I have successfully created dynamic menus using a recursive function, and they are displaying in the correct order without any issues.

Requirement:

My current goal is to identify the last level of menus and assign a checkbox with the value corresponding to their respective id {item.id}.

Example:

For the first menu item one,

 -> [Checkbox with value 1.1.1] One-One-One
 -> [Checkbox with value 1.1.2] One - one - two
 -> [Checkbox with value 1.1.3] One - one - three

For the second menu item two,

 -> [Checkbox with value 2.1] Two - one

The objective is to replicate this structure for all menu items up to the sixth menu item six.

In summary, I need to identify the last hierarchical level of children, as shown in the above example, and associate a checkbox with the corresponding id value.

Please refer to the code snippet below to assist me in achieving the desired outcome of adding checkboxes at the last level.

Note: The checkboxes must be displayed in line with the last level elements, similar to the provided example, with checkbox values matching their respective id.

This requirement arises from having multiple submenus where the last level contains filter values. When a checkbox is clicked, the value is passed to an api to retrieve related data associated with the selected menu item. However, my immediate concern is creating checkboxes that correspond to each menu item.

Answer №1

Based on my interpretation, the requirement is to include a checkbox for the last child element. This can be accomplished by verifying if the element has any children or not.

Here are the modifications that were made to incorporate the checkbox:

render() {
  
  let hasChildren = this.props.children ? false : true;
  
  return (
    <Fragment>
      <Button onClick={this.toggle}>{this.props.name}</Button>
      <Fragment>
        {hasChildren ? <Input type="checkbox" /> : ''}
      </Fragment>
      <Collapse isOpen={this.state.isOpen}>
        <MenuItemContainer menuItems={this.props.children} />
      </Collapse>
    </Fragment>
  );
}

For further details, you can access the plnkr link provided.

I trust this explanation proves beneficial :)

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

The width of the navigation bar adjusts after it becomes fixed to the top of the page

When scrolling past the navigation bar, it sticks to the top but expands beyond the container width. How can this issue be resolved so that the navigation bar retains its original width? Check out my code on GitHub: https://github.com/Archiie/MyPortfolio ...

Conceal a request URL within JavaScript through the use of Laravel/Ajax

I have an idea that I'm not sure is great or even feasible, but I really need to make it work. I am attempting to obfuscate a URL that needs to be used in a Javascript function as a parameter. This is what I currently have: <script> myFunction ...

Generate a JSON object from a given JSON template

Hello, I am currently working on creating a JSON instance from a given JSON schema. I would greatly appreciate a Typescript solution, but I am open to any other suggestions as well. Below is a sample of the schema file: sample-schema.json. My goal is to ...

The -webkit-linear-gradient effect can lead to noticeable banding issues on Chrome and Safari browsers

The title pretty much sums it up. The first image below is a screenshot of a page that is about 8000 pixels tall, taken in the most recent version of Chrome: On the other hand, this image depicts a different page (with the same CSS), which is only about 8 ...

Is it possible to exclusively focus on the specified data attributes?

Is there a method to specifically target the 'data-season' attribute with the value "winter"? This is my HTML: <li data-season="summer">summer <ul class="groups"> <li data-item="beach">beach</li> <li data-item ...

Is it possible to store dat.gui presets for controls that are dynamically added?

I have a dynamic dat.gui interface where I add controls, but the "save settings" feature doesn't seem to recognize them. var mygui = new dat.GUI(); mygui.remember(mygui); // Example of adding a control in the standard way mygui.control1 = 0.0; var c ...

Issue with React Context Provider failing to set value

I am currently diving into React context hooks and encountering an issue that is puzzling me. I have established a user context with the simple string message of "hello user" as follows: import { useContext, createContext } from "react" export ...

Attempting to retrieve an element by its ID from a div that was dynamically loaded using AJAX

I am having trouble retrieving the value of an element with getElementById from a div that is loaded via ajax. Let me explain: In 'example.php' I have the following JavaScript code: <script type= "text/javascript"> var page=document.getE ...

Is the toString() method explicitly invoked by Number() if the value is not of type number or string? (such as a function)

Looking for clarification on the behavior of parseInt() compared to the Number() constructor called as a function. I want to confirm if this is reliable and if there's an official reference to support it. Below is sample code: let adder = (function ...

Tips for adjusting the padding on the body of a webpage

I am experiencing an unexpected white padding issue on my homepage at , and I have been unable to identify the source in my CSS file. If anyone can help me solve this problem, you would truly be a hero! ...

Position the colored div on the left side of the page

Here is the CSS I am currently using... <style type="text/css"> .widediv{width:1366px;margin-left:auto;margin-right:auto} </style> This CSS code helps me create my webpage : <body><div class="widedivv"> <!-- Content go ...

Update the background image URL by setting it as the value of an input field

There are three elements in play here: an input field, a span element, and a div with a background image property. <span>Go</span><input id="ImageUrl"/> <div id="Image"></div> I am working on a script where when a user ente ...

Stretch background image with html2Canvas in Chrome

I am currently using html2canvas to generate an image of a canvas with a background. It is working fine in Firefox, but in Chrome, the background is being vertically stretched. I am unsure of what the issue may be. Here is the link to my fiddle: http://j ...

Learn how to properly register the order of checkboxes in AngularJS

I have a unique requirement for my webapp where I need to display the order in which checkboxes are checked. I came up with the following code to achieve this functionality: $scope.updateNumbers = function(id, checked, inputs) { if (checked === true) ...

How to apply a series of spaces using span/tspan with jquery/javascript

Check out this fiddle: http://jsfiddle.net/jzLu4toe/3/ <span id='tghj'></span> $('#tghj').text('Hey There'); I'm facing an issue where I need to insert multiple spaces inside a span element. ...

Show mathematical equations formatted using Latex or asciiMath

Looking for a way to transform user input from Latex or AsciiMath into a math formula using MathJax in react. Facing challenges with the implementation, especially since the input will vary depending on the user. Any suggestions on how to get started? ...

There appears to be an issue with Javascript's ability to handle JSON API requests

I'm currently working on a webpage that utilizes the openweathermap API to showcase the user's city and local temperature. Unfortunately, I'm encountering an issue where the JSON API is not being processed and nothing is happening. Despite r ...

Is it possible for me to transform this code into a useful helper function?

I am looking to optimize this conditional code by converting it into a helper function using a switch statement instead of multiple if statements. How can I achieve this in a separate file and then import it back into my component seamlessly? import { ...

"Although the substring function is accurately counting characters, it seems to be displaying inaccurately within

I am attempting to display the initial 45 characters of a string using the substring method <div className="well-Font--medium"> {!props.customer_details[item.key] ? `${item.description.substring(0, 45)}...` : ''} </div> Upon v ...

What is the purpose of encasing the routes within the <Switch> element in react-router, when they seem to function perfectly fine without it

Currently utilizing react-router-dom version 5.2.0. As a practice, I typically enclose all my Route components within a Switch component. For instance: const MyRoutes = () => ( <Switch> <Route path='/route1' exact ...