@flowgram.ai/form 0.1.0 → 0.1.2

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/dist/index.d.mts CHANGED
@@ -445,8 +445,8 @@ interface FieldOptions<TValue, TFormValues = any> {
445
445
  }
446
446
  interface FieldRenderProps<TValue> {
447
447
  field: Field$1<TValue>;
448
- fieldState?: Readonly<FieldState>;
449
- formState?: Readonly<FormState>;
448
+ fieldState: Readonly<FieldState>;
449
+ formState: Readonly<FormState>;
450
450
  }
451
451
  interface FieldArrayOptions<TValue> {
452
452
  /**
@@ -468,8 +468,8 @@ interface FieldArrayOptions<TValue> {
468
468
  }
469
469
  interface FieldArrayRenderProps<TValue> {
470
470
  field: FieldArray$1<TValue>;
471
- fieldState?: Readonly<FieldState>;
472
- formState?: Readonly<FormState>;
471
+ fieldState: Readonly<FieldState>;
472
+ formState: Readonly<FormState>;
473
473
  }
474
474
  interface FieldState {
475
475
  /**
@@ -634,7 +634,20 @@ declare namespace Glob {
634
634
  * @private
635
635
  */
636
636
  function splitPattern(pattern: string): string[];
637
- function findMatchPaths(obj: any, pattern: string): string[];
637
+ /**
638
+ * Find all paths matched pattern in object. If withEmptyValue is true, it will include
639
+ * paths whoes value is undefined.
640
+ * @param obj
641
+ * @param pattern
642
+ * @param withEmptyValue
643
+ */
644
+ function findMatchPaths(obj: any, pattern: string, withEmptyValue?: boolean): string[];
645
+ /**
646
+ * Find all paths matched pattern in object, including paths whoes value is undefined.
647
+ * @param obj
648
+ * @param pattern
649
+ */
650
+ function findMatchPathsWithEmptyValue(obj: any, pattern: string): string[];
638
651
  }
639
652
 
640
653
  declare function toField<TValue>(model: FieldModel): Field$1<TValue>;
package/dist/index.d.ts CHANGED
@@ -445,8 +445,8 @@ interface FieldOptions<TValue, TFormValues = any> {
445
445
  }
446
446
  interface FieldRenderProps<TValue> {
447
447
  field: Field$1<TValue>;
448
- fieldState?: Readonly<FieldState>;
449
- formState?: Readonly<FormState>;
448
+ fieldState: Readonly<FieldState>;
449
+ formState: Readonly<FormState>;
450
450
  }
451
451
  interface FieldArrayOptions<TValue> {
452
452
  /**
@@ -468,8 +468,8 @@ interface FieldArrayOptions<TValue> {
468
468
  }
469
469
  interface FieldArrayRenderProps<TValue> {
470
470
  field: FieldArray$1<TValue>;
471
- fieldState?: Readonly<FieldState>;
472
- formState?: Readonly<FormState>;
471
+ fieldState: Readonly<FieldState>;
472
+ formState: Readonly<FormState>;
473
473
  }
474
474
  interface FieldState {
475
475
  /**
@@ -634,7 +634,20 @@ declare namespace Glob {
634
634
  * @private
635
635
  */
636
636
  function splitPattern(pattern: string): string[];
637
- function findMatchPaths(obj: any, pattern: string): string[];
637
+ /**
638
+ * Find all paths matched pattern in object. If withEmptyValue is true, it will include
639
+ * paths whoes value is undefined.
640
+ * @param obj
641
+ * @param pattern
642
+ * @param withEmptyValue
643
+ */
644
+ function findMatchPaths(obj: any, pattern: string, withEmptyValue?: boolean): string[];
645
+ /**
646
+ * Find all paths matched pattern in object, including paths whoes value is undefined.
647
+ * @param obj
648
+ * @param pattern
649
+ */
650
+ function findMatchPathsWithEmptyValue(obj: any, pattern: string): string[];
638
651
  }
639
652
 
640
653
  declare function toField<TValue>(model: FieldModel): Field$1<TValue>;
package/dist/index.js CHANGED
@@ -215,7 +215,7 @@ var Glob;
215
215
  return res;
216
216
  }
217
217
  Glob2.splitPattern = splitPattern;
218
- function findMatchPaths2(obj, pattern) {
218
+ function findMatchPaths(obj, pattern, withEmptyValue) {
219
219
  if (!obj || !pattern) {
220
220
  return [];
221
221
  }
@@ -233,20 +233,26 @@ var Glob;
233
233
  if (nextPaths.length === 0) {
234
234
  return concatPath(parentPath, key);
235
235
  }
236
- return findMatchPaths2(curValue[key], `${nextPaths.join(Glob2.DIVIDER)}`).map(
236
+ return findMatchPaths(curValue[key], `${nextPaths.join(Glob2.DIVIDER)}`, withEmptyValue).map(
237
237
  (p) => concatPath(parentPath, key, p)
238
238
  );
239
239
  })
240
240
  );
241
241
  }
242
- if (!(curKey in curValue)) return [];
242
+ if (!(curKey in curValue) && !withEmptyValue) {
243
+ return [];
244
+ }
243
245
  curValue = curValue[curKey];
244
246
  curPaths.push(curKey);
245
247
  curKey = nextPaths.shift();
246
248
  }
247
249
  return [pattern];
248
250
  }
249
- Glob2.findMatchPaths = findMatchPaths2;
251
+ Glob2.findMatchPaths = findMatchPaths;
252
+ function findMatchPathsWithEmptyValue(obj, pattern) {
253
+ return findMatchPaths(obj, pattern, true);
254
+ }
255
+ Glob2.findMatchPathsWithEmptyValue = findMatchPathsWithEmptyValue;
250
256
  })(Glob || (Glob = {}));
251
257
 
252
258
  // src/types/validate.ts
@@ -691,6 +697,14 @@ function feedbackToFieldErrorsOrWarnings(name, feedback) {
691
697
  [name]: feedback ? [feedback] : []
692
698
  };
693
699
  }
700
+ var hasError = (errors) => {
701
+ for (let fieldErrors in errors) {
702
+ if (fieldErrors.length) {
703
+ return true;
704
+ }
705
+ }
706
+ return false;
707
+ };
694
708
 
695
709
  // src/constants.ts
696
710
  var DEFAULT_FIELD_STATE = {
@@ -1160,7 +1174,6 @@ var FieldArrayModel = class extends FieldModel {
1160
1174
  };
1161
1175
 
1162
1176
  // src/core/form-model.ts
1163
- var findMatchPaths = Glob.findMatchPaths;
1164
1177
  var FormModel = class {
1165
1178
  constructor() {
1166
1179
  this._fieldMap = /* @__PURE__ */ new Map();
@@ -1331,7 +1344,7 @@ var FormModel = class {
1331
1344
  }
1332
1345
  const feedbacksArrPromises = Object.keys(this._options.validate).map(async (nameRule) => {
1333
1346
  const validate = this._options.validate[nameRule];
1334
- const paths = findMatchPaths(this.values, nameRule);
1347
+ const paths = Glob.findMatchPathsWithEmptyValue(this.values, nameRule);
1335
1348
  return Promise.all(
1336
1349
  paths.map(async (path) => {
1337
1350
  const result = await validate({
@@ -1347,6 +1360,7 @@ var FormModel = class {
1347
1360
  if (field) {
1348
1361
  field.state.errors = errors;
1349
1362
  field.state.warnings = warnings;
1363
+ field.state.invalid = hasError(errors);
1350
1364
  field.bubbleState();
1351
1365
  }
1352
1366
  this.state.errors = mergeFeedbacks(this.state.errors, errors);