r/reactjs • • Nov 24 '20

Show /r/reactjs I made a multi-step form component library! | React Hooks + Context API

533 Upvotes

27 comments sorted by

92

u/Oalei Nov 24 '20

While the execution looks nicely done, I find it confusing that you are using the concept of tabs to represent a wizard.

13

u/_Invictuz Nov 24 '20

Good point, design is definitely something I'm quite inexperienced at. I think I might get rid of the tabs altogether and allow users to create their own custom component with my custom hook for backwards navigation.

Also, I just posted a comment with links to the demo and documentation. Sorry it took me so long!

12

u/SleazyDutcham Nov 25 '20

A timeline visualizer to represent stages in a process might work well

1

u/gaytechdadwithson Nov 25 '20

yep. for many many reason. for starters, the previous and next buttons are all over the place.

15

u/_Invictuz Nov 24 '20

I'm super excited to showcase my first web dev project which took me longer than I'd like to admit. Please check it out and let me know what you think!

Tech Stack and Implementation

This React component library is built entirely with function components and utilizes the Context API and many custom hooks to share logic and state values between the components. It also uses Emotion Styled Components and Emotion Theming for styling. My goal for this library was to make the code as declarative as possible so that it's easy to use and configure.

Challenges

As this is my first React project, let alone my first component library, I still have a lot to learn and many questions to be answered:

  • Currently, my form library provides a fixed overall design (while allowing you to configure the inputs) whereas other form libraries allow you to design your own form and use form logic from their library. Is my library practical in the real world - would people actually use it?

    • I originally planned to use this library to build a modal form but I'm not sure if this design is too limiting for a form component library.

  • How else can I allow users to customize their forms? Should I expose a bunch of styling props for each component like how Chakra UI does it, or should I just allow users to customize an overall theme (size and colour) for the whole form?

  • Is there anything else major, besides accessibility, that I need to work on to make this a complete form component library?

  • Is the library package file size, 500kb, too big?

  • Is it viable that I require Emotion (or maybe styled-components) as a peer dependency? If not, how should one style a component library?

Background

I've been self-learning (part-time) for a while now in order to break into the industry after realizing this is what I love to do. The design was the hardest aspect of this project for me and I'm still wondering if the design is practical. I'm hoping that this project will be enough for me to start applying for jobs.

Feel free to try out the library, leave any feedback or ask me any questions!

22

u/callius Nov 24 '20

Hey there,

This is a great first stab at this project. You should feel very proud. It’s a complex problem to solve, and you’ve made something that could be useful to some people.

There is definitely room for improvement and re-thinking. I’ll address your questions and give you some of my initial thoughts. Hopefully it’s helpful. A lot of the points I’m going to make will cover multiple questions.

  1. I would heavily suggest that you refactor your FormBody component. Right now it handles both business and display logic. This not only makes it difficult to reason about, but it will also paint you into a lot of corners down the line. What if design changes enough that you want to split some of this component’s ui layer out into two separate pieces? What if you want to account for additional work flows in your business logic but keep the ui flow the same? Can you easily change one without affecting the other right now? How about 6 to 12 months down the line?

  2. Building off of that, I would recommend asking yourself what you’re trying to solve here. Is it the how it works of multi-step forms or is it the how it looks? The answer may be both, but I would recommend that your answer be carefully considered.

  3. I would be very wary of adding in a ton of pass through props for styling. That, to me, begins to indicate that something is trying to control something else that it shouldn’t be or that a single Component is trying to control too much at once.

  4. Which gets us to your approach to styling. You are relying heavily on emotion, which is a phenomenal library. Yet, at my work we use styled-components. A lot of other places use straight up CSS. What benefit does forcing us to bundle emotion get us? Even if this solved a use case I had, I wouldn’t incorporate it due to file size alone.

  5. You have crafted this such that it is heavily dependent upon your implementation of Input, Checkbox, Combobox, etc. That works well for your needs, and isn’t necessarily a bad approach for a robust and mature style library. But how would one integrate this into an existing system?

Even beyond that, what if I wanted to include additional information in each step (graphics, explanation text, etc)? Rather than using the inputs themselves as handlers, why not delegate that to the user themselves?

6) As an extension of this, your implementation relies heavily on your own method for form handling. What if your user already has their own form handling solutions (rolled their own, Formik, React Hook Form)? How will these interact?

6) As I said before, multi step forms are very hard and come with a lot of complexity. It would be good to think deeply about the various ways someone might wish to use your component. For example, what if I wish to have step 1 perform a POST to sign a user up or in and then render a different body in step 2 depending on whether they are an existing or new user? Can your component accomplish this flow? What if we want to have different, more complex or a priori validation schema per step?

Ultimately, the two biggest things I would recommend that you think about are:

  1. What singular goal do you want this library to accomplish and accomplish well? It currently acts as a form state management, a component library, and a form wizard manager (distinct from but related to state management). Which is the most important to you and how can you abstract the other pieces away?

  2. Related and following from #1, how can you invert control and delegate to your consumers? They may have their own style library, they may have their own form handlers libraries, they may want to use this in ways that you or I aren’t envisioning right now. The best way to accommodate these different scenarios is by pushing more areas onto the consumer for control. For example, does this library need to handle input errors or can it simply expose an errors object that consumers can use, or perhaps even just throw errors and have client catch them themselves. I don’t know the answer, but I do know that the more a component inverts control, the more flexible it can become.

Hopefully this was helpful. Like I said at the top, this is extremely impressive for a first library attempt. I hope this process, and thinking about the things I’ve mentioned, will help you reach a new understanding in how to architect robust, reusable, and long-lasting pieces of code.

8

u/_Invictuz Nov 25 '20

I can't thank you enough for taking the time and effort into reviewing my codebase and writing such a detailed response. I feel like I've learnt more from your one post than I have in the past few months of building this form component library. Besides trying to learn the technology required to build this library, I always had lingering questions of what a form library is supposed to do. Now I see from your post the different things a library can do and that I need to choose one of them to focus on. Also, my inexperience in utilizing other form libraries really held me back, so I really appreciate that you brought up questions that I hadn't thought of.

I have a couple of questions/thoughts about your points which I'll list here:

  1. When you say that my FormBody component contains business logic, do you mean, for example, logic related to when the input is validated? I'm used to thinking of business logic as real-world business rules/workflows outside of the code, so I may be confused here.

  2. That's an excellent way to summarize the question of what I'm trying to solve, which I could never put into words.

Thank you for imparting all your wisdom in regards to flexible/extensible code as I would have probably taken a long time to get to the level of even considering the points you brought up. I'm sure that I'll be considering your advice long into my career as a developer and I am forever grateful for that. I'm also sure that many others will benefit from reading your post as I did!

2

u/callius Nov 26 '20

Heya,

I’m so glad to hear that you found my comments helpful. Your kind words mean a lot to me, thank you.

As for your first question, I may be using incorrect terms here (my education was self-driven), but I generally use business logic to describe the “how does it work” of things (how do I increase the currentIndex of my array?) versus the “what does it look like” of things (when the currentIndex advances what happens to my progress bar or tabs?)

A second glance over your code looks like I may have overstated the amount that these things are mixed in FormBody. In my defense, my 5 day old kid was asleep on my arm when I wrote my comment.

I was trying to get at the fact that certain logic felt out of place in the component, as if FormBody is trying to manage stuff both up and down. For example, it is managing up in handleNextButtonClick(). This could easily be a goNext() function handed down from useInput().

Which means we could potentially solve a managing down piece too and pull <NextButton> out of this component altogether. This would allow our users to implement their own version by giving them access to goNext() from useInput().

We could also remove a good bit of the code that appears to be handling things in two directions such as activation checking and changing logic too (e.g. activateAndClick()). So long as 1) everywhere that needs that information knows their own index and 2) useInput exposes a clean API for things like goToIndex(), goNext(), goPrevious(), etc.

None of this is intended as nit-picking, and your approach works well for what you’re after. I’m just brainstorming ways that we could think about this in different lights. My goal is always to slim things down, have components do one thing super well (idealistic), and trust consumers (devs, not clients) know what they want (say a progress bar rather than tabs, or a dynamic submit state, etc).

Have a great day and know that you’re doing a great job.

2

u/_Invictuz Nov 26 '20

Thanks for taking another look at my FormBody component. I now get what you're saying about the business logic in that component. That example about moving the handleNextButtonClick function into my useInputs hook as a goNext function because it defines "going to the next input" logic really helped me. I completely agree that FormBody needs to be slimmed down as the amount of code in that component looks quite gnarly. Now I have a solid idea of where to start. I can't thank you enough for that!

2

u/SleazyDutcham Nov 25 '20

This is very useful, even if for nothing other than the bit of advice about inverting control. I'm not a front end developer by nature, and I don't really think though my designs this way. I've worked with APIs, their clients, and even a client that was a SDK... working both sides of a problem is generally incredibly informative, but I never thought about it too much at the component/package level for the front end. Thanks!

2

u/Samukxs Nov 25 '20

I like the use of outline/box-shadow for accessibility. I don't know if this was on purpose but you really did a good job I think. All project looks amazing, keep up the great work mate

1

u/_Invictuz Nov 25 '20

Thank you! An outline was there by default (black outline on Google Chrome - used to be a blue outline). As I was looking up how to get rid of the ugly black outline, I came upon accessibility and learnt a lot about it.

I always thought of accessibility as more of a requirement than an enhancement, but then I found this website where its accessibility actually improved the UX for me: https://govuk-form-builder.netlify.app/. It's also got a nice accessible Design System guide.

2

u/Samukxs Nov 25 '20

The same happened to me. This was exactly how I learned about accessibility. Thank you for sharing this website and happy studies!

2

u/justlasse Nov 25 '20

Super impressive piece of coding!! I agree the styling could need some tlc. Especially the floating arrows and the tabs are confusing. Probably better to use a progress timeline and move the arrows below or next to eachother rather than one floating and one on the side. Great work!! I’m studying your code for my own learning of hooks.

2

u/_Invictuz Nov 25 '20 edited Nov 25 '20

Yeah, I contemplated of doing a standard full page multi-step form design instead of something I thought would look cool. Big mistake! "Cool" is rarely usable lol. I think moving forward, I'll have to scrap my whole design to incorporate what you mentioned but at least I can still keep most of the logic. I really appreciate you checking out my code, that means a lot to me!

1

u/justlasse Nov 25 '20

Code looks solid. Ui should essentially be a minor detail to refactor :)

2

u/ptmdevncoder Nov 25 '20

Very creative

1

u/_Invictuz Nov 25 '20

Thank you, that was one of my goals! But as I'm learning, I may have tried to been too creative as people are finding the design confusing. Next time, I'll be sticking with the tried and true designs.

1

u/matmun13 Nov 25 '20

Really nice works for the first project with react.js in kind of self-taught. Is there any good resource which you're learning from?

1

u/_Invictuz Nov 25 '20

Yeah. I like to start with a video course or tutorial that builds a basic application. There are plenty of free ones on Youtube. This helps you get started with the basic syntax and structure of React components, and you get to see the end product. After you're done watching that, you have to go through the Official React Docs more than once. Then whenever you have questions, consult the React docs again or just keep googling and reading articles written by others, that's where the gold is at.

1

u/[deleted] Nov 25 '20

Horrible design of the dialog where you are constantly decreasing the size. That’s not what the dialogs are meant for.

1

u/_Invictuz Nov 25 '20

Thanks for your feedback! Could you give me some tips of what dialogs/modals are meant for, or maybe when I can do these size-changing animations in general? I see a lot of size-changing animations in designs which look nice, but I'm never sure if they are practical.

1

u/[deleted] Nov 25 '20

You could keep the dialog size same and just transition the content of each header instead of making the dialog smaller.

2

u/veeeerain Nov 25 '20

This is cool, if only I had the patience with css I’d try and do something like this.

1

u/awd26 Nov 25 '20

Wow, this looks amazing bro! Congrats!

2

u/[deleted] Nov 25 '20

[removed] — view removed comment

1

u/_Invictuz Nov 25 '20

Thank you for your kind encouragement!

You've touched on a big point of confusion for me. At first, I thought of this project as making a component library specifically for my company's design system (if I worked for one). Can I create the components myself, or should I just be making the multi-step form logic for people to hook up their own components to like how React Hook Form works. So I'm wondering if this type of library fits the internal library use case more than it fits the use case of a public-facing component library. But I'm just guessing right now as I have no real-world experience. I'm really curious as to what challenges you faced - was it overall design/UI flexibility or was it business logic flexibility?

I'll definitely be changing the design to a more standard full-page multi-step form design, which would probably help in terms of flexibility. Thanks again for your kind words, they really motivate me to keep going!