@formality-ui/core 0.2.3 → 0.2.4

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.cjs CHANGED
@@ -464,6 +464,33 @@ function inferFieldsFromDescriptor(descriptor) {
464
464
  }
465
465
 
466
466
  // src/conditions/evaluate.ts
467
+ function buildFormStateFromInput(fieldValues, fieldStates, record) {
468
+ const fields = {};
469
+ for (const [name, value] of Object.entries(fieldValues)) {
470
+ const state = fieldStates?.[name];
471
+ fields[name] = {
472
+ value,
473
+ isTouched: state?.isTouched ?? false,
474
+ isDirty: state?.isDirty ?? false,
475
+ isValidating: state?.isValidating ?? false,
476
+ error: state?.error,
477
+ invalid: state?.invalid ?? false,
478
+ disabled: state?.disabled
479
+ };
480
+ }
481
+ return {
482
+ fields,
483
+ record: record ?? {},
484
+ errors: {},
485
+ defaultValues: {},
486
+ touchedFields: {},
487
+ dirtyFields: {},
488
+ isDirty: false,
489
+ isTouched: false,
490
+ isValid: true,
491
+ isSubmitting: false
492
+ };
493
+ }
467
494
  function evaluateFieldMatcher(fieldName, matcher, fieldValues, fieldStates) {
468
495
  const fieldValue = fieldValues[fieldName];
469
496
  const fieldState = fieldStates?.[fieldName];
@@ -535,7 +562,11 @@ function evaluateConditionMatch(condition, context, fieldValues, fieldStates) {
535
562
  if (typeof condition.selectWhen === "string") {
536
563
  triggerValue = evaluate(condition.selectWhen, context);
537
564
  } else if (typeof condition.selectWhen === "function") {
538
- return false;
565
+ const ctxRecord = context.record ?? void 0;
566
+ triggerValue = condition.selectWhen(
567
+ buildFormStateFromInput(fieldValues, fieldStates, ctxRecord),
568
+ void 0
569
+ );
539
570
  } else {
540
571
  triggerValue = evaluateDescriptor(condition.selectWhen, context);
541
572
  }
@@ -609,7 +640,10 @@ function evaluateConditions(input) {
609
640
  if (typeof condition.selectSet === "string") {
610
641
  setValue = unwrapFieldProxy(evaluate(condition.selectSet, context));
611
642
  } else if (typeof condition.selectSet === "function") {
612
- setValue = condition.selectSet;
643
+ setValue = condition.selectSet(
644
+ buildFormStateFromInput(fieldValues, fieldStates, record),
645
+ void 0
646
+ );
613
647
  } else {
614
648
  setValue = unwrapFieldProxy(
615
649
  evaluateDescriptor(condition.selectSet, context)
@@ -702,6 +736,15 @@ function runSingleValidator(validator, value, formValues) {
702
736
  function resolveNamedValidator(name, validators) {
703
737
  const validator = validators[name];
704
738
  if (typeof validator === "function") {
739
+ let probe;
740
+ try {
741
+ probe = validator(void 0, {});
742
+ } catch {
743
+ return validator;
744
+ }
745
+ if (typeof probe === "function") {
746
+ return validator();
747
+ }
705
748
  return validator;
706
749
  }
707
750
  return void 0;