From bd253b4760ff60edf7b03c0255671ad64fa67ee1 Mon Sep 17 00:00:00 2001 From: Daniel Requejo Date: Tue, 28 Feb 2023 19:06:56 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Fix=20submit=20issue=20&=20add?= =?UTF-8?q?=20prettier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/node.js.yml | 10 +- .github/workflows/package-size-report.yml | 4 +- .prettierignore | 3 + .prettierrc.json | 5 + .vscode/extensions.json | 7 +- CONTRIBUTING.md | 2 +- License.md | 2 +- README.md | 117 +++++---- docs/.vitepress/config.cts | 75 ++++-- docs/.vitepress/theme/index.js | 16 +- docs/api/form-handler.md | 31 +-- docs/api/use-form-handler/clear-error.md | 108 +++++---- docs/api/use-form-handler/clear-field.md | 52 ++-- docs/api/use-form-handler/form-state.md | 64 ++--- docs/api/use-form-handler/handle-submit.md | 96 +++++--- docs/api/use-form-handler/index.md | 187 +++++++------- docs/api/use-form-handler/modified-values.md | 32 +-- docs/api/use-form-handler/register.md | 229 ++++++++++-------- docs/api/use-form-handler/reset-field.md | 27 ++- docs/api/use-form-handler/reset-form.md | 71 +++--- docs/api/use-form-handler/set-error.md | 37 ++- docs/api/use-form-handler/set-value.md | 32 ++- .../use-form-handler/trigger-validation.md | 123 ++++++---- docs/api/use-form-handler/unregister.md | 30 +-- docs/api/use-form-handler/values.md | 37 +-- docs/components/SandboxEmbedder.vue | 41 ++-- docs/examples/async-submission.md | 4 +- docs/examples/async-validations.md | 3 +- docs/examples/basic.md | 3 +- docs/examples/dependent-fields.md | 3 +- docs/get-started/introduction.md | 32 +-- docs/get-started/quick-start.md | 98 +++++--- docs/guides/material-libraries.md | 2 +- docs/guides/typescript.md | 2 +- docs/index.md | 2 +- docs/vite.config.ts | 6 +- examples/async-submission/App.vue | 83 ++++--- examples/async-submission/index.html | 31 ++- examples/async-submission/tsconfig.json | 7 +- examples/async-validations/App.vue | 87 ++++--- examples/async-validations/index.html | 31 ++- examples/async-validations/tsconfig.json | 7 +- examples/basic/App.vue | 34 +-- examples/basic/index.html | 31 ++- examples/basic/tsconfig.json | 7 +- examples/dependent-fields/App.vue | 55 +++-- examples/dependent-fields/index.html | 31 ++- examples/dependent-fields/tsconfig.json | 7 +- examples/interceptor/App.vue | 46 ++-- examples/interceptor/index.html | 31 ++- examples/interceptor/tsconfig.json | 7 +- examples/tutorial/App.vue | 81 ++++--- examples/tutorial/index.html | 31 ++- examples/tutorial/tsconfig.json | 7 +- package-lock.json | 22 ++ package.json | 1 + playground/App.vue | 93 ++++--- playground/index.html | 12 +- src/FormHandler.ts | 41 ++-- src/constants.ts | 15 +- src/index.ts | 2 +- src/logic/getDefaultFieldValue.ts | 18 +- src/logic/getNativeFieldValue.ts | 43 ++-- src/logic/index.ts | 2 +- src/logic/refFn.ts | 97 ++++---- src/logic/transformValidations.ts | 91 ++++--- src/logic/validateField.ts | 37 +-- src/logic/validateForm.ts | 12 +- src/test/component.test.ts | 64 ++--- src/test/handler.test.ts | 179 +++++++------- src/test/register.test.ts | 125 +++++----- src/types/baseControl.ts | 46 ++-- src/types/formHandler.ts | 160 ++++++------ src/types/index.ts | 2 +- src/types/logic.ts | 14 +- src/types/refs.ts | 46 +++- src/types/register.ts | 36 +-- src/types/validations.ts | 92 +++---- src/useFormHandler.ts | 128 ++++++---- src/utils/index.ts | 2 +- src/utils/isCheckboxInput.ts | 3 +- src/utils/isMultipleSelect.ts | 2 +- src/utils/isNativeControl.ts | 2 +- src/utils/isRadioInput.ts | 3 +- src/utils/max.ts | 11 +- src/utils/maxLength.ts | 12 +- src/utils/min.ts | 9 +- src/utils/minLength.ts | 12 +- src/utils/pattern.ts | 9 +- src/utils/required.ts | 9 +- src/utils/test/isCheckboxInput.test.ts | 14 +- src/utils/test/isMultipleSelect.test.ts | 18 +- src/utils/test/isNativeControl.test.ts | 34 +-- src/utils/test/isRadioInput.test.ts | 14 +- src/utils/test/max.test.ts | 19 +- src/utils/test/maxLength.test.ts | 26 +- src/utils/test/min.test.ts | 18 +- src/utils/test/minLength.test.ts | 24 +- src/utils/test/pattern.test.ts | 18 +- src/utils/test/required.test.ts | 18 +- tsconfig.json | 16 +- tsconfig.node.json | 6 +- vite.config.ts | 8 +- 103 files changed, 2137 insertions(+), 1755 deletions(-) create mode 100644 .prettierignore create mode 100644 .prettierrc.json diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 3a3ed55..a785ea1 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -5,9 +5,9 @@ name: CI on: push: - branches: [ "master" ] + branches: ['master'] pull_request: - branches: [ "master" ] + branches: ['master'] jobs: build: @@ -33,7 +33,7 @@ jobs: - name: Package build test run: npm run build --if-present - + unit-test: name: Unit testing runs-on: ubuntu-latest @@ -46,8 +46,8 @@ jobs: uses: actions/setup-node@v3 with: node-version: '18' - + - run: npm ci - + - name: Unit tests run: npm run test diff --git a/.github/workflows/package-size-report.yml b/.github/workflows/package-size-report.yml index 911ffaa..9d95d1b 100644 --- a/.github/workflows/package-size-report.yml +++ b/.github/workflows/package-size-report.yml @@ -2,7 +2,7 @@ name: Package Size Report on: pull_request: - branches: [ master ] + branches: [master] jobs: pkg-size-report: @@ -21,4 +21,4 @@ jobs: - name: Package size report uses: pkg-size/action@v1 env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..1b07c39 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,3 @@ +# Ignore artifacts: +build +coverage \ No newline at end of file diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..e6f8ee3 --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,5 @@ +{ + "singleQuote": true, + "trailingComma": "es5", + "semi": false +} diff --git a/.vscode/extensions.json b/.vscode/extensions.json index cf899f8..c0a6e5a 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,6 +1,3 @@ { - "recommendations": [ - "Vue.volar", - "Vue.vscode-typescript-vue-plugin" - ] -} \ No newline at end of file + "recommendations": ["Vue.volar", "Vue.vscode-typescript-vue-plugin"] +} diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 21b952c..e14376c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1 +1 @@ -**Working on your first Pull Request?** You can learn how from this *free* series [How to Contribute to an Open Source Project on GitHub](https://kcd.im/pull-request) \ No newline at end of file +**Working on your first Pull Request?** You can learn how from this _free_ series [How to Contribute to an Open Source Project on GitHub](https://kcd.im/pull-request) diff --git a/License.md b/License.md index 959464c..b05471f 100644 --- a/License.md +++ b/License.md @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file +SOFTWARE. diff --git a/README.md b/README.md index fe9ab5b..4443d8a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ -

vue-form-handler

@@ -12,39 +11,47 @@ The easy way of handling your vue forms [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) Buy Me A Coffee -
+ ## ðŸ“Ķ Installation + --- -``` yarn add vue-form-handler ``` -``` npm i --save vue-form-handler ``` +`yarn add vue-form-handler` + +`npm i --save vue-form-handler` ## 🚀 Features + --- + - 💊 **Type strong**: Written in TypeScript - ðŸ”Đ **Flexible**: you can wrap the form handler over native inputs or any other like the ones from material libraries or custom inputs. - ðŸŠķ **Super light**: Small package size - ðŸ’ŧ **DX**: Great development experience ## ðŸĶ„ Usage + --- + ### Basic usage ```vue - ``` @@ -52,26 +59,37 @@ const successFn = (form: Record) => {console.log({form})} ```vue - ``` @@ -79,32 +97,41 @@ const successFn = (form: Record) => {console.log({form})} ```vue - ``` ### For a more advanced usage visit the [Docs](https://vue-form-handler.com) ## 📈 Project activity + --- -![Alt](https://repobeats.axiom.co/api/embed/d0da4b79bde282068c5f3da3505091b1447a1f6c.svg "Repobeats analytics image") + +![Alt](https://repobeats.axiom.co/api/embed/d0da4b79bde282068c5f3da3505091b1447a1f6c.svg 'Repobeats analytics image') ## 💜 Thanks + --- + This project is heavily inspired by other awesome projects like: + - [jaredpalmer/formik](https://github.com/jaredpalmer/formik) - [react-hook-form/react-hook-form](https://github.com/react-hook-form/react-hook-form) ## 📄 License + --- -[MIT License](https://github.com/dbssman/vue-form-handler/blob/master/License.md) ÂĐ 2022-PRESENT [Dennis Bosmans](https://github.com/dbssman) \ No newline at end of file + +[MIT License](https://github.com/dbssman/vue-form-handler/blob/master/License.md) ÂĐ 2022-PRESENT [Dennis Bosmans](https://github.com/dbssman) diff --git a/docs/.vitepress/config.cts b/docs/.vitepress/config.cts index 9754ffc..744eaf1 100644 --- a/docs/.vitepress/config.cts +++ b/docs/.vitepress/config.cts @@ -1,4 +1,4 @@ -import { defineConfig } from "vitepress" +import { defineConfig } from 'vitepress' export default defineConfig({ title: 'VueFormHandler', @@ -9,17 +9,31 @@ export default defineConfig({ ['link', { rel: 'icon', href: '/favicon.svg', type: 'image/svg+xml' }], ['meta', { name: 'author', content: 'Dennis R. Bosmans' }], ['meta', { property: 'og:title', content: 'VueFormHandler' }], - ['meta', { property: 'og:image', content: 'https://vue-form-handler.com/favicon.png' }], - ['meta', { property: 'og:description', content: 'The only handler you\'ll need to easily work with forms in vue' }], + [ + 'meta', + { + property: 'og:image', + content: 'https://vue-form-handler.com/favicon.png', + }, + ], + [ + 'meta', + { + property: 'og:description', + content: + "The only handler you'll need to easily work with forms in vue", + }, + ], ], themeConfig: { logo: '/favicon.svg', editLink: { - pattern: 'https://github.com/dbssman/vue-form-handler/edit/master/docs/:path', + pattern: + 'https://github.com/dbssman/vue-form-handler/edit/master/docs/:path', text: 'Edit this page on GitHub', }, socialLinks: [ - { icon: 'github', link: 'https://github.com/dbssman/vue-form-handler' } + { icon: 'github', link: 'https://github.com/dbssman/vue-form-handler' }, ], footer: { message: 'Released under the MIT License.', @@ -31,51 +45,68 @@ export default defineConfig({ ], sidebar: [ { - text: 'Get started', items: [ + text: 'Get started', + items: [ { text: 'Introduction', link: '/get-started/introduction' }, { text: 'Quick Start', link: '/get-started/quick-start' }, - ] + ], }, { - text: 'Guides', collapsible: true, items: [ + text: 'Guides', + collapsible: true, + items: [ { text: 'Custom components', link: '/guides/custom-components' }, { text: 'Material libraries', link: '/guides/material-libraries' }, { text: 'Native support', link: '/guides/native-support' }, { text: 'Typescript', link: '/guides/typescript' }, { text: 'Validation', link: '/guides/validation' }, - ] + ], }, { - text: 'Examples', collapsible: true, items: [ + text: 'Examples', + collapsible: true, + items: [ { text: 'Async submission', link: '/examples/async-submission' }, { text: 'Async validations', link: '/examples/async-validations' }, { text: 'Basic', link: '/examples/basic' }, { text: 'Dependent fields', link: '/examples/dependent-fields' }, - ] + ], }, { - text: 'API Reference', collapsible: true, items: [ + text: 'API Reference', + collapsible: true, + items: [ { - text: 'useFormHandler', link: '/api/use-form-handler/', + text: 'useFormHandler', + link: '/api/use-form-handler/', items: [ { text: 'clearError', link: '/api/use-form-handler/clear-error' }, { text: 'clearField', link: '/api/use-form-handler/clear-field' }, { text: 'formState', link: '/api/use-form-handler/form-state' }, - { text: 'handleSubmit', link: '/api/use-form-handler/handle-submit' }, - { text: 'modifiedValues', link: '/api/use-form-handler/modified-values' }, + { + text: 'handleSubmit', + link: '/api/use-form-handler/handle-submit', + }, + { + text: 'modifiedValues', + link: '/api/use-form-handler/modified-values', + }, { text: 'register', link: '/api/use-form-handler/register' }, { text: 'resetField', link: '/api/use-form-handler/reset-field' }, { text: 'resetForm', link: '/api/use-form-handler/reset-form' }, { text: 'setError', link: '/api/use-form-handler/set-error' }, { text: 'setValue', link: '/api/use-form-handler/set-value' }, - { text: 'triggerValidation', link: '/api/use-form-handler/trigger-validation' }, + { + text: 'triggerValidation', + link: '/api/use-form-handler/trigger-validation', + }, { text: 'unregister', link: '/api/use-form-handler/unregister' }, { text: 'values', link: '/api/use-form-handler/values' }, - ] + ], }, - { text: `FormHandler`, link: '/api/form-handler' } - ] - } + { text: `FormHandler`, link: '/api/form-handler' }, + ], + }, ], - } -}) \ No newline at end of file + }, +}) diff --git a/docs/.vitepress/theme/index.js b/docs/.vitepress/theme/index.js index 894386c..2241992 100644 --- a/docs/.vitepress/theme/index.js +++ b/docs/.vitepress/theme/index.js @@ -1,10 +1,10 @@ -import DefaultTheme from "vitepress/theme"; -import SandboxEmbedder from "../../components/SandboxEmbedder.vue"; +import DefaultTheme from 'vitepress/theme' +import SandboxEmbedder from '../../components/SandboxEmbedder.vue' export default { - ...DefaultTheme, - enhanceApp(ctx) { - DefaultTheme.enhanceApp(ctx) - ctx.app.component('CodeExample', SandboxEmbedder) - } -} \ No newline at end of file + ...DefaultTheme, + enhanceApp(ctx) { + DefaultTheme.enhanceApp(ctx) + ctx.app.component('CodeExample', SandboxEmbedder) + }, +} diff --git a/docs/api/form-handler.md b/docs/api/form-handler.md index 0159d19..86c6b12 100644 --- a/docs/api/form-handler.md +++ b/docs/api/form-handler.md @@ -1,34 +1,35 @@ # \ -The `FormHandler` component is an utility for people that are still using the options API or, that for some reason want to do the handling on the template side. +The `FormHandler` component is an utility for people that are still using the options API or, that for some reason want to do the handling on the template side. ::: tip It is highly recommend that you use the `useFormHandler` composable if possible, since it is the intended way of making use of this package and gives you more control over the form. ::: + ## How it works You pass the same props as you'd be passing to the composable but to a component on the template, and the component will enable you a [scoped slot](https://vuejs.org/guide/components/slots.html#scoped-slots) with the return of `useFormHandler`. As you can imagine, the `FormHandler` composable and `useFormHandler` share the same API but differ in the usage. -## Example: +## Example: ```vue ``` diff --git a/docs/api/use-form-handler/clear-error.md b/docs/api/use-form-handler/clear-error.md index 71bac1d..bfeb7a4 100644 --- a/docs/api/use-form-handler/clear-error.md +++ b/docs/api/use-form-handler/clear-error.md @@ -5,40 +5,41 @@ Clears all the errors from the form or the error from a field programmatically. ## Demo Coming soon... + ## Usage ### Validating/Invalidating the form without a field scoped validation ```vue - ``` ## Type Declarations ```ts -export type ClearError = ( - name?: string -) => void -``` \ No newline at end of file +export type ClearError = (name?: string) => void +``` diff --git a/docs/api/use-form-handler/clear-field.md b/docs/api/use-form-handler/clear-field.md index 815e943..b43569f 100644 --- a/docs/api/use-form-handler/clear-field.md +++ b/docs/api/use-form-handler/clear-field.md @@ -13,34 +13,34 @@ Coming soon... ```vue ``` @@ -49,10 +49,10 @@ const { register } = useFormHandler({ ```vue - ``` @@ -46,28 +56,38 @@ const submitFn = () => { ```vue - ``` @@ -79,7 +99,7 @@ export type HandleSubmitSuccessFn = (values: Record) => void export type HandleSubmitErrorFn = (errors: Record) => void export type HandleSubmit = ( - successFn: HandleSubmitSuccessFn, - errorFn?: HandleSubmitErrorFn + successFn: HandleSubmitSuccessFn, + errorFn?: HandleSubmitErrorFn ) => void -``` \ No newline at end of file +``` diff --git a/docs/api/use-form-handler/index.md b/docs/api/use-form-handler/index.md index 9d17930..8bd2cbb 100644 --- a/docs/api/use-form-handler/index.md +++ b/docs/api/use-form-handler/index.md @@ -13,21 +13,23 @@ Changing the initialValues will trigger a form reset, this means the formState a Values which we use to initialize the form, this is useful when we get an initial value state for the form from an external call. #### Example + ```vue ``` @@ -39,20 +41,20 @@ Function that allows us to intercept any value assignment, accept or deny it and The interceptor will be called passing an object as a parameter with the following: -| attribute | type | description | -|-----------|--------|--------------------------------------------| -| name | `string` | Name of the field that is about to be set | -| value | `any` | Value of the field that is about to be set | -| clearError | [ClearError](/api/use-form-handler/clear-error#type-declarations) | [API - clearError](/api/use-form-handler/clear-error) | -| clearField | [ClearField](/api/use-form-handler/clear-field#type-declarations) | [API - clearField](/api/use-form-handler/clear-field) | -| formState | [FormState](/api/use-form-handler/form-state#type-declarations) | [API - formState](/api/use-form-handler/form-state) | -| modifiedValues | [ModifiedValues](/api/use-form-handler/modified-values#type-declarations) | [API - modifiedValues](/api/use-form-handler/modified-values) | -| resetField | [ResetField](/api/use-form-handler/reset-field#type-declarations) | [API - resetField](/api/use-form-handler/reset-field) | -| resetForm | [ResetForm](/api/use-form-handler/reset-form#type-declarations) | [API - resetForm](/api/use-form-handler/reset-form) | -| setError | [SetError](/api/use-form-handler/set-error#type-declarations) | [API - setError](/api/use-form-handler/set-error) | -| setValue | [SetValue](/api/use-form-handler/set-value#type-declarations) | [API - setValue](/api/use-form-handler/set-value) | +| attribute | type | description | +| ----------------- | ------------------------------------------------------------------------------- | ------------------------------------------------------------------- | +| name | `string` | Name of the field that is about to be set | +| value | `any` | Value of the field that is about to be set | +| clearError | [ClearError](/api/use-form-handler/clear-error#type-declarations) | [API - clearError](/api/use-form-handler/clear-error) | +| clearField | [ClearField](/api/use-form-handler/clear-field#type-declarations) | [API - clearField](/api/use-form-handler/clear-field) | +| formState | [FormState](/api/use-form-handler/form-state#type-declarations) | [API - formState](/api/use-form-handler/form-state) | +| modifiedValues | [ModifiedValues](/api/use-form-handler/modified-values#type-declarations) | [API - modifiedValues](/api/use-form-handler/modified-values) | +| resetField | [ResetField](/api/use-form-handler/reset-field#type-declarations) | [API - resetField](/api/use-form-handler/reset-field) | +| resetForm | [ResetForm](/api/use-form-handler/reset-form#type-declarations) | [API - resetForm](/api/use-form-handler/reset-form) | +| setError | [SetError](/api/use-form-handler/set-error#type-declarations) | [API - setError](/api/use-form-handler/set-error) | +| setValue | [SetValue](/api/use-form-handler/set-value#type-declarations) | [API - setValue](/api/use-form-handler/set-value) | | triggerValidation | [TriggerValidation](/api/use-form-handler/trigger-validation#type-declarations) | [API - triggerValidation](/api/use-form-handler/trigger-validation) | -| values | `Record` | [API - values](/api/use-form-handler/values) | +| values | `Record` | [API - values](/api/use-form-handler/values) | ::: info As you can see, the interceptor is provided with everything the handler does provide but in a separate context. @@ -67,34 +69,34 @@ return true to proceed setting the value and false if the value should not be se ```vue ``` @@ -107,24 +109,24 @@ Use this function in the case you'd rather perform a custom/different validation ```vue ``` @@ -132,12 +134,12 @@ Use this function in the case you'd rather perform a custom/different validation This option allows you to configure the validation mode or strategy the handler will follow. -| name | type | description | -|-----------|--------|--------------------------------------------| -| onChange | `string` | Validation will trigger on the change event with each input, and lead to multiple re-renders. | -| onBlur | `string` | Validation will trigger on the blur event. | -| onSubmit | `string` | Validation will trigger on the submit event. | -| always | `string` | Validation will trigger on change and blur events. | +| name | type | description | +| -------- | -------- | --------------------------------------------------------------------------------------------- | +| onChange | `string` | Validation will trigger on the change event with each input, and lead to multiple re-renders. | +| onBlur | `string` | Validation will trigger on the blur event. | +| onSubmit | `string` | Validation will trigger on the submit event. | +| always | `string` | Validation will trigger on change and blur events. | ::: warning Using the `always` validationMode will have a more significant impact on performance. @@ -162,43 +164,36 @@ Using the `always` validationMode will have a more significant impact on perform ## Type Declarations ```ts - -export type Interceptor = ( - _: InterceptorParams -) => Promise | boolean +export type Interceptor = (_: InterceptorParams) => Promise | boolean export type FormValidation = ( - values: Record + values: Record ) => Promise | boolean export interface FormHandlerParams { - initialValues?: Record - | Ref> + initialValues?: + | Record + | Ref> | ComputedRef> - interceptor?: Interceptor - validate?: FormValidation - validationMode?: 'onChange' - | 'onBlur' - | 'onSubmit' - | 'always' + interceptor?: Interceptor + validate?: FormValidation + validationMode?: 'onChange' | 'onBlur' | 'onSubmit' | 'always' } export interface FormHandlerReturn { - formState: FormState - values: Record - clearError: ClearError - clearField: ClearField - handleSubmit: HandleSubmit - modifiedValues: ModifiedValues - register: Register - resetField: ResetField - resetForm: ResetForm - setError: SetError - setValue: SetValue - triggerValidation: TriggerValidation - unregister: (name: string) => void + formState: FormState + values: Record + clearError: ClearError + clearField: ClearField + handleSubmit: HandleSubmit + modifiedValues: ModifiedValues + register: Register + resetField: ResetField + resetForm: ResetForm + setError: SetError + setValue: SetValue + triggerValidation: TriggerValidation + unregister: (name: string) => void } -export type FormHandler = ( - _?: FormHandlerParams -) => FormHandlerReturn -``` \ No newline at end of file +export type FormHandler = (_?: FormHandlerParams) => FormHandlerReturn +``` diff --git a/docs/api/use-form-handler/modified-values.md b/docs/api/use-form-handler/modified-values.md index 4b1dfb4..b435ab4 100644 --- a/docs/api/use-form-handler/modified-values.md +++ b/docs/api/use-form-handler/modified-values.md @@ -10,26 +10,28 @@ Coming soon... ```vue - ``` @@ -40,4 +42,4 @@ Let's say your form is initialized as above, because you're editing an existing ```ts export type ModifiedValues = () => Record -``` \ No newline at end of file +``` diff --git a/docs/api/use-form-handler/register.md b/docs/api/use-form-handler/register.md index 564e886..8033a5e 100644 --- a/docs/api/use-form-handler/register.md +++ b/docs/api/use-form-handler/register.md @@ -8,60 +8,57 @@ Coming soon... ## Props -| attribute | type | description | -|-----------|-------------------|---------------------------------------| -| name | `string` | Name for the field | -| options | [RegisterOptions](/api/use-form-handler/register.html#type-declarations) | Optional configuration for the field | +| attribute | type | description | +| --------- | ------------------------------------------------------------------------ | ------------------------------------ | +| name | `string` | Name for the field | +| options | [RegisterOptions](/api/use-form-handler/register.html#type-declarations) | Optional configuration for the field | ### `options` -| attribute | type | description | -|-----------|-------------------|---------------------------------------| -| native | `boolean` | Explicitly indicates if the field is a native input or not. The main idea of this is to avoid binding the native input handler to custom components. | -| defaultValue| `any` | Default value for the field, would override the fallback value when the field is empty/cleared | -| validate | [Validations](/api/use-form-handler/register.html#type-declarations) | [Custom validations](/get-started/quick-start.html#custom-validation) object. | -| withDetails | `boolean` | Explicitly indicates if you want to bind dirty and and touched state for the registered field | -| disabled | `boolean` | Disables the field. When a field is disabled it gets reset, and is not able to validate, or set new values until it is enabled again. The field is also not considered for the form validation. | -| useNativeValidation | `boolean` | Set to true if you want to use native HTML validation | - - +| attribute | type | description | +| ------------------- | -------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| native | `boolean` | Explicitly indicates if the field is a native input or not. The main idea of this is to avoid binding the native input handler to custom components. | +| defaultValue | `any` | Default value for the field, would override the fallback value when the field is empty/cleared | +| validate | [Validations](/api/use-form-handler/register.html#type-declarations) | [Custom validations](/get-started/quick-start.html#custom-validation) object. | +| withDetails | `boolean` | Explicitly indicates if you want to bind dirty and and touched state for the registered field | +| disabled | `boolean` | Disables the field. When a field is disabled it gets reset, and is not able to validate, or set new values until it is enabled again. The field is also not considered for the form validation. | +| useNativeValidation | `boolean` | Set to true if you want to use native HTML validation | ## Return -| attribute | type | description | -|-----------|-------------------|---------------------------------------| -| name | `string` | Name of the field | -| error | string | undefined | Current error of the field | -| ref | `(fieldRef:any) => void` | Takes care of updating and keeping the fields correctly aligned with the form, specially when talking about native inputs | -| modelValue | `any` | Current field value binding for non-native inputs | -| 'onUpdate:modelValue' | `(value: any) => Promise` | Value update handler for non-native inputs | -| onBlur | `() => void` | Blur handler | -| onClear | `() => void` | Clear handler | -| disabled | `boolean` | Disabled state binding for the field | -| isDirty | `boolean` | Dirty state binding for the field. Only returned if `withDetails` is true | -| isTouched | `boolean` | Touched state binding for the field. Only returned if `withDetails` is true | -| onChange | `(el: any) => Promise` | Value update handler for native inputs | -| required | `boolean \| string` | Native required validation. Only returned if `useNativeValidations` is set to true and `required` is set. | -| min | `number \| Object` | Native min validation. Only returned if `useNativeValidations` is set to true and `min` is set. | -| max | `number \| Object` | Native max validation. Only returned if `useNativeValidations` is set to true and `max` is set. | -| minLength | `number \| Object` | Native minLength validation. Only returned if `useNativeValidations` is set to true and `minLength` is set. | -| maxLength | `number \| Object` | Native maxLength validation. Only returned if `useNativeValidations` is set to true and `maxLength` is set. | -| pattern | `string \| RegExp \| Object` | Native pattern validation. Only returned if `useNativeValidations` is set to true and `pattern` is set. | +| attribute | type | description | +| --------------------- | ------------------------------------ | ------------------------------------------------------------------------------------------------------------------------- | +| name | `string` | Name of the field | +| error | string | undefined | Current error of the field | +| ref | `(fieldRef:any) => void` | Takes care of updating and keeping the fields correctly aligned with the form, specially when talking about native inputs | +| modelValue | `any` | Current field value binding for non-native inputs | +| 'onUpdate:modelValue' | `(value: any) => Promise` | Value update handler for non-native inputs | +| onBlur | `() => void` | Blur handler | +| onClear | `() => void` | Clear handler | +| disabled | `boolean` | Disabled state binding for the field | +| isDirty | `boolean` | Dirty state binding for the field. Only returned if `withDetails` is true | +| isTouched | `boolean` | Touched state binding for the field. Only returned if `withDetails` is true | +| onChange | `(el: any) => Promise` | Value update handler for native inputs | +| required | `boolean \| string` | Native required validation. Only returned if `useNativeValidations` is set to true and `required` is set. | +| min | `number \| Object` | Native min validation. Only returned if `useNativeValidations` is set to true and `min` is set. | +| max | `number \| Object` | Native max validation. Only returned if `useNativeValidations` is set to true and `max` is set. | +| minLength | `number \| Object` | Native minLength validation. Only returned if `useNativeValidations` is set to true and `minLength` is set. | +| maxLength | `number \| Object` | Native maxLength validation. Only returned if `useNativeValidations` is set to true and `maxLength` is set. | +| pattern | `string \| RegExp \| Object` | Native pattern validation. Only returned if `useNativeValidations` is set to true and `pattern` is set. | :::info Notice how `modelValue` and `'onUpdate:modelValue'` are used as our two way data binding for non-native inputs following the Vue [approach](https://vuejs.org/guide/components/v-model.html). So that your fields used for complex forms could also be re-used in other parts of your application with v-model. ::: - ## Usage ### Basic ```vue - @@ -73,19 +70,24 @@ The usage is very simple and intuitive, we just get the `register` function from ```vue - @@ -94,41 +96,55 @@ const { register } = useFormHandler() As you can see, setting a default value for a field, in this case the country, is very simple, we just give it the value we want and it will be the `default`, not to confuse with the `initial` value, clearing the field will return it to it's default value, or to a value fallback if no default is specified. :::warning -The handler supports also initialization via html attributes like `selected` or `checked` but it is highly recommended to just use the tools that are provided. +The handler supports also initialization via html attributes like `selected` or `checked` but it is highly recommended to just use the tools that are provided. ::: ### In-built Validation ```vue