@ngrdt/forms 0.0.32 → 0.0.34
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/fesm2022/ngrdt-forms.mjs +248 -4
- package/fesm2022/ngrdt-forms.mjs.map +1 -1
- package/index.d.ts +31 -2
- package/package.json +3 -3
package/fesm2022/ngrdt-forms.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
2
|
import { inject, Injector, signal, input, model, linkedSignal, booleanAttribute, computed, effect, untracked, Directive, numberAttribute, InjectionToken, ElementRef } from '@angular/core';
|
|
3
3
|
import { takeUntilDestroyed, toSignal, toObservable } from '@angular/core/rxjs-interop';
|
|
4
|
-
import { FormControlName, FormControlDirective, NgModel, NgControl, Validators, FormControl } from '@angular/forms';
|
|
4
|
+
import { FormControlName, FormControlDirective, NgModel, NgControl, Validators, FormControl, FormGroup } from '@angular/forms';
|
|
5
5
|
import { RdtInteractiveElementComponent } from '@ngrdt/core';
|
|
6
|
-
import { startWith, take, map, forkJoin, of, from, switchMap, debounceTime, distinctUntilChanged } from 'rxjs';
|
|
7
|
-
import { RdtFileUtils, RdtObjectUtils } from '@ngrdt/utils';
|
|
6
|
+
import { startWith, take, map, forkJoin, of, from, switchMap, debounceTime, distinctUntilChanged, merge, takeUntil } from 'rxjs';
|
|
7
|
+
import { RdtFileUtils, RdtObjectUtils, RdtDateUtils } from '@ngrdt/utils';
|
|
8
8
|
import { signalStore, withState, withHooks, withComputed, withMethods, patchState } from '@ngrx/signals';
|
|
9
9
|
|
|
10
10
|
function getFormControl(ngControl) {
|
|
@@ -1130,9 +1130,253 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.0", ngImpor
|
|
|
1130
1130
|
type: Directive
|
|
1131
1131
|
}] });
|
|
1132
1132
|
|
|
1133
|
+
class RdtDateValidators {
|
|
1134
|
+
static notInFuture = (control) => {
|
|
1135
|
+
let value = null;
|
|
1136
|
+
if (control.value == null) {
|
|
1137
|
+
value = null;
|
|
1138
|
+
}
|
|
1139
|
+
else if (typeof control.value === 'string') {
|
|
1140
|
+
value = Date.parse(control.value);
|
|
1141
|
+
}
|
|
1142
|
+
else if (typeof control.value === 'number') {
|
|
1143
|
+
value = control.value;
|
|
1144
|
+
}
|
|
1145
|
+
else if (control.value instanceof Date) {
|
|
1146
|
+
value = control.value.getTime();
|
|
1147
|
+
}
|
|
1148
|
+
if (value == null || Date.now() > value) {
|
|
1149
|
+
return null;
|
|
1150
|
+
}
|
|
1151
|
+
return {
|
|
1152
|
+
dateInFuture: true,
|
|
1153
|
+
};
|
|
1154
|
+
};
|
|
1155
|
+
static lastDayOfMonth = (control) => {
|
|
1156
|
+
if (control.value == null) {
|
|
1157
|
+
return null;
|
|
1158
|
+
}
|
|
1159
|
+
const date = new Date(control.value);
|
|
1160
|
+
const lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
|
|
1161
|
+
if (date.getDate() === lastDay.getDate()) {
|
|
1162
|
+
return null;
|
|
1163
|
+
}
|
|
1164
|
+
return {
|
|
1165
|
+
lastDayOfMonth: true,
|
|
1166
|
+
};
|
|
1167
|
+
};
|
|
1168
|
+
static greaterOrEqualToField(startDateField, error = 'Platnost do musí být později než platnost od.', unsubscribeSubj) {
|
|
1169
|
+
let subbed = false;
|
|
1170
|
+
return (endControl) => {
|
|
1171
|
+
const startControl = startDateField instanceof FormControl
|
|
1172
|
+
? startDateField
|
|
1173
|
+
: endControl.parent?.get(startDateField);
|
|
1174
|
+
if (!startControl) {
|
|
1175
|
+
return null;
|
|
1176
|
+
}
|
|
1177
|
+
if (!subbed) {
|
|
1178
|
+
subbed = true;
|
|
1179
|
+
let change$ = merge(startControl.statusChanges.pipe(distinctUntilChanged()), startControl.valueChanges.pipe(distinctUntilChanged()));
|
|
1180
|
+
if (unsubscribeSubj) {
|
|
1181
|
+
change$ = change$.pipe(takeUntil(unsubscribeSubj));
|
|
1182
|
+
}
|
|
1183
|
+
if (change$) {
|
|
1184
|
+
change$.subscribe(() => {
|
|
1185
|
+
endControl.updateValueAndValidity();
|
|
1186
|
+
});
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
const startDate = startControl?.value;
|
|
1190
|
+
const endDate = endControl.value;
|
|
1191
|
+
if (!startDate || !endDate) {
|
|
1192
|
+
return null;
|
|
1193
|
+
}
|
|
1194
|
+
if (Date.parse(startDate) > Date.parse(endDate)) {
|
|
1195
|
+
return { dateGreaterOrEqualToField: error };
|
|
1196
|
+
}
|
|
1197
|
+
return null;
|
|
1198
|
+
};
|
|
1199
|
+
}
|
|
1200
|
+
static lessOrEqualToField(endDateField, error = 'Platnost od musí být dříve než platnost do.', unsubscribeSubj) {
|
|
1201
|
+
let subbed = false;
|
|
1202
|
+
return (startControl) => {
|
|
1203
|
+
const endControl = endDateField instanceof FormControl
|
|
1204
|
+
? endDateField
|
|
1205
|
+
: startControl.parent?.get(endDateField);
|
|
1206
|
+
if (!endControl) {
|
|
1207
|
+
return null;
|
|
1208
|
+
}
|
|
1209
|
+
if (!subbed) {
|
|
1210
|
+
subbed = true;
|
|
1211
|
+
let change$ = merge(endControl.statusChanges.pipe(distinctUntilChanged()), endControl.valueChanges.pipe(distinctUntilChanged()));
|
|
1212
|
+
if (unsubscribeSubj) {
|
|
1213
|
+
change$ = change$.pipe(takeUntil(unsubscribeSubj));
|
|
1214
|
+
}
|
|
1215
|
+
change$.subscribe(() => {
|
|
1216
|
+
startControl.updateValueAndValidity();
|
|
1217
|
+
});
|
|
1218
|
+
}
|
|
1219
|
+
const startDate = startControl.value;
|
|
1220
|
+
const endDate = endControl?.value;
|
|
1221
|
+
if (!startDate || !endDate) {
|
|
1222
|
+
return null;
|
|
1223
|
+
}
|
|
1224
|
+
if (Date.parse(startDate) > Date.parse(endDate)) {
|
|
1225
|
+
return { dateLessOrEqualToField: error };
|
|
1226
|
+
}
|
|
1227
|
+
return null;
|
|
1228
|
+
};
|
|
1229
|
+
}
|
|
1230
|
+
static lessOrEqualToConstant(value) {
|
|
1231
|
+
return (control) => RdtDateValidators.getDateRangeErrors(control.value, null, value);
|
|
1232
|
+
}
|
|
1233
|
+
static greaterOrEqualToConstant(value) {
|
|
1234
|
+
return (control) => RdtDateValidators.getDateRangeErrors(control.value, value, null);
|
|
1235
|
+
}
|
|
1236
|
+
static getDateRangeErrors(value, minDate, maxDate) {
|
|
1237
|
+
if ((minDate == null && maxDate == null) || value == null) {
|
|
1238
|
+
return null;
|
|
1239
|
+
}
|
|
1240
|
+
const dateValue = Date.parse(value);
|
|
1241
|
+
let minDateValue = RdtDateUtils.parse(minDate);
|
|
1242
|
+
let maxDateValue = RdtDateUtils.parse(maxDate);
|
|
1243
|
+
if (minDateValue) {
|
|
1244
|
+
minDateValue = RdtDateUtils.startOfDay(new Date(minDateValue)).getTime();
|
|
1245
|
+
}
|
|
1246
|
+
if (maxDateValue) {
|
|
1247
|
+
maxDateValue = RdtDateUtils.endOfDay(new Date(maxDateValue)).getTime();
|
|
1248
|
+
}
|
|
1249
|
+
const errors = {};
|
|
1250
|
+
if (minDateValue !== null && dateValue < minDateValue) {
|
|
1251
|
+
errors['dateGreaterOrEqualToConstant'] = {
|
|
1252
|
+
min: RdtDateUtils.formatDate(minDate),
|
|
1253
|
+
actual: RdtDateUtils.formatDate(value),
|
|
1254
|
+
};
|
|
1255
|
+
}
|
|
1256
|
+
if (maxDateValue !== null && dateValue > maxDateValue) {
|
|
1257
|
+
errors['dateLessOrEqualToConstant'] = {
|
|
1258
|
+
max: RdtDateUtils.formatDate(maxDate),
|
|
1259
|
+
actual: RdtDateUtils.formatDate(value),
|
|
1260
|
+
};
|
|
1261
|
+
}
|
|
1262
|
+
if (Object.keys(errors).length === 0) {
|
|
1263
|
+
return null;
|
|
1264
|
+
}
|
|
1265
|
+
else {
|
|
1266
|
+
return errors;
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
|
|
1271
|
+
class RdtCommonValidators {
|
|
1272
|
+
static atLeastOneRequired = () => null;
|
|
1273
|
+
static atLeastOneRequiredParent = (control) => {
|
|
1274
|
+
if (!(control instanceof FormGroup)) {
|
|
1275
|
+
throw new Error('Apply this validator to FormGroup.');
|
|
1276
|
+
}
|
|
1277
|
+
let someControlsHaveIt = false;
|
|
1278
|
+
for (const key in control.controls) {
|
|
1279
|
+
const ctrl = control.controls[key];
|
|
1280
|
+
if (ctrl.hasValidator(RdtCommonValidators.atLeastOneRequired)) {
|
|
1281
|
+
if (ctrl.value !== '' && ctrl.value != null) {
|
|
1282
|
+
return null;
|
|
1283
|
+
}
|
|
1284
|
+
someControlsHaveIt = true;
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
if (someControlsHaveIt) {
|
|
1288
|
+
return {
|
|
1289
|
+
atLeastOneRequired: true,
|
|
1290
|
+
};
|
|
1291
|
+
}
|
|
1292
|
+
return null;
|
|
1293
|
+
};
|
|
1294
|
+
static cannotContainChars(chars) {
|
|
1295
|
+
return (control) => {
|
|
1296
|
+
if (!control.value) {
|
|
1297
|
+
return null;
|
|
1298
|
+
}
|
|
1299
|
+
const value = control.value;
|
|
1300
|
+
if (chars.some((char) => value.includes(char))) {
|
|
1301
|
+
return {
|
|
1302
|
+
cannotContainChars: chars,
|
|
1303
|
+
};
|
|
1304
|
+
}
|
|
1305
|
+
return null;
|
|
1306
|
+
};
|
|
1307
|
+
}
|
|
1308
|
+
static startsWithPrefix(text) {
|
|
1309
|
+
return (control) => {
|
|
1310
|
+
const isValid = control.value?.startsWith(text);
|
|
1311
|
+
return isValid
|
|
1312
|
+
? null
|
|
1313
|
+
: {
|
|
1314
|
+
mustHavePrefix: {
|
|
1315
|
+
text,
|
|
1316
|
+
},
|
|
1317
|
+
};
|
|
1318
|
+
};
|
|
1319
|
+
}
|
|
1320
|
+
static exactLength(length) {
|
|
1321
|
+
return (control) => {
|
|
1322
|
+
if (!control.value) {
|
|
1323
|
+
return null;
|
|
1324
|
+
}
|
|
1325
|
+
const value = control.value;
|
|
1326
|
+
if (value.length !== length) {
|
|
1327
|
+
return {
|
|
1328
|
+
exactLength: length,
|
|
1329
|
+
};
|
|
1330
|
+
}
|
|
1331
|
+
return null;
|
|
1332
|
+
};
|
|
1333
|
+
}
|
|
1334
|
+
static maxNumLength(length) {
|
|
1335
|
+
return (control) => {
|
|
1336
|
+
if (!control.value) {
|
|
1337
|
+
return null;
|
|
1338
|
+
}
|
|
1339
|
+
const maxlengthError = { requiredLength: length };
|
|
1340
|
+
const constrolValue = control.value + '';
|
|
1341
|
+
if (constrolValue.length > length) {
|
|
1342
|
+
return {
|
|
1343
|
+
maxlength: maxlengthError,
|
|
1344
|
+
};
|
|
1345
|
+
}
|
|
1346
|
+
return null;
|
|
1347
|
+
};
|
|
1348
|
+
}
|
|
1349
|
+
static mustBeLowercase = (control) => {
|
|
1350
|
+
if (!control.value) {
|
|
1351
|
+
return null;
|
|
1352
|
+
}
|
|
1353
|
+
const value = control.value;
|
|
1354
|
+
if (value !== value.toLowerCase()) {
|
|
1355
|
+
return {
|
|
1356
|
+
mustBeLowercase: true,
|
|
1357
|
+
};
|
|
1358
|
+
}
|
|
1359
|
+
return null;
|
|
1360
|
+
};
|
|
1361
|
+
static cannotContainAccents = (control) => {
|
|
1362
|
+
if (!control.value) {
|
|
1363
|
+
return null;
|
|
1364
|
+
}
|
|
1365
|
+
const value = control.value;
|
|
1366
|
+
if (/^[a-zA-Z0-9]+$/.test(value)) {
|
|
1367
|
+
return null;
|
|
1368
|
+
}
|
|
1369
|
+
else {
|
|
1370
|
+
return {
|
|
1371
|
+
cannotContainSpecialChars: true,
|
|
1372
|
+
};
|
|
1373
|
+
}
|
|
1374
|
+
};
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1133
1377
|
/**
|
|
1134
1378
|
* Generated bundle index. Do not edit.
|
|
1135
1379
|
*/
|
|
1136
1380
|
|
|
1137
|
-
export { NO_OP_FILE_READER, NoOpFileReader, RDT_DEFAULT_FILE_LABEL_FN, RDT_DEFAULT_FILE_READER, RDT_DEFAULT_MAX_FILE_SIZE, RdtBaseFormInputComponent, RdtBaseSelectCommonComponent, RdtCheckboxComponent, RdtDateComponent, RdtFileInputComponent, RdtFileReader, RdtFileReaderArrayBuffer, RdtFileReaderBase64, RdtMultiSelectComponent, RdtNumericInputComponent, RdtOfflineSelectDatasource, RdtSelectDatasource, RdtSelectOfflineDatasourceProviderDirective, RdtSelectOptionDirective, RdtSelectStore, RdtSingleSelectComponent, RdtTextInputComponent, VnshFileReaderText, czechFileLabelFn, rdtSelectInitialState };
|
|
1381
|
+
export { NO_OP_FILE_READER, NoOpFileReader, RDT_DEFAULT_FILE_LABEL_FN, RDT_DEFAULT_FILE_READER, RDT_DEFAULT_MAX_FILE_SIZE, RdtBaseFormInputComponent, RdtBaseSelectCommonComponent, RdtCheckboxComponent, RdtCommonValidators, RdtDateComponent, RdtDateValidators, RdtFileInputComponent, RdtFileReader, RdtFileReaderArrayBuffer, RdtFileReaderBase64, RdtMultiSelectComponent, RdtNumericInputComponent, RdtOfflineSelectDatasource, RdtSelectDatasource, RdtSelectOfflineDatasourceProviderDirective, RdtSelectOptionDirective, RdtSelectStore, RdtSingleSelectComponent, RdtTextInputComponent, VnshFileReaderText, czechFileLabelFn, rdtSelectInitialState };
|
|
1138
1382
|
//# sourceMappingURL=ngrdt-forms.mjs.map
|