-
Notifications
You must be signed in to change notification settings - Fork 0
Add Documentation with react examples #5
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
eudago
wants to merge
2
commits into
main
Choose a base branch
from
docs
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
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -1,2 +1,232 @@ | ||
| # @kavetech/forms | ||
| # @kavehome/forms | ||
| Reactive Forms for your application | ||
|
|
||
| # How to use | ||
|
|
||
| ## Install | ||
|
|
||
| Then you can install the package using `npm` or `yarn`. | ||
|
|
||
| ```bash | ||
| npm install @kavehome/forms | ||
| ``` | ||
|
|
||
| ```bash | ||
| yarn add @kavehome/forms | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| The `@kavehome/forms` library is an agnostic library, so it can be used with any framework or library, there have some examples of how to use it with `react`. | ||
|
|
||
| This is a simple example of how to use a form in a react functional component: | ||
|
|
||
| ```tsx | ||
| import { useEffect, useRef, useState } from 'react'; | ||
| import { FormFactory } from '@kavehome/forms'; | ||
| import { RxControlGroup } from '@kavehome/forms/dist/types'; | ||
|
|
||
| interface FormValues { | ||
| name: string; | ||
| email: string; | ||
| } | ||
|
|
||
| const FormExample = () => { | ||
| const [formModel, setFormModel] = useState<FormValues| null>(null); | ||
| const form = useRef<RxControlGroup<FormValues> | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| // Create the form | ||
| form.current = FormFactory.group<FormValues>({ | ||
| name: FormFactory.control(''), | ||
| email: FormFactory.control(''), | ||
| }) | ||
|
|
||
| // Set the form model | ||
| setFormModel(form.current.value); | ||
| }, []) | ||
|
|
||
| const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => { | ||
| event.preventDefault(); | ||
|
|
||
| if (form.current!.isValid()) { | ||
| // Do something with the form values | ||
| console.log(form.current!.value); | ||
| } | ||
| } | ||
|
|
||
| const refresh = () => { | ||
| // Refresh the form model | ||
| setFormModel(form.current!.value); | ||
| } | ||
|
|
||
| // We only render the form when the form model is ready | ||
| if (!formModel) { | ||
| return null; | ||
| } | ||
|
|
||
| return (<form onSubmit={handleSubmit} onChange={() => refresh()}> | ||
| <input type="text" onChange={(e) => form.current!.get('name')!.value = e.target.value} /> | ||
| <input type="email" onChange={(e) => form.current!.get('email')!.value = e.target.value} /> | ||
| <button type="submit">Submit</button> | ||
| <p>{JSON.stringify(formModel)}</p> | ||
| </form>) | ||
| } | ||
| ``` | ||
|
|
||
| We can also use the `useForm` hook to create the form, and simplify the code. | ||
|
|
||
| ```tsx | ||
| function useForm<T>(formGroup: RxControlGroup<T>): [ | ||
| RxControlGroup<T> | null, | ||
| T | null, | ||
| (form: RxControlGroup<T> | undefined) => void | ||
| ] { | ||
| const [model, setModel] = useState<T | null>(null); | ||
| const formRef: MutableRefObject<RxControlGroup<T> | null> = useRef<RxControlGroup<T>>(null); | ||
|
|
||
| useLayoutEffect(() => { | ||
| if(!formGroup || formRef.current === formGroup) return; | ||
| formRef.current = formGroup; | ||
| setModel(formRef.current.value); | ||
| }, [formGroup]); | ||
|
|
||
| const onFormChange = (form: RxControlGroup<T> | undefined) => { | ||
| // add a debounce to avoid multiple renders | ||
| if(form) setModel(form.value); | ||
| } | ||
|
|
||
| return [ formRef.current, model, onFormChange ]; | ||
| } | ||
| ``` | ||
|
|
||
| So the previous example can be simplified to: | ||
|
|
||
| ```tsx | ||
| import { useEffect, useRef, useState } from 'react'; | ||
| import { FormFactory } from '@kavehome/forms'; | ||
| import { RxControlGroup } from '@kavehome/forms/dist/types'; | ||
| import { useForm } from '.../useForm'; | ||
|
|
||
| interface FormValues { | ||
| name: string; | ||
| email: string; | ||
| } | ||
|
|
||
| function FormExample() { | ||
| const initForm = useMemo(() => FormFactory.group<FormValues>({ | ||
| name: FormFactory.control(''), | ||
| email: FormFactory.control(''), | ||
| }), []); | ||
| const [form, model, onFormChange] = useForm<FormValues>(initForm); | ||
|
|
||
| if (!form) { | ||
| return null; | ||
| } | ||
|
|
||
| return (<form onChange={() => onFormChange(form)}> | ||
| <input type="text" onChange={(e) => form!.get('name')!.value = e.target.value} /> | ||
| <input type="email" onChange={(e) => form!.get('email')!.value = e.target.value} /> | ||
| <button type="submit">Submit</button> | ||
| <p>{JSON.stringify(model)}</p> | ||
| </form>) | ||
| } | ||
| ``` | ||
|
|
||
| ### FormFactory.group | ||
|
|
||
| The `FormFactory.group` method takes an object with a key-value pair of `string` and `RxFormControl` or `RxFormControl[]` or other `RxControlGroup` as the value. | ||
|
|
||
| ```typescript | ||
| FormFactory.group({ | ||
| name: FormFactory.control(''), | ||
| email: FormFactory.control(''), | ||
| pet: FormFactory.group({ | ||
| name: FormFactory.control(''), | ||
| }), | ||
| }) | ||
| ``` | ||
|
|
||
| We can access the controls using the `get` method. | ||
|
|
||
| ```typescript | ||
| const form = FormFactory.group({ | ||
| name: FormFactory.control(''), | ||
| email: FormFactory.control(''), | ||
| pet: FormFactory.group({ | ||
| name: FormFactory.control(''), | ||
| }), | ||
| }) | ||
|
|
||
| // acces to the name control | ||
| console.log(form.get('name')) | ||
| // acces to the value of the name control | ||
| console.log(form.get('name').value) | ||
| // check if the name control is valid | ||
| console.log(form.get('name').isValid()) | ||
| ``` | ||
|
|
||
| ### FormFactory.control | ||
|
|
||
| The `FormFactory.control` method takes an initial value and an array of validators. | ||
|
|
||
| ```typescript | ||
| FormFactory.control('', [Validators.required]) | ||
| ``` | ||
|
|
||
| ### FormFactory.array | ||
|
|
||
| The `FormFactory.array` method takes an array of `RxFormControl` as the value. | ||
|
|
||
| ```typescript | ||
| FormFactory.array([ | ||
| FormFactory.control(''), | ||
| FormFactory.control(''), | ||
| ]) | ||
| ``` | ||
|
|
||
| ### Validators | ||
|
|
||
| The `Validators` class contains a set of validators that can be used with the `FormFactory.control` method. | ||
|
|
||
| These are the available validators: | ||
|
|
||
| - `required` | ||
| - `min` | ||
| - `max` | ||
| - `range` | ||
| - `format` | ||
|
|
||
| ```typescript | ||
| import { Validators } from '@kavehome/forms'; | ||
|
|
||
| FormFactory.control('', [Validators.required]) | ||
| ``` | ||
|
|
||
| We can also create our own custom validators, a validator is a function that takes a value and returns a object if the value is invalid or null if the value is valid. | ||
|
|
||
| ```typescript | ||
| import { Validators } from '@kavehome/forms'; | ||
|
|
||
| const customValidator = (value: string) : {fooError: string} | null => { | ||
| if (value === 'foo') { | ||
| return { | ||
| fooError: 'Value cannot be foo'; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| FormFactory.control('', [customValidator]) | ||
| ``` | ||
|
|
||
| When a control is invalid, the `errors` property will contain an object with the errors. | ||
|
|
||
| ```typescript | ||
| const form = FormFactory.group({ | ||
| name: FormFactory.control('', [Validators.required]), | ||
| }) | ||
|
|
||
| console.log(form.get('name').errors) | ||
| ``` | ||
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.
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.
it is really necessary to add the
!right after theform? In line 123 there is an if to validate form is defined