@naptics/vue-collection 0.3.1 → 1.0.0-beta.1

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 (129) hide show
  1. package/eslint.config.cjs +0 -2
  2. package/package.json +32 -31
  3. package/postcss.config.js +1 -4
  4. package/src/demo/App.css +47 -3
  5. package/src/demo/components/ComponentSection.tsx +1 -1
  6. package/src/demo/views/NavigationView.tsx +1 -1
  7. package/src/demo/views/presentation/TooltipView.tsx +8 -2
  8. package/src/lib/components/NAlert.tsx +3 -3
  9. package/src/lib/components/NBadge.tsx +1 -1
  10. package/src/lib/components/NButton.tsx +6 -4
  11. package/src/lib/components/NCheckbox.tsx +2 -2
  12. package/src/lib/components/NDialog.tsx +2 -2
  13. package/src/lib/components/NDropdown.tsx +11 -9
  14. package/src/lib/components/NDropzone.tsx +4 -4
  15. package/src/lib/components/NIconButton.tsx +2 -2
  16. package/src/lib/components/NInput.tsx +1 -1
  17. package/src/lib/components/NLink.tsx +1 -1
  18. package/src/lib/components/NModal.tsx +4 -4
  19. package/src/lib/components/NPagination.css +4 -2
  20. package/src/lib/components/NPagination.tsx +1 -1
  21. package/src/lib/components/NSearchbar.tsx +1 -1
  22. package/src/lib/components/NSelect.tsx +1 -1
  23. package/src/lib/components/NSuggestionList.tsx +1 -1
  24. package/src/lib/components/NTable.css +2 -0
  25. package/src/lib/components/NTableAction.tsx +1 -1
  26. package/src/lib/components/NTextArea.tsx +1 -1
  27. package/src/lib/components/NTooltip.css +2 -0
  28. package/src/lib/components/NTooltip.tsx +42 -3
  29. package/src/lib/utils/component.tsx +1 -1
  30. package/tsconfig.lib.json +1 -0
  31. package/vite.config.ts +2 -0
  32. package/components/NAlert.d.ts +0 -29
  33. package/components/NAlert.js +0 -84
  34. package/components/NBadge.d.ts +0 -69
  35. package/components/NBadge.js +0 -64
  36. package/components/NBreadcrub.d.ts +0 -69
  37. package/components/NBreadcrub.js +0 -71
  38. package/components/NButton.d.ts +0 -60
  39. package/components/NButton.js +0 -72
  40. package/components/NCheckbox.d.ts +0 -20
  41. package/components/NCheckbox.js +0 -43
  42. package/components/NCheckboxLabel.d.ts +0 -26
  43. package/components/NCheckboxLabel.js +0 -42
  44. package/components/NCrudModal.d.ts +0 -118
  45. package/components/NCrudModal.js +0 -120
  46. package/components/NDialog.d.ts +0 -81
  47. package/components/NDialog.js +0 -161
  48. package/components/NDropdown.d.ts +0 -67
  49. package/components/NDropdown.js +0 -115
  50. package/components/NDropzone.d.ts +0 -61
  51. package/components/NDropzone.js +0 -218
  52. package/components/NForm.d.ts +0 -21
  53. package/components/NForm.js +0 -29
  54. package/components/NFormModal.d.ts +0 -75
  55. package/components/NFormModal.js +0 -59
  56. package/components/NIconButton.d.ts +0 -79
  57. package/components/NIconButton.js +0 -88
  58. package/components/NIconCircle.d.ts +0 -49
  59. package/components/NIconCircle.js +0 -67
  60. package/components/NInput.css +0 -11
  61. package/components/NInput.d.ts +0 -90
  62. package/components/NInput.js +0 -110
  63. package/components/NInputPhone.d.ts +0 -54
  64. package/components/NInputPhone.js +0 -47
  65. package/components/NInputSelect.d.ts +0 -99
  66. package/components/NInputSelect.js +0 -115
  67. package/components/NInputSuggestion.d.ts +0 -75
  68. package/components/NInputSuggestion.js +0 -64
  69. package/components/NLink.d.ts +0 -47
  70. package/components/NLink.js +0 -67
  71. package/components/NList.d.ts +0 -37
  72. package/components/NList.js +0 -40
  73. package/components/NLoadingIndicator.css +0 -46
  74. package/components/NLoadingIndicator.d.ts +0 -29
  75. package/components/NLoadingIndicator.js +0 -54
  76. package/components/NModal.d.ts +0 -133
  77. package/components/NModal.js +0 -232
  78. package/components/NPagination.css +0 -15
  79. package/components/NPagination.d.ts +0 -37
  80. package/components/NPagination.js +0 -105
  81. package/components/NSearchbar.d.ts +0 -39
  82. package/components/NSearchbar.js +0 -64
  83. package/components/NSearchbarList.d.ts +0 -33
  84. package/components/NSearchbarList.js +0 -41
  85. package/components/NSelect.d.ts +0 -78
  86. package/components/NSelect.js +0 -101
  87. package/components/NSuggestionList.d.ts +0 -153
  88. package/components/NSuggestionList.js +0 -160
  89. package/components/NTable.css +0 -3
  90. package/components/NTable.d.ts +0 -97
  91. package/components/NTable.js +0 -128
  92. package/components/NTableAction.d.ts +0 -30
  93. package/components/NTableAction.js +0 -50
  94. package/components/NTextArea.d.ts +0 -92
  95. package/components/NTextArea.js +0 -133
  96. package/components/NTooltip.css +0 -37
  97. package/components/NTooltip.d.ts +0 -136
  98. package/components/NTooltip.js +0 -207
  99. package/components/NValInput.d.ts +0 -155
  100. package/components/NValInput.js +0 -113
  101. package/components/ValidatedForm.d.ts +0 -39
  102. package/components/ValidatedForm.js +0 -35
  103. package/i18n/de/vue-collection.json +0 -58
  104. package/i18n/en/vue-collection.json +0 -58
  105. package/i18n/index.d.ts +0 -40
  106. package/i18n/index.js +0 -31
  107. package/index.d.ts +0 -2
  108. package/index.js +0 -2
  109. package/tailwind.config.js +0 -38
  110. package/utils/breakpoints.d.ts +0 -18
  111. package/utils/breakpoints.js +0 -40
  112. package/utils/component.d.ts +0 -57
  113. package/utils/component.js +0 -79
  114. package/utils/deferred.d.ts +0 -13
  115. package/utils/deferred.js +0 -17
  116. package/utils/identifiable.d.ts +0 -56
  117. package/utils/identifiable.js +0 -81
  118. package/utils/stringMaxLength.d.ts +0 -14
  119. package/utils/stringMaxLength.js +0 -23
  120. package/utils/tailwind.d.ts +0 -4
  121. package/utils/tailwind.js +0 -1
  122. package/utils/utils.d.ts +0 -47
  123. package/utils/utils.js +0 -56
  124. package/utils/vModel.d.ts +0 -182
  125. package/utils/vModel.js +0 -224
  126. package/utils/validation.d.ts +0 -90
  127. package/utils/validation.js +0 -147
  128. package/utils/vue.d.ts +0 -13
  129. package/utils/vue.js +0 -21
package/utils/vModel.d.ts DELETED
@@ -1,182 +0,0 @@
1
- import type { PropType, Ref } from 'vue';
2
- import type { AnyObject } from './utils';
3
- /**
4
- * Creates props for a `v-model` of the given type.
5
- * A `v-model` consits of a value-property and a update-function.
6
- * @param propType The propType of the `v-model`.
7
- * @returns An object containing the value-property and the update-function.
8
- */
9
- export declare function vModelProps<T>(propType: PropType<T>): {
10
- /**
11
- * The value of the component.
12
- */
13
- readonly value: PropType<T>;
14
- /**
15
- * This will be called, when the value of the component has changed.
16
- */
17
- readonly onUpdateValue: PropType<(newValue: T) => void>;
18
- };
19
- /**
20
- * Creates props for a required `v-model` of the given type.
21
- * @see {@link vModelProps}
22
- */
23
- export declare function vModelRequiredProps<T>(propType: PropType<T>): {
24
- /**
25
- * The value of the component.
26
- */
27
- readonly value: {
28
- readonly type: PropType<T>;
29
- readonly required: true;
30
- };
31
- /**
32
- * This will be called, when the value of the component has changed.
33
- */
34
- readonly onUpdateValue: PropType<(newValue: T) => void>;
35
- };
36
- /**
37
- * Creates props for a `v-model` of the given type with a default value.
38
- * @see {@link vModelProps}
39
- */
40
- export declare function vModelDefaultProps<T>(propType: PropType<T>, defaultValue: () => T): {
41
- /**
42
- * The value of the component.
43
- */
44
- readonly value: {
45
- readonly type: PropType<T>;
46
- readonly default: () => T;
47
- };
48
- /**
49
- * This will be called, when the value of the component has changed.
50
- */
51
- readonly onUpdateValue: PropType<(newValue: T) => void>;
52
- };
53
- /**
54
- * The `VModel` type contains a value and an optional update-function.
55
- * This object is passed to a component to create a two-way binding.
56
- */
57
- export type VModel<T, U = T> = {
58
- value: T;
59
- onUpdateValue?: (newValue: U) => void;
60
- };
61
- /**
62
- * The `VModelForArray` contains the normal `VModel` properties,
63
- * but aditionally a function, which removes the value from the array.
64
- */
65
- export type VModelForArray<T, U = T> = VModel<T, U> & {
66
- remove(): void;
67
- };
68
- /**
69
- * Uses the given `ref` as a `v-model`, to create a two-way binding with the `ref`.
70
- * @param ref The `ref` which should be used as the `v-model`.
71
- * @returns An object of type {@link VModel}, which connects the `ref` to the `v-model`.
72
- */
73
- export declare function vModelForRef<T>(ref: Ref<T>): VModel<T>;
74
- /**
75
- * This creates a `v-model` for a property of an object. The property of the object is taken as the
76
- * value of the `v-model`, the `onUpdate` function, is called every time when the property is updated
77
- * with the whole object and its updated property.
78
- * @param object The object which contains the relevant property.
79
- * @param key The key of the property which should be used as the v-model.
80
- * @param onUpdate The updater function which is called with the entire object when the property has changed.
81
- * @returns An object containing of type {@link VModel}.
82
- * @example
83
- * ```tsx
84
- * // inside setup function
85
- * const customer = ref({ firstName: 'Hansi', lastName: 'Halunk' })
86
- *
87
- * // This input needs a v-model for the `firstName` property.
88
- * return () => (
89
- * <NInput
90
- * name="First Name"
91
- * {...vModelForObjectProperty(customer.value, 'firstName', newVal => (customer.value = newValue))}
92
- * />
93
- * )
94
- * ```
95
- */
96
- export declare function vModelForObjectProperty<T extends AnyObject, K extends keyof T>(object: T, key: K, onUpdate?: (newValue: T) => void): VModel<T[K]>;
97
- /**
98
- * This creates a `v-model` which operates on one property of a parent `v-model`. It takes the value of
99
- * the property as the value of the `v-model` and calls the function `onUpdateValue` of the parent `v-model`
100
- * whenever the property changes. This function can be seen as a kind of mapper for a `v-model`.
101
- * @param vModel The parent `v-model`, which this function extracts a single property from.
102
- * @param key The key of the relevant property.
103
- * @returns An object of type {@link VModel}.
104
- * @example
105
- * ```tsx
106
- * type Customer = { firstName: string, lastName: String }
107
- *
108
- * // inside setup function,
109
- * // This vModel would normally be inside the props of a component e.g., `CustomerEditor`.
110
- * const parentVModel = {
111
- * value: { firstName: 'Hansi', lastName: 'Halunk' },
112
- * onUpdateValue: (newValue: Customer) => {
113
- * // update something
114
- * }
115
- * }
116
- *
117
- * // This input needs a v-model for the `firstName` property.
118
- * return () => (
119
- * <NInput
120
- * name="First Name"
121
- * {...vModelForVModelProperty(parentVModel, 'firstName'))}
122
- * />
123
- * )
124
- * ```
125
- */
126
- export declare function vModelForVModelProperty<Model extends VModel<ModelValue>, Key extends keyof ModelValue, ModelValue extends AnyObject = Model['value']>(vModel: Model, key: Key): VModel<ModelValue[Key]>;
127
- /**
128
- * This function does the same thing as {@link vModelForVModelProperty},
129
- * but can be provided with a mapper function for the modified property.
130
- * @see {@link vModelForVModelProperty}
131
- * @example
132
- * ```tsx
133
- * type Customer = { firstName: string, lastName: String, type: 'admin' | 'user' }
134
- *
135
- * // inside setup function,
136
- * // This vModel would normally be inside the props of a component e.g., `CustomerEditor`.
137
- * const parentVModel = {
138
- * value: { firstName: 'Hansi', lastName: 'Halunk', type: 'user' },
139
- * onUpdateValue: (newValue: Customer) => {
140
- * // update something
141
- * }
142
- * }
143
- *
144
- * // This input needs a v-model for the `type` property.
145
- * // Unfortunately the NSelect input expects an `onUpdateValue` with a
146
- * // parameter of type `string`, not type `admin | user`.
147
- * // So we have to provide a mapper function from `string` to `admin | user`.
148
- * // In this example, we just cast the value.
149
- * return () => (
150
- * <NSelect
151
- * name="Type"
152
- * {...vModelForVModelPropertyMapType(parentVModel, 'type', newVal => newVal as 'admin' | 'user'))}
153
- * />
154
- * )
155
- * ```
156
- */
157
- export declare function vModelForVModelPropertyMapType<Model extends VModel<ModelValue>, Key extends keyof ModelValue, UpdateValue, ModelValue extends AnyObject = Model['value']>(vModel: Model, key: Key, mapType: (value: UpdateValue) => ModelValue[Key]): VModel<ModelValue[Key], UpdateValue>;
158
- /**
159
- * Creates an array of `v-models`, one for every element of an array. All changes in
160
- * a `v-model` of any element, will call the `onUpdate` function, with the updated array.
161
- * @param array The array to create `v-models` for.
162
- * @param onUpdate The updater function, which is called whenever an element has changed.
163
- * @returns An object of type {@link VModelForArray}.
164
- * @example
165
- * ```tsx
166
- * // inside setup function
167
- * const todos = ref([
168
- * 'Create v-model helper functions.',
169
- * 'Document them!'
170
- * ])
171
- *
172
- * // For every todo, there should be an input to modifiy it.
173
- * return () => (
174
- * <div>
175
- * {vModelsForArray(todos.value, newValue => (todos.value = newValue)).map(todoVModel => (
176
- * <NInput name="Todo" {...todoVModel} />
177
- * ))}
178
- * </div>
179
- * )
180
- * ```
181
- */
182
- export declare function vModelsForArray<T>(array: T[], onUpdate?: (newValue: T[]) => void): VModelForArray<T>[];
package/utils/vModel.js DELETED
@@ -1,224 +0,0 @@
1
- /*
2
- * ---------- VModel as Props ----------
3
- */
4
- /**
5
- * Creates props for a `v-model` of the given type.
6
- * A `v-model` consits of a value-property and a update-function.
7
- * @param propType The propType of the `v-model`.
8
- * @returns An object containing the value-property and the update-function.
9
- */
10
- export function vModelProps(propType) {
11
- return {
12
- /**
13
- * The value of the component.
14
- */
15
- value: propType,
16
- /**
17
- * This will be called, when the value of the component has changed.
18
- */
19
- onUpdateValue: Function
20
- };
21
- }
22
- /**
23
- * Creates props for a required `v-model` of the given type.
24
- * @see {@link vModelProps}
25
- */
26
- export function vModelRequiredProps(propType) {
27
- return {
28
- /**
29
- * The value of the component.
30
- */
31
- value: {
32
- type: propType,
33
- required: true
34
- },
35
- /**
36
- * This will be called, when the value of the component has changed.
37
- */
38
- onUpdateValue: Function
39
- };
40
- }
41
- /**
42
- * Creates props for a `v-model` of the given type with a default value.
43
- * @see {@link vModelProps}
44
- */
45
- export function vModelDefaultProps(propType, defaultValue) {
46
- return {
47
- /**
48
- * The value of the component.
49
- */
50
- value: {
51
- type: propType,
52
- default: defaultValue
53
- },
54
- /**
55
- * This will be called, when the value of the component has changed.
56
- */
57
- onUpdateValue: Function
58
- };
59
- }
60
- /**
61
- * Uses the given `ref` as a `v-model`, to create a two-way binding with the `ref`.
62
- * @param ref The `ref` which should be used as the `v-model`.
63
- * @returns An object of type {@link VModel}, which connects the `ref` to the `v-model`.
64
- */
65
- export function vModelForRef(ref) {
66
- return {
67
- value: ref.value,
68
- onUpdateValue: newValue => {
69
- ref.value = newValue;
70
- }
71
- };
72
- }
73
- /**
74
- * This creates a `v-model` for a property of an object. The property of the object is taken as the
75
- * value of the `v-model`, the `onUpdate` function, is called every time when the property is updated
76
- * with the whole object and its updated property.
77
- * @param object The object which contains the relevant property.
78
- * @param key The key of the property which should be used as the v-model.
79
- * @param onUpdate The updater function which is called with the entire object when the property has changed.
80
- * @returns An object containing of type {@link VModel}.
81
- * @example
82
- * ```tsx
83
- * // inside setup function
84
- * const customer = ref({ firstName: 'Hansi', lastName: 'Halunk' })
85
- *
86
- * // This input needs a v-model for the `firstName` property.
87
- * return () => (
88
- * <NInput
89
- * name="First Name"
90
- * {...vModelForObjectProperty(customer.value, 'firstName', newVal => (customer.value = newValue))}
91
- * />
92
- * )
93
- * ```
94
- */
95
- export function vModelForObjectProperty(object, key, onUpdate) {
96
- return {
97
- value: object[key],
98
- onUpdateValue: newValue => {
99
- onUpdate?.({
100
- ...object,
101
- [key]: newValue
102
- });
103
- }
104
- };
105
- }
106
- /**
107
- * This creates a `v-model` which operates on one property of a parent `v-model`. It takes the value of
108
- * the property as the value of the `v-model` and calls the function `onUpdateValue` of the parent `v-model`
109
- * whenever the property changes. This function can be seen as a kind of mapper for a `v-model`.
110
- * @param vModel The parent `v-model`, which this function extracts a single property from.
111
- * @param key The key of the relevant property.
112
- * @returns An object of type {@link VModel}.
113
- * @example
114
- * ```tsx
115
- * type Customer = { firstName: string, lastName: String }
116
- *
117
- * // inside setup function,
118
- * // This vModel would normally be inside the props of a component e.g., `CustomerEditor`.
119
- * const parentVModel = {
120
- * value: { firstName: 'Hansi', lastName: 'Halunk' },
121
- * onUpdateValue: (newValue: Customer) => {
122
- * // update something
123
- * }
124
- * }
125
- *
126
- * // This input needs a v-model for the `firstName` property.
127
- * return () => (
128
- * <NInput
129
- * name="First Name"
130
- * {...vModelForVModelProperty(parentVModel, 'firstName'))}
131
- * />
132
- * )
133
- * ```
134
- */
135
- export function vModelForVModelProperty(vModel, key) {
136
- return {
137
- value: vModel.value[key],
138
- onUpdateValue: newValue => {
139
- vModel.onUpdateValue?.({
140
- ...vModel.value,
141
- [key]: newValue
142
- });
143
- }
144
- };
145
- }
146
- /**
147
- * This function does the same thing as {@link vModelForVModelProperty},
148
- * but can be provided with a mapper function for the modified property.
149
- * @see {@link vModelForVModelProperty}
150
- * @example
151
- * ```tsx
152
- * type Customer = { firstName: string, lastName: String, type: 'admin' | 'user' }
153
- *
154
- * // inside setup function,
155
- * // This vModel would normally be inside the props of a component e.g., `CustomerEditor`.
156
- * const parentVModel = {
157
- * value: { firstName: 'Hansi', lastName: 'Halunk', type: 'user' },
158
- * onUpdateValue: (newValue: Customer) => {
159
- * // update something
160
- * }
161
- * }
162
- *
163
- * // This input needs a v-model for the `type` property.
164
- * // Unfortunately the NSelect input expects an `onUpdateValue` with a
165
- * // parameter of type `string`, not type `admin | user`.
166
- * // So we have to provide a mapper function from `string` to `admin | user`.
167
- * // In this example, we just cast the value.
168
- * return () => (
169
- * <NSelect
170
- * name="Type"
171
- * {...vModelForVModelPropertyMapType(parentVModel, 'type', newVal => newVal as 'admin' | 'user'))}
172
- * />
173
- * )
174
- * ```
175
- */
176
- export function vModelForVModelPropertyMapType(vModel, key, mapType) {
177
- return {
178
- value: vModel.value[key],
179
- onUpdateValue: newValue => {
180
- vModel.onUpdateValue?.({
181
- ...vModel.value,
182
- [key]: mapType(newValue)
183
- });
184
- }
185
- };
186
- }
187
- /**
188
- * Creates an array of `v-models`, one for every element of an array. All changes in
189
- * a `v-model` of any element, will call the `onUpdate` function, with the updated array.
190
- * @param array The array to create `v-models` for.
191
- * @param onUpdate The updater function, which is called whenever an element has changed.
192
- * @returns An object of type {@link VModelForArray}.
193
- * @example
194
- * ```tsx
195
- * // inside setup function
196
- * const todos = ref([
197
- * 'Create v-model helper functions.',
198
- * 'Document them!'
199
- * ])
200
- *
201
- * // For every todo, there should be an input to modifiy it.
202
- * return () => (
203
- * <div>
204
- * {vModelsForArray(todos.value, newValue => (todos.value = newValue)).map(todoVModel => (
205
- * <NInput name="Todo" {...todoVModel} />
206
- * ))}
207
- * </div>
208
- * )
209
- * ```
210
- */
211
- export function vModelsForArray(array, onUpdate) {
212
- return array.map((entry, index) => ({
213
- value: entry,
214
- onUpdateValue: entry => {
215
- const newArray = [...array];
216
- newArray[index] = entry;
217
- onUpdate?.(newArray);
218
- },
219
- remove: () => {
220
- const newArray = [...array.slice(0, index), ...array.slice(index + 1)];
221
- onUpdate?.(newArray);
222
- }
223
- }));
224
- }
@@ -1,90 +0,0 @@
1
- export type ValidationResultValid = {
2
- isValid: true;
3
- errorMessage?: undefined;
4
- };
5
- export type ValidationResultInvalid = {
6
- isValid: false;
7
- errorMessage: string;
8
- };
9
- export type InputValue = string | null | undefined;
10
- export type ValidationResult = ValidationResultValid | ValidationResultInvalid;
11
- /**
12
- * A `ValidationRule` checks an input for a criteria and returns either
13
- * a {@link ValidationResultValid} or a {@link ValidationResultInvalid}.
14
- * A falsy input-value should always return a valid result to not interfere
15
- * with the {@link required} rule.
16
- */
17
- export type ValidationRule = (input: InputValue) => ValidationResult;
18
- /**
19
- * Creates a valid result.
20
- */
21
- export declare function validResult(): ValidationResultValid;
22
- /**
23
- * Creates an invalid result with the provided error message.
24
- */
25
- export declare function invalidResult(errorMessage: string): ValidationResultInvalid;
26
- /**
27
- * Validates a given input with the specified rules.
28
- * The rules are evaluated in the order they're in the array.
29
- * The {@link ValidationResult} will either contain the errorMessage of the failed rule
30
- * or a valid result if all rules passed.
31
- * @param input the input to validate.
32
- * @param rules the rules which should be vaildated, in the order of priority.
33
- * @returns an object containing the result of the validation.
34
- */
35
- export declare function validate(input: InputValue, rules: ValidationRule[]): ValidationResult;
36
- /**
37
- * This rule expects the trimmed input-value to be truthy.
38
- */
39
- export declare const required: ValidationRule;
40
- /**
41
- * This rule expects the input to be an integer.
42
- */
43
- export declare const integer: ValidationRule;
44
- /**
45
- * This rule expects the input to be in the specified length range. An empty input
46
- * will always be allowed by this rule to not interefere with the {@link required} rule.
47
- * @param min The minimum length of the string.
48
- * @param max The maximum length of the string.
49
- */
50
- export declare function length(min: number | undefined, max: number | undefined): ValidationRule;
51
- /**
52
- * This rule expects the input to be a number in the specified range.
53
- * @param min the lower bound, if `undefined` there is no lower bound.
54
- * @param max the upper bound, if `undefined` there is no upper bound.
55
- */
56
- export declare function numberRange(min: number | undefined, max: number | undefined): ValidationRule;
57
- export declare const VALIDATION_FORMAT_EMAIL: RegExp;
58
- /**
59
- * This rule expects the input-value to be a valid email adress matching {@link VALIDATION_FORMAT_EMAIL}.
60
- */
61
- export declare const email: ValidationRule;
62
- export declare const VALIDATION_FORMAT_PASSWORD: RegExp;
63
- /**
64
- * This rule expects the input-value to be a password matching {@link VALIDATION_FORMAT_PASSWORD}.
65
- */
66
- export declare const password: ValidationRule;
67
- /**
68
- * This rule expects the input-value to match another (input-) value.
69
- * In difference to most other rules, this rule does not always return a valid result,
70
- * when the input is falsy. The input is always required to match the other value.
71
- * @param other the other value to match
72
- */
73
- export declare function matches(other: string | null | undefined): ValidationRule;
74
- /**
75
- * This rule expects the input-value to match one option in an array.
76
- * @param options the options which the input can match
77
- */
78
- export declare function option(options: string[]): ValidationRule;
79
- /**
80
- * This rule expects the input-value to match the regex pattern
81
- * @param pattern the pattern the input should match.
82
- */
83
- export declare function regex(pattern: RegExp): ValidationRule;
84
- /**
85
- * This rule can be used if the validation logic happens somewhere else.
86
- * When `isValid = true` is passed, the function will return a valid result,
87
- * otherwise it will return the invalid result with the passed `errorKey`.
88
- * Like always, a falsy input is always valid to not interefere with the {@link required} rule.
89
- */
90
- export declare function external(isValid: boolean, errorMessage: string): ValidationRule;
@@ -1,147 +0,0 @@
1
- import { trsl } from '../i18n';
2
- /**
3
- * Creates a valid result.
4
- */
5
- export function validResult() {
6
- return {
7
- isValid: true
8
- };
9
- }
10
- /**
11
- * Creates an invalid result with the provided error message.
12
- */
13
- export function invalidResult(errorMessage) {
14
- return {
15
- isValid: false,
16
- errorMessage
17
- };
18
- }
19
- const TRANSLATION_KEY_BASE = 'vue-collection.validation.rules';
20
- function invalidResultInternal(key, params) {
21
- return invalidResult(trsl(`${TRANSLATION_KEY_BASE}.${key}`, params));
22
- }
23
- /**
24
- * Validates a given input with the specified rules.
25
- * The rules are evaluated in the order they're in the array.
26
- * The {@link ValidationResult} will either contain the errorMessage of the failed rule
27
- * or a valid result if all rules passed.
28
- * @param input the input to validate.
29
- * @param rules the rules which should be vaildated, in the order of priority.
30
- * @returns an object containing the result of the validation.
31
- */
32
- export function validate(input, rules) {
33
- for (const rule of rules) {
34
- const validationResult = rule(input);
35
- if (!validationResult.isValid) return validationResult;
36
- }
37
- return validResult();
38
- }
39
- /*
40
- * ---------- Validation Rules ----------
41
- */
42
- /**
43
- * This rule expects the trimmed input-value to be truthy.
44
- */
45
- export const required = input => {
46
- const trimmed = input?.trim();
47
- if (trimmed) return validResult();else return invalidResultInternal('required');
48
- };
49
- /**
50
- * This rule expects the input to be an integer.
51
- */
52
- export const integer = input => {
53
- if (!input || Number.isInteger(+input)) return validResult();else return invalidResultInternal('integer');
54
- };
55
- /**
56
- * This rule expects the input to be in the specified length range. An empty input
57
- * will always be allowed by this rule to not interefere with the {@link required} rule.
58
- * @param min The minimum length of the string.
59
- * @param max The maximum length of the string.
60
- */
61
- export function length(min, max) {
62
- return input => {
63
- if (!input) return validResult();
64
- if (min !== undefined && max !== undefined && !(min <= input.length && input.length <= max)) return invalidResultInternal('length.min-max', {
65
- min,
66
- max
67
- });else if (min !== undefined && !(min <= input.length)) return invalidResultInternal('length.min', {
68
- min
69
- });else if (max !== undefined && !(input.length <= max)) return invalidResultInternal('length.max', {
70
- max
71
- });
72
- return validResult();
73
- };
74
- }
75
- /**
76
- * This rule expects the input to be a number in the specified range.
77
- * @param min the lower bound, if `undefined` there is no lower bound.
78
- * @param max the upper bound, if `undefined` there is no upper bound.
79
- */
80
- export function numberRange(min, max) {
81
- return input => {
82
- if (!input) return validResult();
83
- const parsed = Number.parseFloat(input);
84
- if (Number.isNaN(parsed)) return invalidResultInternal('number-range.nan');
85
- if (min !== undefined && max !== undefined && !(min <= parsed && parsed <= max)) return invalidResultInternal('number-range.min-max', {
86
- min,
87
- max
88
- });else if (min !== undefined && !(min <= parsed)) return invalidResultInternal('number-range.min', {
89
- min
90
- });else if (max !== undefined && !(parsed <= max)) return invalidResultInternal('number-range.max', {
91
- max
92
- });
93
- return validResult();
94
- };
95
- }
96
- export const VALIDATION_FORMAT_EMAIL = /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/;
97
- /**
98
- * This rule expects the input-value to be a valid email adress matching {@link VALIDATION_FORMAT_EMAIL}.
99
- */
100
- export const email = input => {
101
- if (!input || VALIDATION_FORMAT_EMAIL.test(input)) return validResult();else return invalidResultInternal('email');
102
- };
103
- export const VALIDATION_FORMAT_PASSWORD = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$^+\-=!*()@%&?]).{8,}$/;
104
- /**
105
- * This rule expects the input-value to be a password matching {@link VALIDATION_FORMAT_PASSWORD}.
106
- */
107
- export const password = input => {
108
- if (!input || VALIDATION_FORMAT_PASSWORD.test(input)) return validResult();else if (input.length < 8) return invalidResultInternal('password.to-short');else if (!/[a-z]+/.test(input)) return invalidResultInternal('password.no-lowercase');else if (!/[A-Z]+/.test(input)) return invalidResultInternal('password.no-uppercase');else if (!/\d+/.test(input)) return invalidResultInternal('password.no-digits');else if (!/[#$^+\-=!*()@%&?]+/.test(input)) return invalidResultInternal('password.no-special-chars');else return invalidResultInternal('password.unknown');
109
- };
110
- /**
111
- * This rule expects the input-value to match another (input-) value.
112
- * In difference to most other rules, this rule does not always return a valid result,
113
- * when the input is falsy. The input is always required to match the other value.
114
- * @param other the other value to match
115
- */
116
- export function matches(other) {
117
- return input => {
118
- if (input === other) return validResult();else return invalidResultInternal('matches');
119
- };
120
- }
121
- /**
122
- * This rule expects the input-value to match one option in an array.
123
- * @param options the options which the input can match
124
- */
125
- export function option(options) {
126
- return input => {
127
- if (!input || options.includes(input || '')) return validResult();else return invalidResultInternal('option');
128
- };
129
- }
130
- /**
131
- * This rule expects the input-value to match the regex pattern
132
- * @param pattern the pattern the input should match.
133
- */
134
- export function regex(pattern) {
135
- return input => {
136
- if (!input || pattern.test(input || '')) return validResult();else return invalidResultInternal('regex');
137
- };
138
- }
139
- /**
140
- * This rule can be used if the validation logic happens somewhere else.
141
- * When `isValid = true` is passed, the function will return a valid result,
142
- * otherwise it will return the invalid result with the passed `errorKey`.
143
- * Like always, a falsy input is always valid to not interefere with the {@link required} rule.
144
- */
145
- export function external(isValid, errorMessage) {
146
- return input => !input || isValid ? validResult() : invalidResult(errorMessage);
147
- }
package/utils/vue.d.ts DELETED
@@ -1,13 +0,0 @@
1
- import { type Ref, type ComputedRef } from 'vue';
2
- /**
3
- * Creates a watcher on the updater function, which sets the value of the ref on every change.
4
- * @param ref the ref to update
5
- * @param updater the updater funtion which provides the updates
6
- */
7
- export declare function updateWith<T>(ref: Ref<T>, updater: () => T): void;
8
- /**
9
- * Conveience function to create a watcher for a ref
10
- * @param ref the ref to watch
11
- * @param onChange the function, which is executed on change of a value
12
- */
13
- export declare function watchRef<T>(ref: Ref<T> | ComputedRef<T>, onChange: (newValue: T) => void): void;
package/utils/vue.js DELETED
@@ -1,21 +0,0 @@
1
- import { watch } from 'vue';
2
- /**
3
- * Creates a watcher on the updater function, which sets the value of the ref on every change.
4
- * @param ref the ref to update
5
- * @param updater the updater funtion which provides the updates
6
- */
7
- export function updateWith(ref, updater) {
8
- watch(updater, newValue => {
9
- ref.value = newValue;
10
- }, {
11
- immediate: true
12
- });
13
- }
14
- /**
15
- * Conveience function to create a watcher for a ref
16
- * @param ref the ref to watch
17
- * @param onChange the function, which is executed on change of a value
18
- */
19
- export function watchRef(ref, onChange) {
20
- watch(() => ref.value, onChange);
21
- }