@prototyp/skeletor 1.0.5 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -96
- package/lib/module/hooks/index.js +1 -1
- package/lib/module/hooks/index.js.map +1 -1
- package/lib/module/hooks/skeleform/README.md +100 -0
- package/lib/module/hooks/skeleform/package.json +53 -0
- package/lib/module/hooks/{useForm.js → skeleform/src/index.js} +31 -16
- package/lib/module/hooks/skeleform/src/index.js.map +1 -0
- package/lib/module/hooks/skeleform/tsconfig.json +14 -0
- package/lib/module/hooks/skeleform/yarn.lock +5132 -0
- package/lib/typescript/components/Block/Block.d.ts.map +1 -0
- package/lib/typescript/components/Block/index.d.ts.map +1 -0
- package/lib/typescript/components/InputFocusScrollView/InputFocusScrollView.d.ts.map +1 -0
- package/lib/typescript/components/InputFocusScrollView/index.d.ts.map +1 -0
- package/lib/typescript/components/Screen/Screen.d.ts.map +1 -0
- package/lib/typescript/components/Screen/index.d.ts.map +1 -0
- package/lib/typescript/components/SkeletorProvider/SkeletorContext.d.ts.map +1 -0
- package/lib/typescript/components/SkeletorProvider/SkeletorProvider.d.ts.map +1 -0
- package/lib/typescript/components/SkeletorProvider/index.d.ts.map +1 -0
- package/lib/typescript/components/Text/Text.d.ts.map +1 -0
- package/lib/typescript/components/Text/index.d.ts.map +1 -0
- package/lib/typescript/components/index.d.ts.map +1 -0
- package/lib/typescript/{lib/module/hooks → hooks}/index.d.ts +1 -1
- package/lib/typescript/hooks/index.d.ts.map +1 -0
- package/lib/typescript/{src/hooks/useForm.d.ts → hooks/skeleform/src/index.d.ts} +8 -8
- package/lib/typescript/hooks/skeleform/src/index.d.ts.map +1 -0
- package/lib/typescript/hooks/useAndroidBackHandler.d.ts.map +1 -0
- package/lib/typescript/hooks/useAnimation.d.ts.map +1 -0
- package/lib/typescript/hooks/useAnimationTimeline.d.ts.map +1 -0
- package/lib/typescript/hooks/useAppState.d.ts.map +1 -0
- package/lib/typescript/hooks/useSkeletor.d.ts.map +1 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/models/Alignment.d.ts.map +1 -0
- package/lib/typescript/models/Border.d.ts.map +1 -0
- package/lib/typescript/models/Size.d.ts.map +1 -0
- package/lib/typescript/models/SkeletorConfig.d.ts.map +1 -0
- package/lib/typescript/models/Spacing.d.ts.map +1 -0
- package/lib/typescript/models/index.d.ts.map +1 -0
- package/lib/typescript/utils/extractAlignmentProperties.d.ts.map +1 -0
- package/lib/typescript/utils/extractSizeProperties.d.ts.map +1 -0
- package/lib/typescript/utils/index.d.ts.map +1 -0
- package/package.json +7 -6
- package/lib/module/hooks/useForm.js.map +0 -1
- package/lib/typescript/lib/module/components/Block/Block.d.ts +0 -4
- package/lib/typescript/lib/module/components/Block/Block.d.ts.map +0 -1
- package/lib/typescript/lib/module/components/Block/index.d.ts.map +0 -1
- package/lib/typescript/lib/module/components/InputFocusScrollView/InputFocusScrollView.d.ts +0 -4
- package/lib/typescript/lib/module/components/InputFocusScrollView/InputFocusScrollView.d.ts.map +0 -1
- package/lib/typescript/lib/module/components/InputFocusScrollView/index.d.ts.map +0 -1
- package/lib/typescript/lib/module/components/Screen/Screen.d.ts +0 -5
- package/lib/typescript/lib/module/components/Screen/Screen.d.ts.map +0 -1
- package/lib/typescript/lib/module/components/Screen/index.d.ts.map +0 -1
- package/lib/typescript/lib/module/components/SkeletorProvider/SkeletorContext.d.ts +0 -14
- package/lib/typescript/lib/module/components/SkeletorProvider/SkeletorContext.d.ts.map +0 -1
- package/lib/typescript/lib/module/components/SkeletorProvider/SkeletorProvider.d.ts +0 -8
- package/lib/typescript/lib/module/components/SkeletorProvider/SkeletorProvider.d.ts.map +0 -1
- package/lib/typescript/lib/module/components/SkeletorProvider/index.d.ts.map +0 -1
- package/lib/typescript/lib/module/components/Text/Text.d.ts +0 -4
- package/lib/typescript/lib/module/components/Text/Text.d.ts.map +0 -1
- package/lib/typescript/lib/module/components/Text/index.d.ts.map +0 -1
- package/lib/typescript/lib/module/components/index.d.ts.map +0 -1
- package/lib/typescript/lib/module/hooks/index.d.ts.map +0 -1
- package/lib/typescript/lib/module/hooks/useAndroidBackHandler.d.ts +0 -2
- package/lib/typescript/lib/module/hooks/useAndroidBackHandler.d.ts.map +0 -1
- package/lib/typescript/lib/module/hooks/useAnimation.d.ts +0 -9
- package/lib/typescript/lib/module/hooks/useAnimation.d.ts.map +0 -1
- package/lib/typescript/lib/module/hooks/useAnimationTimeline.d.ts +0 -10
- package/lib/typescript/lib/module/hooks/useAnimationTimeline.d.ts.map +0 -1
- package/lib/typescript/lib/module/hooks/useAppState.d.ts +0 -2
- package/lib/typescript/lib/module/hooks/useAppState.d.ts.map +0 -1
- package/lib/typescript/lib/module/hooks/useForm.d.ts +0 -33
- package/lib/typescript/lib/module/hooks/useForm.d.ts.map +0 -1
- package/lib/typescript/lib/module/hooks/useSkeletor.d.ts +0 -7
- package/lib/typescript/lib/module/hooks/useSkeletor.d.ts.map +0 -1
- package/lib/typescript/lib/module/index.d.ts.map +0 -1
- package/lib/typescript/lib/module/models/Alignment.d.ts +0 -2
- package/lib/typescript/lib/module/models/Alignment.d.ts.map +0 -1
- package/lib/typescript/lib/module/models/Border.d.ts +0 -2
- package/lib/typescript/lib/module/models/Border.d.ts.map +0 -1
- package/lib/typescript/lib/module/models/Size.d.ts +0 -2
- package/lib/typescript/lib/module/models/Size.d.ts.map +0 -1
- package/lib/typescript/lib/module/models/SkeletorConfig.d.ts +0 -1
- package/lib/typescript/lib/module/models/SkeletorConfig.d.ts.map +0 -1
- package/lib/typescript/lib/module/models/Spacing.d.ts +0 -2
- package/lib/typescript/lib/module/models/Spacing.d.ts.map +0 -1
- package/lib/typescript/lib/module/models/index.d.ts +0 -5
- package/lib/typescript/lib/module/models/index.d.ts.map +0 -1
- package/lib/typescript/lib/module/types/Font.d.d.ts +0 -1
- package/lib/typescript/lib/module/types/Font.d.d.ts.map +0 -1
- package/lib/typescript/lib/module/utils/extractAlignmentProperties.d.ts +0 -8
- package/lib/typescript/lib/module/utils/extractAlignmentProperties.d.ts.map +0 -1
- package/lib/typescript/lib/module/utils/extractSizeProperties.d.ts +0 -10
- package/lib/typescript/lib/module/utils/extractSizeProperties.d.ts.map +0 -1
- package/lib/typescript/lib/module/utils/index.d.ts.map +0 -1
- package/lib/typescript/src/components/Block/Block.d.ts.map +0 -1
- package/lib/typescript/src/components/Block/index.d.ts +0 -2
- package/lib/typescript/src/components/Block/index.d.ts.map +0 -1
- package/lib/typescript/src/components/InputFocusScrollView/InputFocusScrollView.d.ts.map +0 -1
- package/lib/typescript/src/components/InputFocusScrollView/index.d.ts +0 -2
- package/lib/typescript/src/components/InputFocusScrollView/index.d.ts.map +0 -1
- package/lib/typescript/src/components/Screen/Screen.d.ts.map +0 -1
- package/lib/typescript/src/components/Screen/index.d.ts +0 -2
- package/lib/typescript/src/components/Screen/index.d.ts.map +0 -1
- package/lib/typescript/src/components/SkeletorProvider/SkeletorContext.d.ts.map +0 -1
- package/lib/typescript/src/components/SkeletorProvider/SkeletorProvider.d.ts.map +0 -1
- package/lib/typescript/src/components/SkeletorProvider/index.d.ts +0 -3
- package/lib/typescript/src/components/SkeletorProvider/index.d.ts.map +0 -1
- package/lib/typescript/src/components/Text/Text.d.ts.map +0 -1
- package/lib/typescript/src/components/Text/index.d.ts +0 -2
- package/lib/typescript/src/components/Text/index.d.ts.map +0 -1
- package/lib/typescript/src/components/index.d.ts +0 -6
- package/lib/typescript/src/components/index.d.ts.map +0 -1
- package/lib/typescript/src/hooks/index.d.ts +0 -7
- package/lib/typescript/src/hooks/index.d.ts.map +0 -1
- package/lib/typescript/src/hooks/useAndroidBackHandler.d.ts.map +0 -1
- package/lib/typescript/src/hooks/useAnimation.d.ts.map +0 -1
- package/lib/typescript/src/hooks/useAnimationTimeline.d.ts.map +0 -1
- package/lib/typescript/src/hooks/useAppState.d.ts.map +0 -1
- package/lib/typescript/src/hooks/useForm.d.ts.map +0 -1
- package/lib/typescript/src/hooks/useSkeletor.d.ts.map +0 -1
- package/lib/typescript/src/index.d.ts +0 -5
- package/lib/typescript/src/index.d.ts.map +0 -1
- package/lib/typescript/src/models/Alignment.d.ts.map +0 -1
- package/lib/typescript/src/models/Border.d.ts.map +0 -1
- package/lib/typescript/src/models/Size.d.ts.map +0 -1
- package/lib/typescript/src/models/SkeletorConfig.d.ts.map +0 -1
- package/lib/typescript/src/models/Spacing.d.ts.map +0 -1
- package/lib/typescript/src/models/index.d.ts.map +0 -1
- package/lib/typescript/src/utils/extractAlignmentProperties.d.ts.map +0 -1
- package/lib/typescript/src/utils/extractSizeProperties.d.ts.map +0 -1
- package/lib/typescript/src/utils/index.d.ts +0 -3
- package/lib/typescript/src/utils/index.d.ts.map +0 -1
- package/src/components/Block/Block.tsx +0 -106
- package/src/components/Block/index.ts +0 -1
- package/src/components/InputFocusScrollView/InputFocusScrollView.tsx +0 -113
- package/src/components/InputFocusScrollView/index.ts +0 -1
- package/src/components/Screen/Screen.tsx +0 -91
- package/src/components/Screen/index.ts +0 -1
- package/src/components/SkeletorProvider/SkeletorContext.ts +0 -12
- package/src/components/SkeletorProvider/SkeletorProvider.tsx +0 -18
- package/src/components/SkeletorProvider/index.ts +0 -2
- package/src/components/Text/Text.tsx +0 -99
- package/src/components/Text/index.ts +0 -1
- package/src/components/index.ts +0 -5
- package/src/hooks/index.ts +0 -6
- package/src/hooks/useAndroidBackHandler.ts +0 -45
- package/src/hooks/useAnimation.ts +0 -62
- package/src/hooks/useAnimationTimeline.ts +0 -139
- package/src/hooks/useAppState.ts +0 -26
- package/src/hooks/useForm.ts +0 -190
- package/src/hooks/useSkeletor.ts +0 -7
- package/src/index.ts +0 -4
- package/src/models/Alignment.ts +0 -9
- package/src/models/Border.ts +0 -17
- package/src/models/Size.ts +0 -11
- package/src/models/SkeletorConfig.ts +0 -6
- package/src/models/Spacing.ts +0 -22
- package/src/models/index.ts +0 -5
- package/src/types/Font.d.ts +0 -1
- package/src/utils/extractAlignmentProperties.ts +0 -13
- package/src/utils/extractSizeProperties.ts +0 -13
- package/src/utils/index.ts +0 -2
- /package/lib/typescript/{src/components → components}/Block/Block.d.ts +0 -0
- /package/lib/typescript/{lib/module/components → components}/Block/index.d.ts +0 -0
- /package/lib/typescript/{src/components → components}/InputFocusScrollView/InputFocusScrollView.d.ts +0 -0
- /package/lib/typescript/{lib/module/components → components}/InputFocusScrollView/index.d.ts +0 -0
- /package/lib/typescript/{src/components → components}/Screen/Screen.d.ts +0 -0
- /package/lib/typescript/{lib/module/components → components}/Screen/index.d.ts +0 -0
- /package/lib/typescript/{src/components → components}/SkeletorProvider/SkeletorContext.d.ts +0 -0
- /package/lib/typescript/{src/components → components}/SkeletorProvider/SkeletorProvider.d.ts +0 -0
- /package/lib/typescript/{lib/module/components → components}/SkeletorProvider/index.d.ts +0 -0
- /package/lib/typescript/{src/components → components}/Text/Text.d.ts +0 -0
- /package/lib/typescript/{lib/module/components → components}/Text/index.d.ts +0 -0
- /package/lib/typescript/{lib/module/components → components}/index.d.ts +0 -0
- /package/lib/typescript/{src/hooks → hooks}/useAndroidBackHandler.d.ts +0 -0
- /package/lib/typescript/{src/hooks → hooks}/useAnimation.d.ts +0 -0
- /package/lib/typescript/{src/hooks → hooks}/useAnimationTimeline.d.ts +0 -0
- /package/lib/typescript/{src/hooks → hooks}/useAppState.d.ts +0 -0
- /package/lib/typescript/{src/hooks → hooks}/useSkeletor.d.ts +0 -0
- /package/lib/typescript/{lib/module/index.d.ts → index.d.ts} +0 -0
- /package/lib/typescript/{src/models → models}/Alignment.d.ts +0 -0
- /package/lib/typescript/{src/models → models}/Border.d.ts +0 -0
- /package/lib/typescript/{src/models → models}/Size.d.ts +0 -0
- /package/lib/typescript/{src/models → models}/SkeletorConfig.d.ts +0 -0
- /package/lib/typescript/{src/models → models}/Spacing.d.ts +0 -0
- /package/lib/typescript/{src/models → models}/index.d.ts +0 -0
- /package/lib/typescript/{src/utils → utils}/extractAlignmentProperties.d.ts +0 -0
- /package/lib/typescript/{src/utils → utils}/extractSizeProperties.d.ts +0 -0
- /package/lib/typescript/{lib/module/utils → utils}/index.d.ts +0 -0
package/README.md
CHANGED
|
@@ -242,102 +242,7 @@ height?: "full" | "auto";
|
|
|
242
242
|
|
|
243
243
|
### useForm & useFormUtils
|
|
244
244
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
Handle form value updates and validation with `useForm`. Full TypeScript support, ability to configure optional parameters, custom validation rules.
|
|
248
|
-
|
|
249
|
-
Flexible in its function, supports multiple validation approaches through callbacks:
|
|
250
|
-
|
|
251
|
-
1. For on-change validation, use `update("prop", value, true)`
|
|
252
|
-
2. To trigger standalone validation, use `validate("prop")`
|
|
253
|
-
3. Validate entire form with `validateForm()`
|
|
254
|
-
|
|
255
|
-
#### Example 1: Simple use case with standalone validation on blur:
|
|
256
|
-
|
|
257
|
-
```javascript
|
|
258
|
-
const {state, validation, update, validate} = useForm({email: "", password: "");
|
|
259
|
-
|
|
260
|
-
...
|
|
261
|
-
<Input
|
|
262
|
-
keyboardType="email-address"
|
|
263
|
-
returnKeyType="next"
|
|
264
|
-
label="E-mail Address"
|
|
265
|
-
errorMessage="Email is not valid."
|
|
266
|
-
emptyMessage="Email is a required field."
|
|
267
|
-
placeholder="What's your email address?"
|
|
268
|
-
value={state.email}
|
|
269
|
-
textContentType="emailAddress"
|
|
270
|
-
valid={validation.email}
|
|
271
|
-
onChangeText={(text) => update("email", text)}
|
|
272
|
-
onBlur={() => validate("email")}
|
|
273
|
-
/>
|
|
274
|
-
...
|
|
275
|
-
```
|
|
276
|
-
|
|
277
|
-
#### Example 2: Simple use case with on-change validation:
|
|
278
|
-
|
|
279
|
-
```javascript
|
|
280
|
-
// See example 1
|
|
281
|
-
...
|
|
282
|
-
onChangeText={(text) => update("email", text, true)}
|
|
283
|
-
...
|
|
284
|
-
```
|
|
285
|
-
|
|
286
|
-
#### Example 3: Simple use case with on-submit validation:
|
|
287
|
-
|
|
288
|
-
```javascript
|
|
289
|
-
// See example 1
|
|
290
|
-
|
|
291
|
-
function submit() {
|
|
292
|
-
if(!validateForm()) {
|
|
293
|
-
// Throw invalid error
|
|
294
|
-
// validation object will be populated
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
...
|
|
299
|
-
<Input
|
|
300
|
-
keyboardType="email-address"
|
|
301
|
-
returnKeyType="next"
|
|
302
|
-
label="E-mail Address"
|
|
303
|
-
errorMessage="Email is not valid."
|
|
304
|
-
emptyMessage="Email is a required field."
|
|
305
|
-
placeholder="What's your email address?"
|
|
306
|
-
value={state.email}
|
|
307
|
-
textContentType="emailAddress"
|
|
308
|
-
valid={validation.email}
|
|
309
|
-
onChangeText={(text) => update("email", text)}
|
|
310
|
-
/>
|
|
311
|
-
<Button onPress={submit}>Submit</Button>
|
|
312
|
-
```
|
|
313
|
-
|
|
314
|
-
#### Example 4: Form configuration
|
|
315
|
-
|
|
316
|
-
```javascript
|
|
317
|
-
const { state, validation, update, validate } = useForm(
|
|
318
|
-
{ firstName: "", middleName: "", lastName: ""},
|
|
319
|
-
{
|
|
320
|
-
// If left empty, validation.middleName will be true
|
|
321
|
-
optional: ["middleName"],
|
|
322
|
-
// validation.lastName is invalid if lastName is <3 characters long
|
|
323
|
-
// state can be used to compare with other values (ie repeat password)
|
|
324
|
-
rules: { lastName: (value, state) => value.length >= 3,
|
|
325
|
-
}
|
|
326
|
-
```
|
|
327
|
-
|
|
328
|
-
#### Example 5: useFormUtils
|
|
329
|
-
|
|
330
|
-
Utility functions to help with standalone state validation, such as when re-validating a list of form values in a parent components.
|
|
331
|
-
|
|
332
|
-
```javascript
|
|
333
|
-
const { stateValidation } = useFormUtils<Person>();
|
|
334
|
-
|
|
335
|
-
function validatePeople() {
|
|
336
|
-
return people.every(person => stateValidation(person).valid);
|
|
337
|
-
}
|
|
338
|
-
```
|
|
339
|
-
|
|
340
|
-
Other utilities: `doesValueExist`, `validateByRule`, `isOptional`, `fieldValidation`, `stateValidation`. Some are meant for internal `useForm` usage, such as `fieldValidation` and `validateByRule`, but are not unusable.
|
|
245
|
+
Read documentation about useForm here: https://github.com/prototypdigital/skeleform
|
|
341
246
|
|
|
342
247
|
### useAnimation & useAnimationTimeline
|
|
343
248
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export * from "./useForm";
|
|
2
1
|
export * from "./useAnimation";
|
|
3
2
|
export * from "./useAnimationTimeline";
|
|
4
3
|
export * from "./useAppState";
|
|
5
4
|
export * from "./useSkeletor";
|
|
6
5
|
export * from "./useAndroidBackHandler";
|
|
6
|
+
export * from "./skeleform/src/index";
|
|
7
7
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":[],"sourceRoot":"../../src","sources":["index.ts"],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../src","sources":["index.ts"],"mappings":"AAAA,cAAc,gBAAgB;AAC9B,cAAc,wBAAwB;AACtC,cAAc,eAAe;AAC7B,cAAc,eAAe;AAC7B,cAAc,yBAAyB;AACvC,cAAc,uBAAuB"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# skeleform
|
|
2
|
+
|
|
3
|
+
Form management and validation toolkit originally a part of https://github.com/prototypdigital/skeletor
|
|
4
|
+
|
|
5
|
+
### Usage
|
|
6
|
+
|
|
7
|
+
Handle form value updates and validation with `useForm`. Full TypeScript support, ability to configure optional parameters, custom validation rules.
|
|
8
|
+
|
|
9
|
+
Flexible in its function, supports multiple validation approaches through callbacks:
|
|
10
|
+
|
|
11
|
+
1. For on-change validation, use `update("prop", value, true)`
|
|
12
|
+
2. To trigger standalone validation, use `validate("prop")`
|
|
13
|
+
3. Validate entire form with `validateForm()`
|
|
14
|
+
|
|
15
|
+
The order of importance when it comes to validating fields is:
|
|
16
|
+
|
|
17
|
+
1. Custom validation rule - will get the value as-is for your rule to validate it fully. The configuration rule receives the current value, state and optional flag values for you to validate against.
|
|
18
|
+
2. If there is no custom validation rule and the primitive value check returns false (there is no value), check if the field is optional. If optional, the validation result is `true`, otherwise it is `false`;
|
|
19
|
+
3. If all previous checks have not been triggered, return the primitive "has value" check result. This will return `false` for values such as: `null | undefined | NaN | "" | Invalid Date`. This will return `true` if you have a complex value type such as `object | Array`, <b>so in case you have a complex value type use a custom validation rule to get what you need</b>.
|
|
20
|
+
|
|
21
|
+
#### Example 1: Simple use case with standalone validation on blur:
|
|
22
|
+
|
|
23
|
+
```javascript
|
|
24
|
+
const {state, validation, update, validate} = useForm({email: "", password: "");
|
|
25
|
+
|
|
26
|
+
...
|
|
27
|
+
<input
|
|
28
|
+
...
|
|
29
|
+
value={state.email}
|
|
30
|
+
onChange={(e) => update("email", e.currentTarget.value)}
|
|
31
|
+
onBlur={() => validate("email")}
|
|
32
|
+
/>
|
|
33
|
+
...
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
#### Example 2: Simple use case with on-change validation:
|
|
37
|
+
|
|
38
|
+
```javascript
|
|
39
|
+
// See example 1
|
|
40
|
+
...
|
|
41
|
+
onChange={(e) => update("email", e.currentTarget.value, true)}
|
|
42
|
+
...
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
#### Example 3: Simple use case with on-submit validation:
|
|
46
|
+
|
|
47
|
+
```javascript
|
|
48
|
+
// See example 1
|
|
49
|
+
|
|
50
|
+
function submit() {
|
|
51
|
+
if(!validateForm()) {
|
|
52
|
+
// Throw invalid error
|
|
53
|
+
// validation object will be populated
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
...
|
|
58
|
+
<input
|
|
59
|
+
...
|
|
60
|
+
value={state.email}
|
|
61
|
+
onUpdate={(e) => update("email", e.currentTarget.value)}
|
|
62
|
+
/>
|
|
63
|
+
<button onPress={submit}>Submit</button>
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Validation has three possible values:
|
|
67
|
+
|
|
68
|
+
1. `undefined` = validation of the field value has not been done yet.
|
|
69
|
+
2. `false` = validation has failed, the state value is not considered valid.
|
|
70
|
+
3. `true` = validation has succeeded, the state value is considered valid.
|
|
71
|
+
|
|
72
|
+
What does that mean? Well, it means that when you have `validation.email === false`, you should show an error message on the field. `undefined | true` are not relevant for rendering, only functionally to block form submit events.
|
|
73
|
+
|
|
74
|
+
#### Example 4: Form configuration
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
const { state, validation, update, validate } = useForm(
|
|
78
|
+
{ firstName: "", middleName: "", lastName: ""},
|
|
79
|
+
{
|
|
80
|
+
// If left empty, validation.middleName will be true
|
|
81
|
+
optional: ["middleName"],
|
|
82
|
+
// validation.lastName is invalid if lastName is <3 characters long
|
|
83
|
+
// state can be used to compare with other values (ie repeat password)
|
|
84
|
+
rules: { lastName: (value, state, optional) => value.length >= 3,
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
#### Example 5: useFormUtils
|
|
89
|
+
|
|
90
|
+
Utility functions to help with standalone state validation, such as when re-validating a list of form values in a parent components.
|
|
91
|
+
|
|
92
|
+
```javascript
|
|
93
|
+
const { stateValidation } = useFormUtils<Person>();
|
|
94
|
+
|
|
95
|
+
function validatePeople() {
|
|
96
|
+
return people.every(person => stateValidation(person).valid);
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Other utilities: `doesValueExist`, `validateByRule`, `isOptional`, `fieldValidation`, `stateValidation`. Some are meant for internal `useForm` usage, such as `fieldValidation` and `validateByRule`, but are not unusable.
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@prototyp/skeleform",
|
|
3
|
+
"description": "React/React-Native form management utility",
|
|
4
|
+
"author": "Luka Buljan <luka@prototyp.digital>",
|
|
5
|
+
"version": "1.0.1",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/prototypdigital/skeleform",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"directory": "https://github.com/prototypdigital/skeleform"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"main": "dist/index.js",
|
|
14
|
+
"types": "dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"prepack": "tsc"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/jest": "29.4.0",
|
|
26
|
+
"@types/react": "^18.0.24",
|
|
27
|
+
"jest": "29.4.2",
|
|
28
|
+
"react": "^18.2.0",
|
|
29
|
+
"typescript": "4.8.4"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"react",
|
|
36
|
+
"react-native",
|
|
37
|
+
"typescript",
|
|
38
|
+
"form",
|
|
39
|
+
"input",
|
|
40
|
+
"state",
|
|
41
|
+
"validation",
|
|
42
|
+
"helpers",
|
|
43
|
+
"utilities",
|
|
44
|
+
"utils"
|
|
45
|
+
],
|
|
46
|
+
"files": [
|
|
47
|
+
"dist",
|
|
48
|
+
"README.md"
|
|
49
|
+
],
|
|
50
|
+
"eslintIgnore": [
|
|
51
|
+
"node_modules/"
|
|
52
|
+
]
|
|
53
|
+
}
|
|
@@ -108,32 +108,47 @@ export function useForm(values, config) {
|
|
|
108
108
|
/** Helper hook to validate form state outside of the scope of useForm. */
|
|
109
109
|
export function useFormUtils(config) {
|
|
110
110
|
function doesValueExist(value) {
|
|
111
|
-
if (value ===
|
|
111
|
+
if (value === undefined || value === null) {
|
|
112
112
|
return false;
|
|
113
113
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
114
|
+
if (typeof value === "string") {
|
|
115
|
+
return value !== "";
|
|
116
|
+
}
|
|
117
|
+
if (typeof value === "number") {
|
|
118
|
+
return value >= 0 || value < 0 || !isNaN(value);
|
|
119
|
+
}
|
|
120
|
+
if (value instanceof Date) {
|
|
121
|
+
return !isNaN(value.valueOf());
|
|
121
122
|
}
|
|
122
|
-
// else return true because we know the value exists already.
|
|
123
123
|
return true;
|
|
124
124
|
}
|
|
125
125
|
function isOptional(key) {
|
|
126
126
|
var _config$optional;
|
|
127
|
-
return config === null || config === void 0 ? void 0 : (_config$optional = config.optional) === null || _config$optional === void 0 ? void 0 : _config$optional.includes(key);
|
|
127
|
+
return (config === null || config === void 0 ? void 0 : (_config$optional = config.optional) === null || _config$optional === void 0 ? void 0 : _config$optional.includes(key)) || false;
|
|
128
128
|
}
|
|
129
|
+
|
|
130
|
+
/** Validate by custom validation rule. If the rule does not exist, returns undefined. */
|
|
131
|
+
function validateByRule(key, value, state) {
|
|
132
|
+
var _config$rules, _config$rules$key;
|
|
133
|
+
return config === null || config === void 0 ? void 0 : (_config$rules = config.rules) === null || _config$rules === void 0 ? void 0 : (_config$rules$key = _config$rules[key]) === null || _config$rules$key === void 0 ? void 0 : _config$rules$key.call(_config$rules, value, state, isOptional(key));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Handles validation for a specific form field.
|
|
137
|
+
* Order of priority:
|
|
138
|
+
* 1. If there is a custom validation rule, always use that to preserve all possible type values
|
|
139
|
+
* 2. If there is no value (is a falsy value), check if the field is optional
|
|
140
|
+
* 3. Fallback to simple truthy value check if all other checks are not triggered. */
|
|
129
141
|
function fieldValidation(key, value, state) {
|
|
142
|
+
var _config$rules2;
|
|
130
143
|
const hasValue = doesValueExist(value);
|
|
131
144
|
const optional = isOptional(key);
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
145
|
+
|
|
146
|
+
// If form has custom validation rule, always trigger only that.
|
|
147
|
+
if (!!(config !== null && config !== void 0 && (_config$rules2 = config.rules) !== null && _config$rules2 !== void 0 && _config$rules2[key])) return validateByRule(key, value, state);
|
|
148
|
+
// If value does not exist (is null, undefined or other falsy value), check if field is optional.
|
|
149
|
+
if (!hasValue) return !!optional;
|
|
150
|
+
// Fallback, simple check if value exists
|
|
151
|
+
return hasValue;
|
|
137
152
|
}
|
|
138
153
|
function stateValidation(state) {
|
|
139
154
|
const keys = Object.keys(state).map(key => key);
|
|
@@ -156,4 +171,4 @@ export function useFormUtils(config) {
|
|
|
156
171
|
stateValidation
|
|
157
172
|
};
|
|
158
173
|
}
|
|
159
|
-
//# sourceMappingURL=
|
|
174
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useEffect","useState","useForm","values","config","keys","Object","validation","setValidation","initialState","setInitialState","state","setState","fieldValidation","stateValidation","isOptional","useFormUtils","changed","filter","key","length","updatedState","forEach","update","value","shouldValidate","s","undefined","validate","validateForm","formValidationState","valid","isFormValid","some","resetState","resetInitialValues","resetValidation","clearForm","doesValueExist","isNaN","Date","valueOf","optional","includes","validateByRule","rules","hasValue","map"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":"AAAA,SAASA,SAAS,EAAEC,QAAQ,QAAQ,OAAO;AAyB3C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,OAAO,CAAIC,MAAiB,EAAEC,MAAsB,EAAE;EACpE,MAAMC,IAAI,GAAGC,MAAM,CAACD,IAAI,CAACF,MAAM,CAAmB;EAClD,MAAM,CAACI,UAAU,EAAEC,aAAa,CAAC,GAAGP,QAAQ,CAAgB,CAAC,CAAC,CAAC;EAC/D,MAAM,CAACQ,YAAY,EAAEC,eAAe,CAAC,GAAGT,QAAQ,CAACE,MAAM,CAAC;EACxD,MAAM,CAACQ,KAAK,EAAEC,QAAQ,CAAC,GAAGX,QAAQ,CAACE,MAAM,CAAC;EAC1C,MAAM;IAAEU,eAAe;IAAEC,eAAe;IAAEC;EAAW,CAAC,GAAGC,YAAY,CAACZ,MAAM,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,UAAU,CAACY,GAAG,CAAC,GAAGN,eAAe,CAACM,GAAG,EAAEK,KAAK,EAAEb,KAAK,CAAC,IAAI,KAAK;IAC/D,CAAC,CAAC;IAEF,OAAO;MACLoB,KAAK,EAAE,CAAC1B,IAAI,CAAC4B,IAAI,CAAEd,GAAG,IAAK,CAACZ,UAAU,CAACY,GAAG,CAAC,CAAC;MAC5CZ;IACF,CAAC;EACH;EAEA,OAAO;IACL+B,cAAc;IACdM,cAAc;IACd7B,UAAU;IACVF,eAAe;IACfC;EACF,CAAC;AACH"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"include": ["src"] /* Include only the src directory */,
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
|
|
5
|
+
"module": "ES6" /* Specify what module code is generated. */,
|
|
6
|
+
"declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
|
|
7
|
+
"outDir": "dist",
|
|
8
|
+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
|
|
9
|
+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
|
10
|
+
"strict": true /* Enable all strict type-checking options. */,
|
|
11
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */,
|
|
12
|
+
"moduleResolution": "Node"
|
|
13
|
+
}
|
|
14
|
+
}
|