Skip to content
This repository was archived by the owner on Apr 9, 2025. It is now read-only.

Commit e551992

Browse files
author
Daniel Requejo
committed
Merge branch 'master' into feature/23-reduce-final-package-size
2 parents 52b2f69 + 9cae605 commit e551992

File tree

6 files changed

+44
-19
lines changed

6 files changed

+44
-19
lines changed

docs/.vitepress/config.cts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@ export default defineConfig({
3838
},
3939
{
4040
text: 'Guides', collapsible: true, items: [
41-
{ text: 'Validation', link: '/guides/validation' },
42-
{ text: 'Typescript', link: '/guides/typescript' },
43-
{ text: 'Form submission', link: '/guides/form-submission' },
4441
{ text: 'Custom components', link: '/guides/custom-components' },
42+
{ text: 'Material libraries', link: '/guides/material-libraries' },
4543
{ text: 'Native support', link: '/guides/native-support' },
44+
{ text: 'Typescript', link: '/guides/typescript' },
45+
{ text: 'Validation', link: '/guides/validation' },
4646
]
4747
},
4848
{
4949
text: 'Examples', collapsible: true, items: [
50-
{ text: 'Basic', link: '/examples/basic' },
5150
{ text: 'Async validations', link: '/examples/async-validations' },
52-
{ text: 'Typescript', link: '/examples/typescript' },
53-
{ text: 'Interceptor', link: '/examples/interceptor' },
51+
{ text: 'Basic', link: '/examples/basic' },
5452
{ text: 'Dependent fields', link: '/examples/dependent-fields' },
55-
{ text: 'More examples', link: '/examples/more-examples' }
53+
{ text: 'Interceptor', link: '/examples/interceptor' },
54+
{ text: 'More examples', link: '/examples/more-examples' },
55+
{ text: 'Typescript', link: '/examples/typescript' },
5656
]
5757
},
5858
{

docs/api/use-form-handler/register.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ Coming soon...
4141
| isDirty | `boolean` | Dirty state binding for the field. Only returned if `withDetails` is true |
4242
| isTouched | `boolean` | Touched state binding for the field. Only returned if `withDetails` is true |
4343
| onChange | `(el: any) => Promise<void>` | Value update handler for native inputs |
44-
| required | `boolean` | Native required validation. Only returned if `useNativeValidations` is set to true and `required` is set. |
45-
| min | `number` | Native min validation. Only returned if `useNativeValidations` is set to true and `min` is set. |
46-
| max | `number` | Native max validation. Only returned if `useNativeValidations` is set to true and `max` is set. |
47-
| minLength | `number` | Native minLength validation. Only returned if `useNativeValidations` is set to true and `minLength` is set. |
48-
| maxLength | `number` | Native maxLength validation. Only returned if `useNativeValidations` is set to true and `maxLength` is set. |
49-
| pattern | `RegExp` | Native pattern validation. Only returned if `useNativeValidations` is set to true and `pattern` is set. |
44+
| required | `boolean \| string` | Native required validation. Only returned if `useNativeValidations` is set to true and `required` is set. |
45+
| min | `number \| Object` | Native min validation. Only returned if `useNativeValidations` is set to true and `min` is set. |
46+
| max | `number \| Object` | Native max validation. Only returned if `useNativeValidations` is set to true and `max` is set. |
47+
| minLength | `number \| Object` | Native minLength validation. Only returned if `useNativeValidations` is set to true and `minLength` is set. |
48+
| maxLength | `number \| Object` | Native maxLength validation. Only returned if `useNativeValidations` is set to true and `maxLength` is set. |
49+
| pattern | `string \| RegExp \| Object` | Native pattern validation. Only returned if `useNativeValidations` is set to true and `pattern` is set. |
5050

5151
:::info
5252
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.
@@ -254,7 +254,7 @@ export interface ValidationsConfiguration {
254254
max?: number | ValidationWithMessage
255255
minLength?: number | ValidationWithMessage
256256
maxLength?: number | ValidationWithMessage
257-
pattern?: RegExp | ValidationWithMessage
257+
pattern?: string | RegExp | ValidationWithMessage
258258
}
259259

260260
export interface RegisterOptions extends ValidationsConfiguration {

src/logic/transformValidations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default (validations: ValidationsConfiguration = {}): Validations => {
3030
}),
3131
...(validations.pattern && {
3232
pattern: !(validations.pattern as ValidationWithMessage)?.value
33-
? pattern(validations.pattern as RegExp)
33+
? pattern((typeof validations.pattern === 'string' ? new RegExp(validations.pattern) : validations.pattern) as RegExp)
3434
: pattern((validations.pattern as ValidationWithMessage).value as RegExp, (validations.pattern as ValidationWithMessage).message as string)
3535
})
3636
}

src/types/register.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BaseControlProps } from "./baseControl";
2-
import { ValidationsConfiguration } from "./validations";
2+
import { ValidationsConfiguration, NativeValidations } from "./validations";
33

44
/** Function returning true for correct validation or a string with an error if it's invalid */
55
export type ValidationFn = (_: any) => Promise<true | string> | true | string
@@ -28,7 +28,7 @@ export interface RegisterOptions extends ValidationsConfiguration {
2828
useNativeValidation?: boolean
2929
}
3030

31-
export type RegisterReturn = BaseControlProps & ValidationsConfiguration
31+
export type RegisterReturn = BaseControlProps & NativeValidations
3232

3333
/** Function that allows you to register a control to interact with the form */
3434
export type Register = (name: string, options?: RegisterOptions) => RegisterReturn

src/types/validations.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@ export interface ValidationWithMessage {
66
message: string
77
}
88

9+
export interface NativeValidations {
10+
/** Required validation */
11+
required?: boolean
12+
13+
/** Min validation */
14+
min?: number
15+
16+
/** Max validation */
17+
max?: number
18+
19+
/** MinLength validation */
20+
minLength?: number
21+
22+
/** MaxLength validation */
23+
maxLength?: number
24+
25+
/** Pattern validation */
26+
pattern?: string
27+
}
28+
929
export interface ValidationsConfiguration {
1030
/** Required validation */
1131
/** If true, the field is required */
@@ -35,5 +55,5 @@ export interface ValidationsConfiguration {
3555
/** Pattern validation */
3656
/** If a RegExp, the field must match the RegExp */
3757
/** If an object, the field must match the object.value and the object.message is the validation message */
38-
pattern?: RegExp | ValidationWithMessage
58+
pattern?: string | RegExp | ValidationWithMessage
3959
}

src/useFormHandler.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { NativeValidations } from './types/validations';
12
import { DEFAULT_FIELD_VALUE } from './core/constants';
23
import {
34
ModifiedValues,
@@ -218,6 +219,7 @@ const useFormHandler: FormHandler = ({
218219
withDetails,
219220
native,
220221
useNativeValidation,
222+
pattern,
221223
...nativeValidations } = options
222224
_initControl(name, options);
223225
return ({
@@ -237,7 +239,10 @@ const useFormHandler: FormHandler = ({
237239
onChange: async () => await handleChange(name, getNativeFieldValue(_refs[name].ref)),
238240
}),
239241
...(useNativeValidation && {
240-
...nativeValidations
242+
...({
243+
...nativeValidations,
244+
...(pattern && { pattern: pattern instanceof RegExp ? pattern.source : pattern }),
245+
} as NativeValidations)
241246
})
242247
})
243248
}

0 commit comments

Comments
 (0)