svelteuidev / svelteui

SvelteUI Monorepo
https://svelteui.dev
MIT License
1.27k stars 63 forks source link

[@svelteui/core] form management #428

Open khalibloo opened 1 year ago

khalibloo commented 1 year ago

Description

Ported mantine form into svelte. The useForm hook was converted into a svelte Form component to maintain reactivity.

Closes #369

Features

Before submitting the PR, please make sure you do the following

BeeMargarida commented 1 year ago

Hey, thank you for this! I'll take some time to check it out, but thank you for taking this initiative!

notramo commented 1 year ago

@khalibloo, I will take some time to dig through it, but here are some quick questions:

khalibloo commented 1 year ago

@khalibloo, I will take some time to dig through it, but here are some quick questions:

* How is i18n solved for validation messages?

* What validation library is used? Can I use Zod, or other TypeScript validation libraries?

* If it's a flexible solution, how much copy-paste boilerplate is needed?

* How can it handle multi-step forms? Do the inputs have to be in the DOM all the time and hidden with CSS, or is it OK if an `{#if}` statement removes them from the DOM, based on the current step?

Great to have another pair of eyes on it. It mostly mimic's mantine's useForm. Do take a look at this file to get an idea of its usage https://github.com/svelteuidev/svelteui/pull/428/files#diff-a0f3dafbe6885a5e04e3be1825d4ccc960adbeceb5933503b58142a31e1ba7b7

  1. the user provides the validation strings. the messages will have to be localized by the user.
  2. there are a few built-in validators for common use cases (ex: isEmail, isNotEmpty, matchesField), but it also includes adapters for joi, zod, superstruct, and yup.
  3. there is just a Form component that takes initial values, validation schema, etc. and a Field component that wraps around individual fields in the form. There are 2 aspects I'm not fond of. First is that the Field requires a prop to obtain the form instance. It would be best if this was done through context, but I can't quite get that to work yet. Secondly, the Form is not able to infer the typescript type of the data within it.
  4. I have not tested multi-step forms yet, and I'm not sure that mantine even supports that. For this PR, I just wanted to port over the mantine implementation. If it's not supported in mantine, it'll have to come in as a future enhancement.

Edit: I tested the 4th item, and it works even when the inputs are removed from the DOM. Both in mantine form and in this implementation.