Ways to display a pop-up window depending on the user's selection?

I am faced with a challenge concerning my modal and form. I have successfully implemented a feature where clicking on an "Add" button opens the form within the modal. However, I now want to extend this functionality to include editing items created using the form when clicking on the div of a specific item.

My main struggle lies in determining when to render the "Add" items part versus the "Edit" items part in my application. Currently, I utilize context for the modal, which only has a default value of false. The modal opens successfully when either the button or item is clicked, but it defaults to the add form.

Below is the structure of my form:

const AddEditForm = () => {
  const [list, setList] = useContext(DataContext)
  const [modal, setModal] = useContext(DataContext2)
  const { item, setItem, id, updatedItem, setUpdatedItem } =
    useContext(DataContext3)
  const [editForm, setEditForm] = useState(false)
  const STATUS = ['-', 'todo', 'inProgress', 'done']
  const USERS = ['-', 'Unassigned', 'JD', 'AJ', 'SS']

  const toggleOnClick = () => {
    addItem()
    setModal(!modal)
  }

  const toggleOnClickEdit = () => {
    UpdateItem()
    setModal(!modal)
  }

  // Rest of the code remains the same...

}

I hope this solution can potentially benefit others facing a similar dilemma!

Answer №1

  1. Implement a click function within your HTML modal to pass current data.

  2. Within the clicked function, assign the form data to the parameters passed from the modal.

    clicked(data){ this.editForm = this.fb.group({ title: item.title, status: item.status, user: item.user, description: item.description }) }

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

Sending an array of styles to a React component

I am facing a challenge in passing an array of styles to a React Component using postcss-loader and css modules. In my webpack configuration file for compiling CSS files, I have the following setup: loaders: [ 'style-loader', 'css ...

Merging two arrays upon the fulfillment of a promise in Vue.js

Within my code, I am storing data in the variable this.roomsData.room_photos [ { "url": "https://api.example.com/uploads/609ee58907166.jpg" }, { "url": "https://api.example.com/uploads/609ee5898ba19.jpg" }, { "u ...

Creating a React App Production Error Boundary that is mapped to the source code involves following a few steps to

I have implemented an error boundary component to effectively capture react errors, which is working fine. However, I am facing an issue in the production app where the logging seems inefficient due to the unorganized component stack structure: \n ...

Using JQuery to load elements and applying Select2 functionality

I'm currently utilizing select2 in a project, and it's functioning properly by initializing the select2 using: $(function() { $(".elegible").select2(); }); The problem arises when I attempt to apply the function to elements loaded through jQ ...

Opinion sought: Which is better - creating a CustomWidget or tweaking the CSS? Implementing Surveyjs in conjunction with Next.js and Material UI

Seeking guidance on a current project scenario: Working with a nextjs app using material ui v5 with a custom theme This theme is utilized across multiple apps to maintain consistent look and feel Goal is to create survey forms through the creator tool ...

Adding automatic hyphens in dates within React

My goal is to create a date field in React, similar to the one on this page, with the date format of yyyy/mm/dd. This is my current approach: const [date_of_birth,setDateofBirth] = useState(""); const handleChangeDOB = (e) => { let value = e.target ...

I want to apply a transition property to my anchor tag so that it changes color smoothly when transitioning from being visited to being hovered over

Here is the HTML content for the Main Page: <body> <div class="container"> <header> <nav class="nav-items"> <ul> <li id="li_1"><a href="ind ...

Using a conditional statement within a map function in React

Is there a way to map a function to listItems only if the condition is true, and skip it otherwise? I've tried different approaches but I don't want to prematurely return before completing the list. Here's my code snippet: const list ...

Discovering the significance of a function's scope

I'm a bit confused about how the answer is coming out to be 15 in this scenario. I do understand that the function scope of doSomething involves calling doSomethingElse, but my calculation isn't leading me to the same result as 15. function doSo ...

VueJS component fails to properly sanitize the readme file, as discovered by Marked

Could someone explain why the output from the compiledMarkdown function is not sanitized, resulting in unstyled content from the markdown file? <template> <div style="padding:35px;"> <div v-html="compiledMarkdown" ...

Using React alongside Three.js and OrbitControls across numerous canvases

I've successfully created a React + Three.js sandbox with multiple canvases displaying simple tetrahedron models. However, I am facing an issue where all instances are using Three.OrbitControls in the same way, causing all models to capture mouse even ...

"Exploring the power of Nextjs Server-Side Generation with 10 million

I am working on a Next.js application that utilizes an API to fetch 10 million posts. I am considering using the SSG method for this purpose, but I am unsure if it is the standard approach. Additionally, I may need to add new posts dynamically in the fut ...

Increase the character count when two spans contain data

I am currently working with the code provided below. The user interface allows users to choose either a single date or a range of dates. These dates are displayed within span tags, with a "-" symbol intended to be shown if there is a valid toDate. However, ...

Leveraging Material-ui to craft an inline form incorporating labels, text fields, select dropdowns, and buttons, reminiscent of the style seen in

I'm having difficulty creating an inline form with Material-UI and React that looks like the example below: https://i.stack.imgur.com/SiOW3.png Bootstrap HTML snippet To achieve the above, I used the following HTML snippet. You can test it out on W3 ...

Exploring the MongoDB database for values not currently present in the array

Here is an array of values. Some of these values are already stored in the MongoDB database, while others are not. For example, here are the values currently in the database: [ {"id":100}, {"id":500}, {"id":606}, ...

Retrieve the most recently added item in the Material-ui Autocomplete component

Currently, I am incorporating the material-ui Autocomplete component with multiple selection feature. In one specific scenario, I require the ability to retrieve only the value of a newly added item. Despite utilizing the onChange listener, the event pro ...

How to Access Nested Arrays in ReactJS

As a ReactJS beginner, I've been making progress with my project. However, I've hit a roadblock that seems to be my final hurdle. Here's what I'm trying to achieve: TV Show - Simpsons Name: Bart Simpson, Gender: Male Name: Homer Simp ...

How to target the TextField component in material-ui v1

While working on a React app with material-ui v1, I encountered an issue when trying to programmatically use the .focus() method on a TextField. The ref property on the TextField keeps returning null, making it difficult to reference and call .focus() wh ...

Storing User Information in Cookies using jQuery

My current jQuery script is linked to a button and it functions by toggling font styles on a website by enabling or disabling a stylesheet upon clicking the button. Now, I want to enhance this functionality by saving user preference to a cookie so that the ...

Transitioning from AngularJS to Angular: Updating the factory prototype

I've been in the process of migrating from AngularJS to Angular and have made a lot of progress. However, I am currently facing challenges with factories and prototypes. In this context, the Card object represents a playing card. function Car ...