@prototyp/skeletor 1.0.6 → 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.
Files changed (188) hide show
  1. package/README.md +1 -102
  2. package/lib/module/hooks/index.js +1 -1
  3. package/lib/module/hooks/index.js.map +1 -1
  4. package/lib/module/hooks/skeleform/README.md +100 -0
  5. package/lib/module/hooks/skeleform/package.json +53 -0
  6. package/lib/module/hooks/{useForm.js → skeleform/src/index.js} +1 -1
  7. package/lib/module/hooks/skeleform/src/index.js.map +1 -0
  8. package/lib/module/hooks/skeleform/tsconfig.json +14 -0
  9. package/lib/module/hooks/skeleform/yarn.lock +5132 -0
  10. package/lib/typescript/components/Block/Block.d.ts.map +1 -0
  11. package/lib/typescript/components/Block/index.d.ts.map +1 -0
  12. package/lib/typescript/components/InputFocusScrollView/InputFocusScrollView.d.ts.map +1 -0
  13. package/lib/typescript/components/InputFocusScrollView/index.d.ts.map +1 -0
  14. package/lib/typescript/components/Screen/Screen.d.ts.map +1 -0
  15. package/lib/typescript/components/Screen/index.d.ts.map +1 -0
  16. package/lib/typescript/components/SkeletorProvider/SkeletorContext.d.ts.map +1 -0
  17. package/lib/typescript/components/SkeletorProvider/SkeletorProvider.d.ts.map +1 -0
  18. package/lib/typescript/components/SkeletorProvider/index.d.ts.map +1 -0
  19. package/lib/typescript/components/Text/Text.d.ts.map +1 -0
  20. package/lib/typescript/components/Text/index.d.ts.map +1 -0
  21. package/lib/typescript/components/index.d.ts.map +1 -0
  22. package/lib/typescript/{lib/module/hooks → hooks}/index.d.ts +1 -1
  23. package/lib/typescript/hooks/index.d.ts.map +1 -0
  24. package/lib/typescript/{src/hooks/useForm.d.ts → hooks/skeleform/src/index.d.ts} +1 -1
  25. package/lib/typescript/hooks/skeleform/src/index.d.ts.map +1 -0
  26. package/lib/typescript/hooks/useAndroidBackHandler.d.ts.map +1 -0
  27. package/lib/typescript/hooks/useAnimation.d.ts.map +1 -0
  28. package/lib/typescript/hooks/useAnimationTimeline.d.ts.map +1 -0
  29. package/lib/typescript/hooks/useAppState.d.ts.map +1 -0
  30. package/lib/typescript/hooks/useSkeletor.d.ts.map +1 -0
  31. package/lib/typescript/index.d.ts.map +1 -0
  32. package/lib/typescript/models/Alignment.d.ts.map +1 -0
  33. package/lib/typescript/models/Border.d.ts.map +1 -0
  34. package/lib/typescript/models/Size.d.ts.map +1 -0
  35. package/lib/typescript/models/SkeletorConfig.d.ts.map +1 -0
  36. package/lib/typescript/models/Spacing.d.ts.map +1 -0
  37. package/lib/typescript/models/index.d.ts.map +1 -0
  38. package/lib/typescript/utils/extractAlignmentProperties.d.ts.map +1 -0
  39. package/lib/typescript/utils/extractSizeProperties.d.ts.map +1 -0
  40. package/lib/typescript/utils/index.d.ts.map +1 -0
  41. package/package.json +7 -6
  42. package/lib/module/hooks/useForm.js.map +0 -1
  43. package/lib/typescript/lib/module/components/Block/Block.d.ts +0 -4
  44. package/lib/typescript/lib/module/components/Block/Block.d.ts.map +0 -1
  45. package/lib/typescript/lib/module/components/Block/index.d.ts.map +0 -1
  46. package/lib/typescript/lib/module/components/InputFocusScrollView/InputFocusScrollView.d.ts +0 -4
  47. package/lib/typescript/lib/module/components/InputFocusScrollView/InputFocusScrollView.d.ts.map +0 -1
  48. package/lib/typescript/lib/module/components/InputFocusScrollView/index.d.ts.map +0 -1
  49. package/lib/typescript/lib/module/components/Screen/Screen.d.ts +0 -5
  50. package/lib/typescript/lib/module/components/Screen/Screen.d.ts.map +0 -1
  51. package/lib/typescript/lib/module/components/Screen/index.d.ts.map +0 -1
  52. package/lib/typescript/lib/module/components/SkeletorProvider/SkeletorContext.d.ts +0 -14
  53. package/lib/typescript/lib/module/components/SkeletorProvider/SkeletorContext.d.ts.map +0 -1
  54. package/lib/typescript/lib/module/components/SkeletorProvider/SkeletorProvider.d.ts +0 -8
  55. package/lib/typescript/lib/module/components/SkeletorProvider/SkeletorProvider.d.ts.map +0 -1
  56. package/lib/typescript/lib/module/components/SkeletorProvider/index.d.ts.map +0 -1
  57. package/lib/typescript/lib/module/components/Text/Text.d.ts +0 -4
  58. package/lib/typescript/lib/module/components/Text/Text.d.ts.map +0 -1
  59. package/lib/typescript/lib/module/components/Text/index.d.ts.map +0 -1
  60. package/lib/typescript/lib/module/components/index.d.ts.map +0 -1
  61. package/lib/typescript/lib/module/hooks/index.d.ts.map +0 -1
  62. package/lib/typescript/lib/module/hooks/useAndroidBackHandler.d.ts +0 -2
  63. package/lib/typescript/lib/module/hooks/useAndroidBackHandler.d.ts.map +0 -1
  64. package/lib/typescript/lib/module/hooks/useAnimation.d.ts +0 -9
  65. package/lib/typescript/lib/module/hooks/useAnimation.d.ts.map +0 -1
  66. package/lib/typescript/lib/module/hooks/useAnimationTimeline.d.ts +0 -10
  67. package/lib/typescript/lib/module/hooks/useAnimationTimeline.d.ts.map +0 -1
  68. package/lib/typescript/lib/module/hooks/useAppState.d.ts +0 -2
  69. package/lib/typescript/lib/module/hooks/useAppState.d.ts.map +0 -1
  70. package/lib/typescript/lib/module/hooks/useForm.d.ts +0 -33
  71. package/lib/typescript/lib/module/hooks/useForm.d.ts.map +0 -1
  72. package/lib/typescript/lib/module/hooks/useSkeletor.d.ts +0 -7
  73. package/lib/typescript/lib/module/hooks/useSkeletor.d.ts.map +0 -1
  74. package/lib/typescript/lib/module/index.d.ts.map +0 -1
  75. package/lib/typescript/lib/module/models/Alignment.d.ts +0 -2
  76. package/lib/typescript/lib/module/models/Alignment.d.ts.map +0 -1
  77. package/lib/typescript/lib/module/models/Border.d.ts +0 -2
  78. package/lib/typescript/lib/module/models/Border.d.ts.map +0 -1
  79. package/lib/typescript/lib/module/models/Size.d.ts +0 -2
  80. package/lib/typescript/lib/module/models/Size.d.ts.map +0 -1
  81. package/lib/typescript/lib/module/models/SkeletorConfig.d.ts +0 -1
  82. package/lib/typescript/lib/module/models/SkeletorConfig.d.ts.map +0 -1
  83. package/lib/typescript/lib/module/models/Spacing.d.ts +0 -2
  84. package/lib/typescript/lib/module/models/Spacing.d.ts.map +0 -1
  85. package/lib/typescript/lib/module/models/index.d.ts +0 -5
  86. package/lib/typescript/lib/module/models/index.d.ts.map +0 -1
  87. package/lib/typescript/lib/module/types/Font.d.d.ts +0 -1
  88. package/lib/typescript/lib/module/types/Font.d.d.ts.map +0 -1
  89. package/lib/typescript/lib/module/utils/extractAlignmentProperties.d.ts +0 -8
  90. package/lib/typescript/lib/module/utils/extractAlignmentProperties.d.ts.map +0 -1
  91. package/lib/typescript/lib/module/utils/extractSizeProperties.d.ts +0 -10
  92. package/lib/typescript/lib/module/utils/extractSizeProperties.d.ts.map +0 -1
  93. package/lib/typescript/lib/module/utils/index.d.ts.map +0 -1
  94. package/lib/typescript/src/components/Block/Block.d.ts.map +0 -1
  95. package/lib/typescript/src/components/Block/index.d.ts +0 -2
  96. package/lib/typescript/src/components/Block/index.d.ts.map +0 -1
  97. package/lib/typescript/src/components/InputFocusScrollView/InputFocusScrollView.d.ts.map +0 -1
  98. package/lib/typescript/src/components/InputFocusScrollView/index.d.ts +0 -2
  99. package/lib/typescript/src/components/InputFocusScrollView/index.d.ts.map +0 -1
  100. package/lib/typescript/src/components/Screen/Screen.d.ts.map +0 -1
  101. package/lib/typescript/src/components/Screen/index.d.ts +0 -2
  102. package/lib/typescript/src/components/Screen/index.d.ts.map +0 -1
  103. package/lib/typescript/src/components/SkeletorProvider/SkeletorContext.d.ts.map +0 -1
  104. package/lib/typescript/src/components/SkeletorProvider/SkeletorProvider.d.ts.map +0 -1
  105. package/lib/typescript/src/components/SkeletorProvider/index.d.ts +0 -3
  106. package/lib/typescript/src/components/SkeletorProvider/index.d.ts.map +0 -1
  107. package/lib/typescript/src/components/Text/Text.d.ts.map +0 -1
  108. package/lib/typescript/src/components/Text/index.d.ts +0 -2
  109. package/lib/typescript/src/components/Text/index.d.ts.map +0 -1
  110. package/lib/typescript/src/components/index.d.ts +0 -6
  111. package/lib/typescript/src/components/index.d.ts.map +0 -1
  112. package/lib/typescript/src/hooks/index.d.ts +0 -7
  113. package/lib/typescript/src/hooks/index.d.ts.map +0 -1
  114. package/lib/typescript/src/hooks/useAndroidBackHandler.d.ts.map +0 -1
  115. package/lib/typescript/src/hooks/useAnimation.d.ts.map +0 -1
  116. package/lib/typescript/src/hooks/useAnimationTimeline.d.ts.map +0 -1
  117. package/lib/typescript/src/hooks/useAppState.d.ts.map +0 -1
  118. package/lib/typescript/src/hooks/useForm.d.ts.map +0 -1
  119. package/lib/typescript/src/hooks/useSkeletor.d.ts.map +0 -1
  120. package/lib/typescript/src/index.d.ts +0 -5
  121. package/lib/typescript/src/index.d.ts.map +0 -1
  122. package/lib/typescript/src/models/Alignment.d.ts.map +0 -1
  123. package/lib/typescript/src/models/Border.d.ts.map +0 -1
  124. package/lib/typescript/src/models/Size.d.ts.map +0 -1
  125. package/lib/typescript/src/models/SkeletorConfig.d.ts.map +0 -1
  126. package/lib/typescript/src/models/Spacing.d.ts.map +0 -1
  127. package/lib/typescript/src/models/index.d.ts.map +0 -1
  128. package/lib/typescript/src/utils/extractAlignmentProperties.d.ts.map +0 -1
  129. package/lib/typescript/src/utils/extractSizeProperties.d.ts.map +0 -1
  130. package/lib/typescript/src/utils/index.d.ts +0 -3
  131. package/lib/typescript/src/utils/index.d.ts.map +0 -1
  132. package/src/components/Block/Block.tsx +0 -106
  133. package/src/components/Block/index.ts +0 -1
  134. package/src/components/InputFocusScrollView/InputFocusScrollView.tsx +0 -113
  135. package/src/components/InputFocusScrollView/index.ts +0 -1
  136. package/src/components/Screen/Screen.tsx +0 -91
  137. package/src/components/Screen/index.ts +0 -1
  138. package/src/components/SkeletorProvider/SkeletorContext.ts +0 -12
  139. package/src/components/SkeletorProvider/SkeletorProvider.tsx +0 -18
  140. package/src/components/SkeletorProvider/index.ts +0 -2
  141. package/src/components/Text/Text.tsx +0 -99
  142. package/src/components/Text/index.ts +0 -1
  143. package/src/components/index.ts +0 -5
  144. package/src/hooks/index.ts +0 -6
  145. package/src/hooks/useAndroidBackHandler.ts +0 -45
  146. package/src/hooks/useAnimation.ts +0 -62
  147. package/src/hooks/useAnimationTimeline.ts +0 -139
  148. package/src/hooks/useAppState.ts +0 -26
  149. package/src/hooks/useForm.ts +0 -208
  150. package/src/hooks/useSkeletor.ts +0 -7
  151. package/src/index.ts +0 -4
  152. package/src/models/Alignment.ts +0 -9
  153. package/src/models/Border.ts +0 -17
  154. package/src/models/Size.ts +0 -11
  155. package/src/models/SkeletorConfig.ts +0 -6
  156. package/src/models/Spacing.ts +0 -22
  157. package/src/models/index.ts +0 -5
  158. package/src/types/Font.d.ts +0 -1
  159. package/src/utils/extractAlignmentProperties.ts +0 -13
  160. package/src/utils/extractSizeProperties.ts +0 -13
  161. package/src/utils/index.ts +0 -2
  162. /package/lib/typescript/{src/components → components}/Block/Block.d.ts +0 -0
  163. /package/lib/typescript/{lib/module/components → components}/Block/index.d.ts +0 -0
  164. /package/lib/typescript/{src/components → components}/InputFocusScrollView/InputFocusScrollView.d.ts +0 -0
  165. /package/lib/typescript/{lib/module/components → components}/InputFocusScrollView/index.d.ts +0 -0
  166. /package/lib/typescript/{src/components → components}/Screen/Screen.d.ts +0 -0
  167. /package/lib/typescript/{lib/module/components → components}/Screen/index.d.ts +0 -0
  168. /package/lib/typescript/{src/components → components}/SkeletorProvider/SkeletorContext.d.ts +0 -0
  169. /package/lib/typescript/{src/components → components}/SkeletorProvider/SkeletorProvider.d.ts +0 -0
  170. /package/lib/typescript/{lib/module/components → components}/SkeletorProvider/index.d.ts +0 -0
  171. /package/lib/typescript/{src/components → components}/Text/Text.d.ts +0 -0
  172. /package/lib/typescript/{lib/module/components → components}/Text/index.d.ts +0 -0
  173. /package/lib/typescript/{lib/module/components → components}/index.d.ts +0 -0
  174. /package/lib/typescript/{src/hooks → hooks}/useAndroidBackHandler.d.ts +0 -0
  175. /package/lib/typescript/{src/hooks → hooks}/useAnimation.d.ts +0 -0
  176. /package/lib/typescript/{src/hooks → hooks}/useAnimationTimeline.d.ts +0 -0
  177. /package/lib/typescript/{src/hooks → hooks}/useAppState.d.ts +0 -0
  178. /package/lib/typescript/{src/hooks → hooks}/useSkeletor.d.ts +0 -0
  179. /package/lib/typescript/{lib/module/index.d.ts → index.d.ts} +0 -0
  180. /package/lib/typescript/{src/models → models}/Alignment.d.ts +0 -0
  181. /package/lib/typescript/{src/models → models}/Border.d.ts +0 -0
  182. /package/lib/typescript/{src/models → models}/Size.d.ts +0 -0
  183. /package/lib/typescript/{src/models → models}/SkeletorConfig.d.ts +0 -0
  184. /package/lib/typescript/{src/models → models}/Spacing.d.ts +0 -0
  185. /package/lib/typescript/{src/models → models}/index.d.ts +0 -0
  186. /package/lib/typescript/{src/utils → utils}/extractAlignmentProperties.d.ts +0 -0
  187. /package/lib/typescript/{src/utils → utils}/extractSizeProperties.d.ts +0 -0
  188. /package/lib/typescript/{lib/module/utils → utils}/index.d.ts +0 -0
@@ -1,139 +0,0 @@
1
- import { useEffect, useState } from "react";
2
- import { Animated, ViewStyle } from "react-native";
3
- import { AnimationSet } from "./useAnimation";
4
-
5
- interface BaseTimelineType {
6
- elements: AnimationSet<any>[];
7
- start: boolean;
8
- onFinished?: () => void;
9
- onStarted?: () => void;
10
- }
11
-
12
- type DelayTimeline = BaseTimelineType & {
13
- delay: number;
14
- };
15
- type StaggerTimeline = BaseTimelineType & {
16
- stagger: number;
17
- };
18
-
19
- interface TimelineConfiguration {
20
- delay?: DelayTimeline;
21
- parallel?: BaseTimelineType;
22
- sequence?: BaseTimelineType;
23
- stagger?: StaggerTimeline;
24
- }
25
-
26
- /** Used to layout animated values on a timeline and handle starting/reversing the animations.
27
- * Supports all Animated types (delay, stagger, parallel, sequence).
28
- * @example
29
- * useAnimationTimeline({
30
- stagger: { elements: [ring1, ring2, ring3], stagger: 900, start: show },
31
- sequence: { elements: [button], start: Boolean(show && !disabled) },
32
- });
33
- */
34
- export function useAnimationTimeline(config: TimelineConfiguration): void {
35
- const { delay, parallel, sequence, stagger } = config;
36
- const staggerStart = Boolean(stagger?.start);
37
- const delayStart = Boolean(delay?.start);
38
- const sequenceStart = Boolean(sequence?.start);
39
- const parallelStart = Boolean(parallel?.start);
40
-
41
- const [previousDelayStart, setPreviousDelayStart] = useState(false);
42
- const [previousStaggerStart, setPreviousStaggerStart] = useState(false);
43
- const [previousSequenceStart, setPreviousSequenceStart] = useState(false);
44
- const [previousParallelStart, setPreviousParallelStart] = useState(false);
45
-
46
- function getBaseAnimations(
47
- timeline: BaseTimelineType | StaggerTimeline | DelayTimeline,
48
- ) {
49
- const compositions: Animated.CompositeAnimation[] = [];
50
- timeline.elements.forEach(
51
- ({ values, animations, configuration, definitions }) => {
52
- const keys = Object.keys(animations).map(
53
- (key) => key as keyof Partial<ViewStyle>,
54
- );
55
-
56
- const elementCompositions = keys.map((key, index) => {
57
- const value = values[index];
58
- const lastValue = definitions[key]!.length - 1;
59
- const nativeAnimation = configuration.loop
60
- ? false
61
- : configuration.useNativeDriver || false;
62
-
63
- const base = Animated.timing(value, {
64
- toValue: timeline.start ? lastValue : 0,
65
- duration: configuration.duration,
66
- useNativeDriver: nativeAnimation,
67
- });
68
-
69
- return configuration.loop ? Animated.loop(base) : base;
70
- });
71
-
72
- compositions.push(Animated.parallel(elementCompositions));
73
- },
74
- );
75
-
76
- return compositions;
77
- }
78
-
79
- function setupParallelAnimations(timeline: BaseTimelineType) {
80
- if (timeline.onStarted) {
81
- timeline.onStarted();
82
- }
83
- Animated.parallel(getBaseAnimations(timeline)).start(timeline.onFinished);
84
- }
85
-
86
- function setupSequenceAnimations(timeline: BaseTimelineType) {
87
- if (timeline.onStarted) {
88
- timeline.onStarted();
89
- }
90
- Animated.sequence(getBaseAnimations(timeline)).start(timeline.onFinished);
91
- }
92
-
93
- function setupStaggerAnimations(timeline: StaggerTimeline) {
94
- if (timeline.onStarted) {
95
- timeline.onStarted();
96
- }
97
- Animated.stagger(timeline.stagger, getBaseAnimations(timeline)).start(
98
- timeline.onFinished,
99
- );
100
- }
101
-
102
- function setupDelayAnimations(timeline: DelayTimeline) {
103
- Animated.delay(timeline.delay).start(() =>
104
- setupParallelAnimations(timeline),
105
- );
106
- }
107
-
108
- useEffect(() => {
109
- if (stagger && previousStaggerStart !== staggerStart) {
110
- setupStaggerAnimations(stagger);
111
- }
112
-
113
- setPreviousStaggerStart(staggerStart);
114
- }, [previousStaggerStart, staggerStart, stagger]);
115
-
116
- useEffect(() => {
117
- if (parallel && previousParallelStart !== parallelStart) {
118
- setupParallelAnimations(parallel);
119
- }
120
-
121
- setPreviousParallelStart(parallelStart);
122
- }, [previousParallelStart, parallelStart, parallel]);
123
-
124
- useEffect(() => {
125
- if (sequence && previousSequenceStart !== sequenceStart) {
126
- setupSequenceAnimations(sequence);
127
- }
128
-
129
- setPreviousSequenceStart(sequenceStart);
130
- }, [previousSequenceStart, sequenceStart, sequence]);
131
-
132
- useEffect(() => {
133
- if (delay && previousDelayStart !== delayStart) {
134
- setupDelayAnimations(delay);
135
- }
136
-
137
- setPreviousDelayStart(delayStart);
138
- }, [previousDelayStart, delayStart, delay]);
139
- }
@@ -1,26 +0,0 @@
1
- import { useCallback, useEffect } from "react";
2
- import { AppState, AppStateStatus } from "react-native";
3
-
4
- interface Config {
5
- onForeground?: () => void;
6
- onBackground?: () => void;
7
- }
8
-
9
- export function useAppState({ onForeground, onBackground }: Config) {
10
- const handleAppStateChange = useCallback(
11
- (state: AppStateStatus) => {
12
- if (state.match(/background|inactive/)) {
13
- onBackground?.();
14
- }
15
- if (state === "active") {
16
- onForeground?.();
17
- }
18
- },
19
- [onBackground, onForeground],
20
- );
21
-
22
- useEffect(() => {
23
- const event = AppState.addEventListener("change", handleAppStateChange);
24
- return event.remove;
25
- }, [handleAppStateChange]);
26
- }
@@ -1,208 +0,0 @@
1
- import { useEffect, useState } from "react";
2
-
3
- export type Validation<T> = { [K in keyof Partial<T>]?: boolean };
4
- type Rules<T> = {
5
- [K in keyof T]?: (
6
- value: T[K],
7
- state: T,
8
- optional: boolean
9
- ) => boolean | undefined;
10
- };
11
-
12
- export type Values<T> = {
13
- [K in keyof T]: T[K];
14
- };
15
-
16
- export type FormConfig<R> = {
17
- /** List of optional properties. Optional properties will be marked as valid if left empty. */
18
- optional?: Array<keyof R>;
19
- /** Validation rules by specified property name. If you define a validation rule function here, the field will be validated against it. If no rule is set, a crude value check will be used instead (optional || Boolean(value)). Note: For complex value types (objects, arrays, dates etc) it is best to use a custom validation rule. */
20
- rules?: Rules<R>;
21
- /** Will compare key/value pairs instead of just top level JSON.stringify on complex objects/arrays */
22
- deepCompare?: boolean;
23
- logging?: boolean;
24
- };
25
-
26
- /** One-fits-all solution to manage state changes, field validation and optional entries within a form.
27
- * @example <caption>Simple use case:</caption>
28
- * const { state, validation, update } = useForm({ email: '', password: '', });
29
- *
30
- *
31
- * @example <caption>For more complex form states (ie one field can be of multiple types), you should pass the form's type:</caption>
32
- * const { state, validation, update } = useForm<{ numericOrUndefined: number | undefined }>({ numericOrUndefined: undefined }, { rules: { numericOrUndefined: (value: number | undefined): boolean | undefined => ... }});
33
- *
34
- */
35
- export function useForm<T>(values: Values<T>, config?: FormConfig<T>) {
36
- const keys = Object.keys(values) as Array<keyof T>;
37
- const [validation, setValidation] = useState<Validation<T>>({});
38
- const [initialState, setInitialState] = useState(values);
39
- const [state, setState] = useState(values);
40
- const { fieldValidation, stateValidation, isOptional } = useFormUtils(config);
41
-
42
- useEffect(() => {
43
- const changed = keys.filter((key) => values[key] !== initialState[key]);
44
- if (!changed.length) {
45
- return;
46
- }
47
-
48
- const updatedState = { ...state };
49
- changed.forEach((key) => (updatedState[key] = values[key]));
50
-
51
- setInitialState({ ...values, ...updatedState });
52
- setState(updatedState);
53
- }, [values]);
54
-
55
- /** This function updates the specific property with a new value and validates it if it needs to do so.
56
- * @example <caption>Usage:</caption>
57
- * <TextInput ... onChange={(event) => update("nameOfProp", event.nativeEvent.text, false)} onBlur={(event) => update("nameOfProp", event.nativeEvent.text, true)}
58
- * */
59
- function update<K extends keyof T>(
60
- key: K,
61
- value: Values<T>[K],
62
- shouldValidate?: boolean
63
- ) {
64
- setState((s) => ({ ...s, [key]: value }));
65
- setValidation((s) => ({
66
- ...s,
67
- [key]: shouldValidate ? fieldValidation(key, value, state) : undefined,
68
- }));
69
- }
70
-
71
- function validate<K extends keyof T>(key: K) {
72
- setValidation((s) => ({
73
- ...s,
74
- [key]: fieldValidation(key, state[key], state),
75
- }));
76
- }
77
-
78
- /** Validate entire form, store validation state and return validation value.
79
- * In human readable terms, use this when you want to validate the form on submit.
80
- */
81
- function validateForm(): boolean {
82
- const formValidationState = stateValidation(state);
83
- setValidation(formValidationState.validation);
84
- return formValidationState.valid;
85
- }
86
-
87
- /** Boolean value of whether the form is valid (ie can be submitted). Use this to disable/enable form submission.
88
- * Only use when validating fields separately, has no value when valiating on form submit. */
89
- function isFormValid() {
90
- return !keys.some((key) =>
91
- isOptional(key) ? validation[key] === false : !validation[key]
92
- );
93
- }
94
-
95
- /** Resets changed values to initial state */
96
- function resetState() {
97
- setState(values);
98
- }
99
-
100
- /** Re-initializes initial state from currently passed values. Use this if you need to reinitialize the form after the source values change, i.e. if a different state object comes into play after submitting the form (such as reducer data or API endpoint response data) */
101
- function resetInitialValues() {
102
- setInitialState(values);
103
- }
104
-
105
- /** Resets validation to initial state. */
106
- function resetValidation() {
107
- setValidation({});
108
- }
109
-
110
- /** Resets entire form, state and validation included */
111
- function clearForm() {
112
- resetInitialValues();
113
- resetState();
114
- resetValidation();
115
- }
116
-
117
- return {
118
- state,
119
- validation,
120
- update,
121
- validate,
122
- validateForm,
123
- isFormValid,
124
- clearForm,
125
- resetState,
126
- resetValidation,
127
- resetInitialValues,
128
- };
129
- }
130
-
131
- /** Helper hook to validate form state outside of the scope of useForm. */
132
- export function useFormUtils<T>(config?: FormConfig<T>) {
133
- function doesValueExist(
134
- value: T[keyof T]
135
- ): value is Exclude<T[keyof T], null | undefined> {
136
- if (value === undefined || value === null) {
137
- return false;
138
- }
139
-
140
- if (typeof value === "string") {
141
- return value !== "";
142
- }
143
-
144
- if (typeof value === "number") {
145
- return value >= 0 || value < 0 || !isNaN(value);
146
- }
147
-
148
- if (value instanceof Date) {
149
- return !isNaN(value.valueOf());
150
- }
151
-
152
- return true;
153
- }
154
-
155
- function isOptional(key: keyof T) {
156
- return config?.optional?.includes(key) || false;
157
- }
158
-
159
- /** Validate by custom validation rule. If the rule does not exist, returns undefined. */
160
- function validateByRule<K extends keyof T>(
161
- key: K,
162
- value: T[K],
163
- state: Values<T>
164
- ) {
165
- return config?.rules?.[key]?.(value, state, isOptional(key));
166
- }
167
-
168
- /** Handles validation for a specific form field.
169
- * Order of priority:
170
- * 1. If there is a custom validation rule, always use that to preserve all possible type values
171
- * 2. If there is no value (is a falsy value), check if the field is optional
172
- * 3. Fallback to simple truthy value check if all other checks are not triggered. */
173
- function fieldValidation(key: keyof T, value: T[keyof T], state: Values<T>) {
174
- const hasValue = doesValueExist(value);
175
- const optional = isOptional(key);
176
-
177
- // If form has custom validation rule, always trigger only that.
178
- if (!!config?.rules?.[key]) return validateByRule(key, value, state);
179
- // If value does not exist (is null, undefined or other falsy value), check if field is optional.
180
- if (!hasValue) return !!optional;
181
- // Fallback, simple check if value exists
182
- return hasValue;
183
- }
184
-
185
- function stateValidation(state: Values<T>) {
186
- const keys = Object.keys(state).map((key) => key as keyof T);
187
- const validation: Validation<T> = {};
188
-
189
- keys.forEach((key) => {
190
- const value = state[key];
191
- // Force true / false values for entire form. Undefined has no value when submitting.
192
- validation[key] = fieldValidation(key, value, state) || false;
193
- });
194
-
195
- return {
196
- valid: !keys.some((key) => !validation[key]),
197
- validation,
198
- };
199
- }
200
-
201
- return {
202
- doesValueExist,
203
- validateByRule,
204
- isOptional,
205
- fieldValidation,
206
- stateValidation,
207
- };
208
- }
@@ -1,7 +0,0 @@
1
- import { useContext } from "react";
2
- import { SkeletorConfig } from "../models";
3
- import { SkeletorContext } from "../components";
4
-
5
- export function useSkeletor(): SkeletorConfig {
6
- return useContext(SkeletorContext);
7
- }
package/src/index.ts DELETED
@@ -1,4 +0,0 @@
1
- export * from "./components";
2
- export * from "./hooks";
3
- export * from "./models";
4
- export * from "./utils";
@@ -1,9 +0,0 @@
1
- import { ViewStyle } from "react-native";
2
-
3
- export interface Alignment {
4
- align?: ViewStyle["alignItems"];
5
- alignSelf?: ViewStyle["alignSelf"];
6
- justify?: ViewStyle["justifyContent"];
7
- flexDirection?: ViewStyle["flexDirection"];
8
- flexWrap?: ViewStyle["flexWrap"];
9
- }
@@ -1,17 +0,0 @@
1
- import { AnimatableNumericValue } from "react-native/types";
2
-
3
- export interface Border {
4
- border?: {
5
- borderWidth?: number;
6
- borderTopWidth?: number;
7
- borderBottomWidth?: number;
8
- borderLeftWidth?: number;
9
- borderRightWidth?: number;
10
- borderColor?: string;
11
- borderRadius?: AnimatableNumericValue;
12
- borderTopLeftRadius?: AnimatableNumericValue;
13
- borderTopRightRadius?: AnimatableNumericValue;
14
- borderBottomLeftRadius?: AnimatableNumericValue;
15
- borderBottomRightRadius?: AnimatableNumericValue;
16
- };
17
- }
@@ -1,11 +0,0 @@
1
- import { DimensionValue } from "react-native/types";
2
-
3
- export interface Size {
4
- width?: DimensionValue;
5
- height?: DimensionValue;
6
- minHeight?: DimensionValue;
7
- minWidth?: DimensionValue;
8
- maxHeight?: DimensionValue;
9
- maxWidth?: DimensionValue;
10
- flex?: number;
11
- }
@@ -1,6 +0,0 @@
1
- export interface SkeletorConfig {
2
- defaultFont: Font | undefined;
3
- defaultFontSize: [number, number] | number;
4
- defaultStatusBarType: "dark-content" | "light-content" | "default";
5
- defaultTextColor: string;
6
- }
@@ -1,22 +0,0 @@
1
- import { DimensionValue } from "react-native/types";
2
-
3
- export interface Spacing {
4
- margins?: {
5
- marginTop?: DimensionValue;
6
- marginBottom?: DimensionValue;
7
- marginLeft?: DimensionValue;
8
- marginRight?: DimensionValue;
9
- marginHorizontal?: DimensionValue;
10
- marginVertical?: DimensionValue;
11
- margin?: DimensionValue;
12
- };
13
- paddings?: {
14
- paddingTop?: DimensionValue;
15
- paddingBottom?: DimensionValue;
16
- paddingLeft?: DimensionValue;
17
- paddingRight?: DimensionValue;
18
- paddingHorizontal?: DimensionValue;
19
- paddingVertical?: DimensionValue;
20
- padding?: DimensionValue;
21
- };
22
- }
@@ -1,5 +0,0 @@
1
- export * from "./Alignment";
2
- export * from "./Border";
3
- export * from "./Size";
4
- export * from "./SkeletorConfig";
5
- export * from "./Spacing";
@@ -1 +0,0 @@
1
- type Font = string;
@@ -1,13 +0,0 @@
1
- import { Alignment } from "../models";
2
-
3
- export function extractAlignmentProperties<Props extends Alignment>(
4
- props: Props,
5
- ): Alignment {
6
- return {
7
- align: props.align,
8
- alignSelf: props.alignSelf,
9
- justify: props.justify,
10
- flexDirection: props.flexDirection,
11
- flexWrap: props.flexWrap,
12
- };
13
- }
@@ -1,13 +0,0 @@
1
- import { Size } from "../models";
2
-
3
- export function extractSizeProperties<Props extends Size>(props: Props): Size {
4
- return {
5
- flex: props.flex,
6
- height: props.height,
7
- width: props.width,
8
- maxHeight: props.maxHeight,
9
- maxWidth: props.maxWidth,
10
- minHeight: props.minHeight,
11
- minWidth: props.minWidth,
12
- };
13
- }
@@ -1,2 +0,0 @@
1
- export * from "./extractAlignmentProperties";
2
- export * from "./extractSizeProperties";
File without changes