Watts-Lab / researcher-portal

deliberation lab collaborator tools
0 stars 0 forks source link

Explore higher level form libraries for the editor popups #45

Closed JamesPHoughton closed 1 month ago

JamesPHoughton commented 1 month ago

As a developer, I want to take advantage of other people's work to make forms easy, so that I don't have to write so much code, and i have more confidence that the code is robust.

The AddPopup.tsx file currently does a lot of manual work to make the form elements that get filled in. We can probably tighten this up (and improve form validation) using a form library. There are a number of different libraries we could choose: https://medium.com/@ansonch/5-best-libraries-to-develop-react-js-forms-2024-6a68f43c3187

My instinct at this point is to use something that interacts with our existing types and zod. From the perspective of brining on new people, it would make sense for it to be something popular, to increase the likelihood that our team has used it in the past, or that they can reuse their knowledge on other projects in the future.

Tasks

christabusho commented 1 month ago

Current Approach:

So far, we are handling the form management using the react hook useState() to manage state and side effects of various form inputs and selected options. For example, selectedOption, nameValue, durationValue, and elementValues (in AddPopup.tsx) are all state variables managed with useState.

Then, we use event handlers handleNameChange, handleDurationChange, handleSelectChange, and handleInputChange to handle user interactions with the form inputs. These functions update the corresponding state variables with the new values.

The handleSave function is used to handle form submission. It checks the saveType and performs the appropriate action such as adding a stage, editing a stage, adding an element, editing an element, deleting an element, or deleting a stage. After performing the action, it updates the treatment state and stores the updated treatment in local storage.

Cons:

Other options: Higher level form libraries

Formik

It’s a popular library for building forms. It provides a “Formik” component that you can use to create your form. This component takes care of managing the form's state including the form values, the form submission, and form validation.

Pros:

Cons: Formik can be a bit complex and difficult to learn.

React-Hook Forms

React Hook Form is a lightweight and efficient library for managing forms in React. It leverages React hooks and the HTML form validation API to provide a simple and intuitive API for form management.

Pros:

Cons: The error messages provided by React Hook Form can sometimes be vague or difficult to understand, which can make debugging more challenging.

JamesPHoughton commented 1 month ago

Looks like React-hook-forms is the way to go then! I've created an issue here: https://github.com/Watts-Lab/researcher-portal/issues/48 for the task of converting what we have to use that library. Would you like to fill out a description of the task to be done in the issue, and take a crack at it? The issue should probably start with a description of the high level goal, and end with a list of items that will help us know it's implemented correctly. I'll let you fill in the details, as practice using issues to document tasks. =)

JamesPHoughton commented 1 month ago

Do you think this issue is ready to close, now that we've made the decision?