-
Notifications
You must be signed in to change notification settings - Fork 0
TS tn-forms - Form builder #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lakardion
wants to merge
8
commits into
main
Choose a base branch
from
feature/form-builder-ts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
095c874
feat(builder) add form builder basic
lakardion d7bfd78
feat(builder) add form replicate
lakardion 34af021
feat(builder) add initial form level validator
lakardion 0c29533
feat(builder) add validate
lakardion f7d4cb8
feat(builder) add form level validator test
lakardion ca56e0a
feat(builder) add get value
lakardion 40f91a8
some ts updates
lakardion 1a0ba84
--wip-- [skip ci]
lakardion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // I want to get a very basic version of this working and then we can start working on adding more and more functionality | ||
| // There are some methods and stuff in the original form class that I am not fully sure how useful they are for current users. From my use of the library most of them seem to be unnecessary. I will try to keep the API as simple as possible and only add features that are being used currently in the repos. The previous form will continue to live for sure but I want to get this exposed so that we can start tinkering with it. | ||
|
|
||
| import { FormField, FormFieldZod } from './forms' | ||
| import { IFormFieldZod, IFormFieldZodCreate } from './interfaces' | ||
|
|
||
| /** | ||
| * A v2 of tn-forms that is based on a builder pattern and allows for better field discoverability and type safety | ||
| */ | ||
| export class FormBuilder<T extends Record<string, IFormFieldZod<any, any>> = {}> { | ||
| fields: T = {} as T | ||
|
|
||
| constructor(fields: T = {} as T) { | ||
| this.fields = fields | ||
| } | ||
|
|
||
| addField = <TName extends string, TField extends IFormFieldZodCreate<any, TName>>( | ||
| field: TField, | ||
| ) => { | ||
| this.fields = { | ||
| ...this.fields, | ||
| [field.name]: FormFieldZod.create(field), | ||
| } | ||
| return this as FormBuilder<T & Record<TField['name'], IFormFieldZod<TField['value'], TName>>> | ||
| } | ||
|
|
||
| setFieldValue<TFieldName extends keyof T>(fieldName: TFieldName, value: T[TFieldName]['value']) { | ||
| this.fields[fieldName].value = value | ||
| } | ||
|
|
||
| replicate = () => new FormBuilder(this.fields) | ||
|
|
||
| // addFormLevelValidator<TFieldName extends keyof T>( | ||
| // fieldName: TFieldName, | ||
| // validator: IFormLevelValidator, | ||
| // ) { | ||
| // const currentField = this.fields[fieldName] | ||
| // const newValidator = validator | ||
| // newValidator.setMatchingField(this.fields) | ||
| // if (this.fields[fieldName] instanceof FormField) { | ||
| // currentField.addValidator(newValidator) | ||
| // } | ||
| // return this | ||
| // } | ||
|
|
||
| /** | ||
| * Check all the validators on the fields. | ||
| */ | ||
| validate() { | ||
| for (const f of Object.values(this.fields)) { | ||
| if (f instanceof FormField) { | ||
| f.validate() | ||
| } | ||
| } | ||
| return this | ||
| } | ||
|
|
||
| //TODO: This still has the same problem than the original tn-forms. We have no proper way of displaying the right type with the current system of validators. I'm thinking about moving away from these validators and instead provide a zod-like validators system where we just use either parse or safe parse and we get the valid value for real and that would be runtime and type checked. | ||
| get value() { | ||
| return Object.fromEntries(Object.entries(this.fields).map(([k, v]) => [k, v.value])) as { | ||
| [K in keyof T]: T[K]['value'] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export const createForm = () => new FormBuilder() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { createForm } from './builder' | ||
| import { | ||
| EmailValidator, | ||
| MinLengthValidator, | ||
| MustMatchValidator, | ||
| RequiredValidator, | ||
| } from './validators' | ||
|
|
||
| const accountForm = createForm() | ||
| .addField({ | ||
| name: 'firstName', | ||
| label: 'First name', | ||
| placeholder: 'First Name', | ||
| type: 'text', | ||
| validators: [new RequiredValidator({ message: 'Please enter your first' })], | ||
| value: '', | ||
| }) | ||
| .addField({ | ||
| name: 'lastName', | ||
| label: 'Last Name', | ||
| placeholder: 'Last Name', | ||
| type: 'text', | ||
| validators: [new RequiredValidator({ message: 'Please enter your last name' })], | ||
| value: '', | ||
| }) | ||
| .addField({ | ||
| name: 'email', | ||
| label: 'Email', | ||
| placeholder: 'Email', | ||
| type: 'email', | ||
| value: '', | ||
| validators: [new EmailValidator({ message: 'Please enter a valid email' })], | ||
| }) | ||
| .addField({ | ||
| name: 'password', | ||
| label: 'Password', | ||
| placeholder: 'Password', | ||
| type: 'password', | ||
| validators: [ | ||
| new MinLengthValidator({ | ||
| minLength: 8, | ||
| message: 'Please enter a password with a minimum of 8 characters', | ||
| }), | ||
| ], | ||
| value: '', | ||
| }) | ||
| .addField({ | ||
| name: 'confirmPassword', | ||
| label: 'Confirm Password', | ||
| placeholder: 'Confirm Password', | ||
| type: 'password', | ||
| value: '', | ||
| validators: [], | ||
| }) | ||
| .addFormLevelValidator( | ||
| 'confirmPassword', | ||
| new MustMatchValidator({ | ||
| message: 'Passwords must match', | ||
| matcher: 'password', | ||
| }), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should break out of the builder pattern and instead return a
successprop which would tell whether the validation was successful.If the validation was successful we should also return a
dataorvaluewhere the fully validated form value is returned, so that we can use that type-safely@oudeismetis From our discussion on bootstrapper