@naptics/vue-collection 0.0.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.
- package/README.md +27 -0
- package/lib/components/NAlert.d.ts +72 -0
- package/lib/components/NAlert.jsx +69 -0
- package/lib/components/NBadge.d.ts +174 -0
- package/lib/components/NBadge.jsx +58 -0
- package/lib/components/NBreadcrub.d.ts +161 -0
- package/lib/components/NBreadcrub.jsx +64 -0
- package/lib/components/NButton.d.ts +149 -0
- package/lib/components/NButton.jsx +58 -0
- package/lib/components/NCheckbox.d.ts +51 -0
- package/lib/components/NCheckbox.jsx +38 -0
- package/lib/components/NCheckboxLabel.d.ts +70 -0
- package/lib/components/NCheckboxLabel.jsx +42 -0
- package/lib/components/NCrudModal.d.ts +301 -0
- package/lib/components/NCrudModal.jsx +89 -0
- package/lib/components/NDialog.d.ts +190 -0
- package/lib/components/NDialog.jsx +144 -0
- package/lib/components/NDropdown.d.ts +116 -0
- package/lib/components/NDropdown.jsx +92 -0
- package/lib/components/NDropzone.d.ts +162 -0
- package/lib/components/NDropzone.jsx +211 -0
- package/lib/components/NForm.d.ts +43 -0
- package/lib/components/NForm.jsx +26 -0
- package/lib/components/NFormModal.d.ts +190 -0
- package/lib/components/NFormModal.jsx +48 -0
- package/lib/components/NIconButton.d.ts +202 -0
- package/lib/components/NIconButton.jsx +71 -0
- package/lib/components/NIconCircle.d.ts +135 -0
- package/lib/components/NIconCircle.jsx +67 -0
- package/lib/components/NInput.css +11 -0
- package/lib/components/NInput.d.ts +232 -0
- package/lib/components/NInput.jsx +97 -0
- package/lib/components/NInputPhone.d.ts +150 -0
- package/lib/components/NInputPhone.jsx +31 -0
- package/lib/components/NInputSelect.d.ts +271 -0
- package/lib/components/NInputSelect.jsx +89 -0
- package/lib/components/NInputSuggestion.d.ts +212 -0
- package/lib/components/NInputSuggestion.jsx +48 -0
- package/lib/components/NLink.d.ts +111 -0
- package/lib/components/NLink.jsx +58 -0
- package/lib/components/NList.d.ts +43 -0
- package/lib/components/NList.jsx +24 -0
- package/lib/components/NLoadingIndicator.css +46 -0
- package/lib/components/NLoadingIndicator.d.ts +77 -0
- package/lib/components/NLoadingIndicator.jsx +42 -0
- package/lib/components/NModal.d.ts +333 -0
- package/lib/components/NModal.jsx +170 -0
- package/lib/components/NPagination.css +15 -0
- package/lib/components/NPagination.d.ts +99 -0
- package/lib/components/NPagination.jsx +104 -0
- package/lib/components/NSearchbar.d.ts +94 -0
- package/lib/components/NSearchbar.jsx +58 -0
- package/lib/components/NSearchbarList.d.ts +90 -0
- package/lib/components/NSearchbarList.jsx +20 -0
- package/lib/components/NSelect.d.ts +159 -0
- package/lib/components/NSelect.jsx +81 -0
- package/lib/components/NSuggestionList.d.ts +275 -0
- package/lib/components/NSuggestionList.jsx +157 -0
- package/lib/components/NTable.css +3 -0
- package/lib/components/NTable.d.ts +187 -0
- package/lib/components/NTable.jsx +146 -0
- package/lib/components/NTableAction.d.ts +74 -0
- package/lib/components/NTableAction.jsx +35 -0
- package/lib/components/NTextArea.d.ts +252 -0
- package/lib/components/NTextArea.jsx +108 -0
- package/lib/components/NTooltip.css +37 -0
- package/lib/components/NTooltip.d.ts +186 -0
- package/lib/components/NTooltip.jsx +161 -0
- package/lib/components/NValInput.d.ts +297 -0
- package/lib/components/NValInput.jsx +101 -0
- package/lib/components/ValidatedForm.d.ts +34 -0
- package/lib/components/ValidatedForm.js +25 -0
- package/lib/i18n/de/template.json +10 -0
- package/lib/i18n/de/vue-collection.json +58 -0
- package/lib/i18n/de.d.ts +61 -0
- package/lib/i18n/de.js +5 -0
- package/lib/i18n/en/template.json +10 -0
- package/lib/i18n/en/vue-collection.json +58 -0
- package/lib/i18n/en.d.ts +61 -0
- package/lib/i18n/en.js +5 -0
- package/lib/i18n/index.d.ts +17 -0
- package/lib/i18n/index.js +34 -0
- package/lib/index.d.ts +64 -0
- package/lib/index.js +65 -0
- package/lib/utils/breakpoints.d.ts +15 -0
- package/lib/utils/breakpoints.js +37 -0
- package/lib/utils/component.d.ts +50 -0
- package/lib/utils/component.js +51 -0
- package/lib/utils/deferred.d.ts +13 -0
- package/lib/utils/deferred.js +17 -0
- package/lib/utils/identifiable.d.ts +57 -0
- package/lib/utils/identifiable.js +78 -0
- package/lib/utils/stringMaxLength.d.ts +14 -0
- package/lib/utils/stringMaxLength.js +28 -0
- package/lib/utils/tailwind.d.ts +2 -0
- package/lib/utils/tailwind.js +1 -0
- package/lib/utils/utils.d.ts +27 -0
- package/lib/utils/utils.js +26 -0
- package/lib/utils/vModel.d.ts +182 -0
- package/lib/utils/vModel.js +215 -0
- package/lib/utils/validation.d.ts +84 -0
- package/lib/utils/validation.js +163 -0
- package/lib/utils/vue.d.ts +13 -0
- package/lib/utils/vue.js +19 -0
- package/package.json +56 -0
|
@@ -0,0 +1,215 @@
|
|
|
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?.({ ...object, [key]: newValue });
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* This creates a `v-model` which operates on one property of a parent `v-model`. It takes the value of
|
|
105
|
+
* the property as the value of the `v-model` and calls the function `onUpdateValue` of the parent `v-model`
|
|
106
|
+
* whenever the property changes. This function can be seen as a kind of mapper for a `v-model`.
|
|
107
|
+
* @param vModel The parent `v-model`, which this function extracts a single property from.
|
|
108
|
+
* @param key The key of the relevant property.
|
|
109
|
+
* @returns An object of type {@link VModel}.
|
|
110
|
+
* @example
|
|
111
|
+
* ```tsx
|
|
112
|
+
* type Customer = { firstName: string, lastName: String }
|
|
113
|
+
*
|
|
114
|
+
* // inside setup function,
|
|
115
|
+
* // This vModel would normally be inside the props of a component e.g., `CustomerEditor`.
|
|
116
|
+
* const parentVModel = {
|
|
117
|
+
* value: { firstName: 'Hansi', lastName: 'Halunk' },
|
|
118
|
+
* onUpdateValue: (newValue: Customer) => {
|
|
119
|
+
* // update something
|
|
120
|
+
* }
|
|
121
|
+
* }
|
|
122
|
+
*
|
|
123
|
+
* // This input needs a v-model for the `firstName` property.
|
|
124
|
+
* return () => (
|
|
125
|
+
* <NInput
|
|
126
|
+
* name="First Name"
|
|
127
|
+
* {...vModelForVModelProperty(parentVModel, 'firstName'))}
|
|
128
|
+
* />
|
|
129
|
+
* )
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
export function vModelForVModelProperty(vModel, key) {
|
|
133
|
+
return {
|
|
134
|
+
value: vModel.value[key],
|
|
135
|
+
onUpdateValue: (newValue) => {
|
|
136
|
+
vModel.onUpdateValue?.({ ...vModel.value, [key]: newValue });
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* This function does the same thing as {@link vModelForVModelProperty},
|
|
142
|
+
* but can be provided with a mapper function for the modified property.
|
|
143
|
+
* @see {@link vModelForVModelProperty}
|
|
144
|
+
* @example
|
|
145
|
+
* ```tsx
|
|
146
|
+
* type Customer = { firstName: string, lastName: String, type: 'admin' | 'user' }
|
|
147
|
+
*
|
|
148
|
+
* // inside setup function,
|
|
149
|
+
* // This vModel would normally be inside the props of a component e.g., `CustomerEditor`.
|
|
150
|
+
* const parentVModel = {
|
|
151
|
+
* value: { firstName: 'Hansi', lastName: 'Halunk', type: 'user' },
|
|
152
|
+
* onUpdateValue: (newValue: Customer) => {
|
|
153
|
+
* // update something
|
|
154
|
+
* }
|
|
155
|
+
* }
|
|
156
|
+
*
|
|
157
|
+
* // This input needs a v-model for the `type` property.
|
|
158
|
+
* // Unfortunately the NSelect input expects an `onUpdateValue` with a
|
|
159
|
+
* // parameter of type `string`, not type `admin | user`.
|
|
160
|
+
* // So we have to provide a mapper function from `string` to `admin | user`.
|
|
161
|
+
* // In this example, we just cast the value.
|
|
162
|
+
* return () => (
|
|
163
|
+
* <NSelect
|
|
164
|
+
* name="Type"
|
|
165
|
+
* {...vModelForVModelPropertyMapType(parentVModel, 'type', newVal => newVal as 'admin' | 'user'))}
|
|
166
|
+
* />
|
|
167
|
+
* )
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
export function vModelForVModelPropertyMapType(vModel, key, mapType) {
|
|
171
|
+
return {
|
|
172
|
+
value: vModel.value[key],
|
|
173
|
+
onUpdateValue: (newValue) => {
|
|
174
|
+
vModel.onUpdateValue?.({ ...vModel.value, [key]: mapType(newValue) });
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Creates an array of `v-models`, one for every element of an array. All changes in
|
|
180
|
+
* a `v-model` of any element, will call the `onUpdate` function, with the updated array.
|
|
181
|
+
* @param array The array to create `v-models` for.
|
|
182
|
+
* @param onUpdate The updater function, which is called whenever an element has changed.
|
|
183
|
+
* @returns An object of type {@link VModelForArray}.
|
|
184
|
+
* @example
|
|
185
|
+
* ```tsx
|
|
186
|
+
* // inside setup function
|
|
187
|
+
* const todos = ref([
|
|
188
|
+
* 'Create v-model helper functions.',
|
|
189
|
+
* 'Document them!'
|
|
190
|
+
* ])
|
|
191
|
+
*
|
|
192
|
+
* // For every todo, there should be an input to modifiy it.
|
|
193
|
+
* return () => (
|
|
194
|
+
* <div>
|
|
195
|
+
* {vModelsForArray(todos.value, newValue => (todos.value = newValue)).map(todoVModel => (
|
|
196
|
+
* <NInput name="Todo" {...todoVModel} />
|
|
197
|
+
* ))}
|
|
198
|
+
* </div>
|
|
199
|
+
* )
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
export function vModelsForArray(array, onUpdate) {
|
|
203
|
+
return array.map((entry, index) => ({
|
|
204
|
+
value: entry,
|
|
205
|
+
onUpdateValue: (entry) => {
|
|
206
|
+
const newArray = [...array];
|
|
207
|
+
newArray[index] = entry;
|
|
208
|
+
onUpdate?.(newArray);
|
|
209
|
+
},
|
|
210
|
+
remove: () => {
|
|
211
|
+
const newArray = [...array.slice(0, index), ...array.slice(index + 1)];
|
|
212
|
+
onUpdate?.(newArray);
|
|
213
|
+
},
|
|
214
|
+
}));
|
|
215
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
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
|
+
export declare function validResult(): ValidationResultValid;
|
|
19
|
+
export declare function invalidResult(ruleKey: string, params?: Record<string, unknown>): ValidationResultInvalid;
|
|
20
|
+
/**
|
|
21
|
+
* Validates a given input with the specified rules.
|
|
22
|
+
* The rules are evaluated in the order they're in the array.
|
|
23
|
+
* The {@link ValidationResult} will either contain the errorMessage of the failed rule
|
|
24
|
+
* or a valid result if all rules passed.
|
|
25
|
+
* @param input the input to validate.
|
|
26
|
+
* @param rules the rules which should be vaildated, in the order of priority.
|
|
27
|
+
* @returns an object containing the result of the validation.
|
|
28
|
+
*/
|
|
29
|
+
export declare function validate(input: InputValue, rules: ValidationRule[]): ValidationResult;
|
|
30
|
+
/**
|
|
31
|
+
* This rule expects the trimmed input-value to be truthy.
|
|
32
|
+
*/
|
|
33
|
+
export declare const required: ValidationRule;
|
|
34
|
+
/**
|
|
35
|
+
* This rule expects the input to be an integer.
|
|
36
|
+
*/
|
|
37
|
+
export declare const integer: ValidationRule;
|
|
38
|
+
/**
|
|
39
|
+
* This rule expects the input to be in the specified length range. An empty input
|
|
40
|
+
* will always be allowed by this rule to not interefere with the {@link required} rule.
|
|
41
|
+
* @param min The minimum length of the string.
|
|
42
|
+
* @param max The maximum length of the string.
|
|
43
|
+
*/
|
|
44
|
+
export declare function length(min: number | undefined, max: number | undefined): ValidationRule;
|
|
45
|
+
/**
|
|
46
|
+
* This rule expects the input to be a number in the specified range.
|
|
47
|
+
* @param min the lower bound, if `undefined` there is no lower bound.
|
|
48
|
+
* @param max the upper bound, if `undefined` there is no upper bound.
|
|
49
|
+
*/
|
|
50
|
+
export declare function numberRange(min: number | undefined, max: number | undefined): ValidationRule;
|
|
51
|
+
export declare const VALIDATION_FORMAT_EMAIL: RegExp;
|
|
52
|
+
/**
|
|
53
|
+
* This rule expects the input-value to be a valid email adress matching {@link VALIDATION_FORMAT_EMAIL}.
|
|
54
|
+
*/
|
|
55
|
+
export declare const email: ValidationRule;
|
|
56
|
+
export declare const VALIDATION_FORMAT_PASSWORD: RegExp;
|
|
57
|
+
/**
|
|
58
|
+
* This rule expects the input-value to be a password matching {@link VALIDATION_FORMAT_PASSWORD}.
|
|
59
|
+
*/
|
|
60
|
+
export declare const password: ValidationRule;
|
|
61
|
+
/**
|
|
62
|
+
* This rule expects the input-value to match another (input-) value.
|
|
63
|
+
* In difference to most other rules, this rule does not always return a valid result,
|
|
64
|
+
* when the input is falsy. The input is always required to match the other value.
|
|
65
|
+
* @param other the other value to match
|
|
66
|
+
*/
|
|
67
|
+
export declare function matches(other: string | null | undefined): ValidationRule;
|
|
68
|
+
/**
|
|
69
|
+
* This rule expects the input-value to match one option in an array.
|
|
70
|
+
* @param options the options which the input can match
|
|
71
|
+
*/
|
|
72
|
+
export declare function option(options: string[]): ValidationRule;
|
|
73
|
+
/**
|
|
74
|
+
* This rule expects the input-value to match the regex pattern
|
|
75
|
+
* @param pattern the pattern the input should match.
|
|
76
|
+
*/
|
|
77
|
+
export declare function regex(pattern: RegExp): ValidationRule;
|
|
78
|
+
/**
|
|
79
|
+
* This rule can be used if the validation logic happens somwhere else.
|
|
80
|
+
* When `isValid = true` is passed, the function will return a valid result,
|
|
81
|
+
* otherwise it will return the invalid result with the passed `errorKey`.
|
|
82
|
+
* Like always, a falsy input is always valid to not interefere with the {@link required} rule.
|
|
83
|
+
*/
|
|
84
|
+
export declare function external(isValid: boolean, errorKey: string): ValidationRule;
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { trsl } from '../i18n';
|
|
2
|
+
export function validResult() {
|
|
3
|
+
return { isValid: true };
|
|
4
|
+
}
|
|
5
|
+
export function invalidResult(ruleKey, params) {
|
|
6
|
+
return { isValid: false, errorMessage: trsl(`vue-collection.validation.rules.${ruleKey}`, params) };
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Validates a given input with the specified rules.
|
|
10
|
+
* The rules are evaluated in the order they're in the array.
|
|
11
|
+
* The {@link ValidationResult} will either contain the errorMessage of the failed rule
|
|
12
|
+
* or a valid result if all rules passed.
|
|
13
|
+
* @param input the input to validate.
|
|
14
|
+
* @param rules the rules which should be vaildated, in the order of priority.
|
|
15
|
+
* @returns an object containing the result of the validation.
|
|
16
|
+
*/
|
|
17
|
+
export function validate(input, rules) {
|
|
18
|
+
for (const rule of rules) {
|
|
19
|
+
const validationResult = rule(input);
|
|
20
|
+
if (!validationResult.isValid)
|
|
21
|
+
return validationResult;
|
|
22
|
+
}
|
|
23
|
+
return validResult();
|
|
24
|
+
}
|
|
25
|
+
/*
|
|
26
|
+
* ---------- Validation Rules ----------
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* This rule expects the trimmed input-value to be truthy.
|
|
30
|
+
*/
|
|
31
|
+
export const required = input => {
|
|
32
|
+
const trimmed = input?.trim();
|
|
33
|
+
if (trimmed)
|
|
34
|
+
return validResult();
|
|
35
|
+
else
|
|
36
|
+
return invalidResult('required');
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* This rule expects the input to be an integer.
|
|
40
|
+
*/
|
|
41
|
+
export const integer = input => {
|
|
42
|
+
if (!input || Number.isInteger(+input))
|
|
43
|
+
return validResult();
|
|
44
|
+
else
|
|
45
|
+
return invalidResult('integer');
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* This rule expects the input to be in the specified length range. An empty input
|
|
49
|
+
* will always be allowed by this rule to not interefere with the {@link required} rule.
|
|
50
|
+
* @param min The minimum length of the string.
|
|
51
|
+
* @param max The maximum length of the string.
|
|
52
|
+
*/
|
|
53
|
+
export function length(min, max) {
|
|
54
|
+
return input => {
|
|
55
|
+
if (!input)
|
|
56
|
+
return validResult();
|
|
57
|
+
if (min !== undefined && max !== undefined && !(min <= input.length && input.length <= max))
|
|
58
|
+
return invalidResult('length.min-max', { min, max });
|
|
59
|
+
else if (min !== undefined && !(min <= input.length))
|
|
60
|
+
return invalidResult('length.min', { min });
|
|
61
|
+
else if (max !== undefined && !(input.length <= max))
|
|
62
|
+
return invalidResult('length.max', { max });
|
|
63
|
+
return validResult();
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* This rule expects the input to be a number in the specified range.
|
|
68
|
+
* @param min the lower bound, if `undefined` there is no lower bound.
|
|
69
|
+
* @param max the upper bound, if `undefined` there is no upper bound.
|
|
70
|
+
*/
|
|
71
|
+
export function numberRange(min, max) {
|
|
72
|
+
return input => {
|
|
73
|
+
if (!input)
|
|
74
|
+
return validResult();
|
|
75
|
+
const parsed = Number.parseFloat(input);
|
|
76
|
+
if (Number.isNaN(parsed))
|
|
77
|
+
return invalidResult('number-range.nan');
|
|
78
|
+
if (min !== undefined && max !== undefined && !(min <= parsed && parsed <= max))
|
|
79
|
+
return invalidResult('number-range.min-max', { min, max });
|
|
80
|
+
else if (min !== undefined && !(min <= parsed))
|
|
81
|
+
return invalidResult('number-range.min', { min });
|
|
82
|
+
else if (max !== undefined && !(parsed <= max))
|
|
83
|
+
return invalidResult('number-range.max', { max });
|
|
84
|
+
return validResult();
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
export const VALIDATION_FORMAT_EMAIL = /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/;
|
|
88
|
+
/**
|
|
89
|
+
* This rule expects the input-value to be a valid email adress matching {@link VALIDATION_FORMAT_EMAIL}.
|
|
90
|
+
*/
|
|
91
|
+
export const email = input => {
|
|
92
|
+
if (!input || VALIDATION_FORMAT_EMAIL.test(input))
|
|
93
|
+
return validResult();
|
|
94
|
+
else
|
|
95
|
+
return invalidResult('email');
|
|
96
|
+
};
|
|
97
|
+
export const VALIDATION_FORMAT_PASSWORD = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$^+\-=!*()@%&?]).{8,}$/;
|
|
98
|
+
/**
|
|
99
|
+
* This rule expects the input-value to be a password matching {@link VALIDATION_FORMAT_PASSWORD}.
|
|
100
|
+
*/
|
|
101
|
+
export const password = input => {
|
|
102
|
+
if (!input || VALIDATION_FORMAT_PASSWORD.test(input))
|
|
103
|
+
return validResult();
|
|
104
|
+
else if (input.length < 8)
|
|
105
|
+
return invalidResult('password.to-short');
|
|
106
|
+
else if (!/[a-z]+/.test(input))
|
|
107
|
+
return invalidResult('password.no-lowercase');
|
|
108
|
+
else if (!/[A-Z]+/.test(input))
|
|
109
|
+
return invalidResult('password.no-uppercase');
|
|
110
|
+
else if (!/\d+/.test(input))
|
|
111
|
+
return invalidResult('password.no-digits');
|
|
112
|
+
else if (!/[#$^+\-=!*()@%&?]+/.test(input))
|
|
113
|
+
return invalidResult('password.no-special-chars');
|
|
114
|
+
else
|
|
115
|
+
return invalidResult('password.unknown');
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* This rule expects the input-value to match another (input-) value.
|
|
119
|
+
* In difference to most other rules, this rule does not always return a valid result,
|
|
120
|
+
* when the input is falsy. The input is always required to match the other value.
|
|
121
|
+
* @param other the other value to match
|
|
122
|
+
*/
|
|
123
|
+
export function matches(other) {
|
|
124
|
+
return input => {
|
|
125
|
+
if (input === other)
|
|
126
|
+
return validResult();
|
|
127
|
+
else
|
|
128
|
+
return invalidResult('matches');
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* This rule expects the input-value to match one option in an array.
|
|
133
|
+
* @param options the options which the input can match
|
|
134
|
+
*/
|
|
135
|
+
export function option(options) {
|
|
136
|
+
return input => {
|
|
137
|
+
if (!input || options.includes(input || ''))
|
|
138
|
+
return validResult();
|
|
139
|
+
else
|
|
140
|
+
return invalidResult('option');
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* This rule expects the input-value to match the regex pattern
|
|
145
|
+
* @param pattern the pattern the input should match.
|
|
146
|
+
*/
|
|
147
|
+
export function regex(pattern) {
|
|
148
|
+
return input => {
|
|
149
|
+
if (!input || pattern.test(input || ''))
|
|
150
|
+
return validResult();
|
|
151
|
+
else
|
|
152
|
+
return invalidResult('regex');
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* This rule can be used if the validation logic happens somwhere else.
|
|
157
|
+
* When `isValid = true` is passed, the function will return a valid result,
|
|
158
|
+
* otherwise it will return the invalid result with the passed `errorKey`.
|
|
159
|
+
* Like always, a falsy input is always valid to not interefere with the {@link required} rule.
|
|
160
|
+
*/
|
|
161
|
+
export function external(isValid, errorKey) {
|
|
162
|
+
return input => (!input || isValid ? validResult() : invalidResult(errorKey));
|
|
163
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
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/lib/utils/vue.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
}, { immediate: true });
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Conveience function to create a watcher for a ref
|
|
14
|
+
* @param ref the ref to watch
|
|
15
|
+
* @param onChange the function, which is executed on change of a value
|
|
16
|
+
*/
|
|
17
|
+
export function watchRef(ref, onChange) {
|
|
18
|
+
watch(() => ref.value, onChange);
|
|
19
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@naptics/vue-collection",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "./lib/index.js",
|
|
5
|
+
"files": [
|
|
6
|
+
"lib"
|
|
7
|
+
],
|
|
8
|
+
"scripts": {
|
|
9
|
+
"dev": "vite",
|
|
10
|
+
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
|
|
11
|
+
"type-check": "run-p type-check-lib type-check-demo",
|
|
12
|
+
"type-check-demo": "vue-tsc --noEmit -p tsconfig.demo.json --composite false",
|
|
13
|
+
"type-check-lib": "vue-tsc --noEmit -p tsconfig.lib.json --composite false",
|
|
14
|
+
"test": "vitest --environment jsdom",
|
|
15
|
+
"build-demo": "run-p type-check-demo build-only-demo",
|
|
16
|
+
"build-lib": "run-p type-check-lib build-only-lib",
|
|
17
|
+
"build-only-demo": "vite build",
|
|
18
|
+
"build-only-lib": "rm -rf ./lib && tsc --project tsconfig.lib.json && mv ./lib/src/lib/* ./lib && rm -rf ./lib/src && rm ./lib/tsconfig.lib.tsbuildinfo && cp ./src/lib/components/*.css ./lib/components"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@headlessui/vue": "^1.7.10",
|
|
22
|
+
"@popperjs/core": "^2.11.6",
|
|
23
|
+
"awesome-phonenumber": "^5.1.0"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@heroicons/vue": "^2.0.16",
|
|
27
|
+
"vue": "^3.2.36",
|
|
28
|
+
"vue-i18n": "^9.2.2"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@intlify/unplugin-vue-i18n": "^0.8.2",
|
|
32
|
+
"@rushstack/eslint-patch": "^1.2.0",
|
|
33
|
+
"@tailwindcss/forms": "^0.5.3",
|
|
34
|
+
"@types/jsdom": "^21.1.0",
|
|
35
|
+
"@types/node": "^18.14.0",
|
|
36
|
+
"@vitejs/plugin-vue": "^4.0.0",
|
|
37
|
+
"@vitejs/plugin-vue-jsx": "^3.0.0",
|
|
38
|
+
"@vue/eslint-config-prettier": "^7.1.0",
|
|
39
|
+
"@vue/eslint-config-typescript": "^11.0.2",
|
|
40
|
+
"@vue/test-utils": "^2.3.0",
|
|
41
|
+
"@vue/tsconfig": "^0.1.3",
|
|
42
|
+
"autoprefixer": "^10.4.13",
|
|
43
|
+
"eslint": "^8.34.0",
|
|
44
|
+
"eslint-plugin-vue": "^9.9.0",
|
|
45
|
+
"jsdom": "^21.1.0",
|
|
46
|
+
"npm-run-all": "^4.1.5",
|
|
47
|
+
"postcss": "^8.4.21",
|
|
48
|
+
"prettier": "^2.8.4",
|
|
49
|
+
"tailwindcss": "^3.2.7",
|
|
50
|
+
"typescript": "^4.9.5",
|
|
51
|
+
"vite": "^4.1.3",
|
|
52
|
+
"vitest": "^0.28.5",
|
|
53
|
+
"vue-router": "^4.1.6",
|
|
54
|
+
"vue-tsc": "^1.1.5"
|
|
55
|
+
}
|
|
56
|
+
}
|