easy-forms-core 1.1.0 → 1.1.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/dist/easy-form.js CHANGED
@@ -382,7 +382,41 @@ function getBaseStyles(colors) {
382
382
  .easy-form-wizard-nav {
383
383
  display: flex;
384
384
  gap: 1rem;
385
- margin-top: 1rem;
385
+ margin-top: 2rem;
386
+ justify-content: flex-end;
387
+ align-items: center;
388
+ }
389
+ .easy-form-wizard-prev,
390
+ .easy-form-wizard-next {
391
+ padding: 0.75rem 1.5rem;
392
+ background: var(--easy-form-primary);
393
+ color: white;
394
+ border: none;
395
+ border-radius: 4px;
396
+ font-size: 1rem;
397
+ font-weight: 600;
398
+ cursor: pointer;
399
+ transition: all 0.2s ease;
400
+ font-family: inherit;
401
+ min-width: 120px;
402
+ }
403
+ .easy-form-wizard-prev:hover:not(:disabled),
404
+ .easy-form-wizard-next:hover:not(:disabled) {
405
+ opacity: 0.9;
406
+ transform: translateY(-1px);
407
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
408
+ }
409
+ .easy-form-wizard-prev:disabled {
410
+ opacity: 0.5;
411
+ cursor: not-allowed;
412
+ }
413
+ .easy-form-wizard-prev {
414
+ background: var(--easy-form-secondary);
415
+ order: 1;
416
+ }
417
+ .easy-form-wizard-next {
418
+ background: var(--easy-form-primary);
419
+ order: 2;
386
420
  }
387
421
  .easy-form-array-item {
388
422
  padding: 1rem;
@@ -1731,7 +1765,20 @@ var ValidationEngine = class {
1731
1765
  if (!value) {
1732
1766
  return { isValid: true };
1733
1767
  }
1734
- const regex = typeof pattern === "string" ? new RegExp(pattern) : pattern;
1768
+ let regex;
1769
+ if (pattern instanceof RegExp) {
1770
+ regex = pattern;
1771
+ } else if (typeof pattern === "string") {
1772
+ try {
1773
+ regex = new RegExp(pattern);
1774
+ } catch (e) {
1775
+ console.warn("Invalid regex pattern:", pattern);
1776
+ return { isValid: true };
1777
+ }
1778
+ } else {
1779
+ console.warn("Pattern validation expects string or RegExp, got:", typeof pattern, pattern);
1780
+ return { isValid: true };
1781
+ }
1735
1782
  const isValid = regex.test(String(value));
1736
1783
  return {
1737
1784
  isValid,
@@ -4602,26 +4649,45 @@ var EasyForm = class extends BrowserHTMLElement {
4602
4649
  return;
4603
4650
  }
4604
4651
  const preservedValues = this.preserveCurrentValues();
4652
+ const previousWizardState = this.stateManager.getWizardState();
4605
4653
  if (preservedValues && Object.keys(preservedValues).length > 0) {
4606
4654
  for (const [key, value] of Object.entries(preservedValues)) {
4607
4655
  this.stateManager.setValueWithoutValidation(key, value);
4608
4656
  }
4609
4657
  }
4610
4658
  const initialData = this.initialData;
4611
- this.stateManager.initializeSchema(schema, initialData || void 0);
4612
- const wizardState = this.stateManager.getWizardState();
4659
+ const wasWizard = previousWizardState !== null;
4660
+ const isWizard = schema.steps && schema.steps.length > 0;
4661
+ const shouldReinitialize = !previousWizardState || !wasWizard || !isWizard || previousWizardState && schema.steps && previousWizardState.totalSteps !== schema.steps.length;
4662
+ if (shouldReinitialize) {
4663
+ this.stateManager.initializeSchema(schema, initialData || void 0);
4664
+ if (wasWizard && isWizard && previousWizardState) {
4665
+ const wizardState = this.stateManager.getWizardState();
4666
+ if (wizardState && previousWizardState.totalSteps === wizardState.totalSteps) {
4667
+ if (previousWizardState.currentStep >= 0 && previousWizardState.currentStep < wizardState.totalSteps) {
4668
+ this.stateManager.goToStep(previousWizardState.currentStep);
4669
+ }
4670
+ for (const completedStep of previousWizardState.completedSteps) {
4671
+ if (completedStep >= 0 && completedStep < wizardState.totalSteps) {
4672
+ this.stateManager.completeStep(completedStep);
4673
+ }
4674
+ }
4675
+ }
4676
+ }
4677
+ }
4678
+ const finalWizardState = this.stateManager.getWizardState();
4613
4679
  const newFormElement = document.createElement("form");
4614
4680
  newFormElement.addEventListener("submit", (e) => this.handleSubmit(e));
4615
- if (wizardState) {
4616
- this.renderWizard(newFormElement, wizardState);
4681
+ if (finalWizardState) {
4682
+ this.renderWizard(newFormElement);
4617
4683
  } else {
4618
4684
  this.renderFields(newFormElement, schema.fields || []);
4685
+ const submitButton = document.createElement("button");
4686
+ submitButton.type = "submit";
4687
+ submitButton.textContent = "Enviar";
4688
+ submitButton.className = "easy-form-submit";
4689
+ newFormElement.appendChild(submitButton);
4619
4690
  }
4620
- const submitButton = document.createElement("button");
4621
- submitButton.type = "submit";
4622
- submitButton.textContent = "Enviar";
4623
- submitButton.className = "easy-form-submit";
4624
- newFormElement.appendChild(submitButton);
4625
4691
  const oldForm = this.shadow.querySelector("form");
4626
4692
  if (oldForm && oldForm.parentNode === this.shadow && oldForm !== newFormElement) {
4627
4693
  try {
@@ -4910,7 +4976,9 @@ var EasyForm = class extends BrowserHTMLElement {
4910
4976
  /**
4911
4977
  * Renderiza wizard
4912
4978
  */
4913
- renderWizard(container, wizardState) {
4979
+ renderWizard(container) {
4980
+ const wizardState = this.stateManager.getWizardState();
4981
+ if (!wizardState) return;
4914
4982
  const wizardContainer = document.createElement("div");
4915
4983
  wizardContainer.className = "easy-form-wizard";
4916
4984
  const stepsIndicator = document.createElement("div");
@@ -4943,43 +5011,83 @@ var EasyForm = class extends BrowserHTMLElement {
4943
5011
  wizardContainer.appendChild(fieldsContainer);
4944
5012
  const navContainer = document.createElement("div");
4945
5013
  navContainer.className = "easy-form-wizard-nav";
4946
- const prevButton = document.createElement("button");
4947
- prevButton.type = "button";
4948
- prevButton.textContent = "Anterior";
4949
- prevButton.className = "easy-form-wizard-prev";
4950
- prevButton.disabled = wizardState.currentStep === 0;
4951
- prevButton.addEventListener("click", () => {
4952
- if (this.stateManager.previousStep()) {
4953
- this.render();
4954
- this.emitStepChange();
4955
- }
4956
- });
4957
- navContainer.appendChild(prevButton);
4958
- const nextButton = document.createElement("button");
4959
- nextButton.type = "button";
4960
- nextButton.textContent = wizardState.currentStep === wizardState.totalSteps - 1 ? "Enviar" : "Siguiente";
4961
- nextButton.className = "easy-form-wizard-next";
4962
- nextButton.addEventListener("click", async () => {
4963
- if (wizardState.currentStep === wizardState.totalSteps - 1) {
4964
- await this.handleSubmit(new Event("submit"));
4965
- } else {
5014
+ if (wizardState.currentStep > 0) {
5015
+ const prevButton = document.createElement("button");
5016
+ prevButton.type = "button";
5017
+ prevButton.textContent = "Anterior";
5018
+ prevButton.className = "easy-form-wizard-prev";
5019
+ prevButton.addEventListener("click", () => {
5020
+ const currentState = this.stateManager.getWizardState();
5021
+ if (currentState && this.stateManager.previousStep()) {
5022
+ this.render();
5023
+ this.emitStepChange();
5024
+ }
5025
+ });
5026
+ navContainer.appendChild(prevButton);
5027
+ }
5028
+ if (wizardState.currentStep < wizardState.totalSteps - 1) {
5029
+ const nextButton = document.createElement("button");
5030
+ nextButton.type = "button";
5031
+ nextButton.textContent = "Siguiente";
5032
+ nextButton.className = "easy-form-wizard-next";
5033
+ nextButton.addEventListener("click", async () => {
5034
+ const currentState = this.stateManager.getWizardState();
5035
+ if (!currentState) return;
4966
5036
  const currentFields2 = this.stateManager.getCurrentStepFields();
4967
- const errors = await this.stateManager.validateForm();
4968
- const hasErrors = currentFields2.some(
4969
- (f) => errors[f.name] && errors[f.name].length > 0
4970
- );
5037
+ for (const field of currentFields2) {
5038
+ if (this.stateManager.getFieldVisibility(field.name)) {
5039
+ await this.stateManager.validateField(field.name);
5040
+ }
5041
+ }
5042
+ const allErrors = this.stateManager.getAllErrors();
5043
+ const stepErrors = {};
5044
+ for (const field of currentFields2) {
5045
+ if (allErrors[field.name] && allErrors[field.name].length > 0) {
5046
+ stepErrors[field.name] = allErrors[field.name];
5047
+ }
5048
+ }
5049
+ const hasErrors = Object.keys(stepErrors).length > 0;
4971
5050
  if (!hasErrors) {
4972
- this.stateManager.completeStep(wizardState.currentStep);
4973
- if (this.stateManager.nextStep()) {
5051
+ this.stateManager.completeStep(currentState.currentStep);
5052
+ const moved = this.stateManager.nextStep();
5053
+ if (moved) {
4974
5054
  this.render();
4975
5055
  this.emitStepChange();
4976
5056
  }
4977
5057
  } else {
4978
- this.emitError(errors);
5058
+ this.emitError(stepErrors);
4979
5059
  }
4980
- }
4981
- });
4982
- navContainer.appendChild(nextButton);
5060
+ });
5061
+ navContainer.appendChild(nextButton);
5062
+ }
5063
+ if (wizardState.currentStep === wizardState.totalSteps - 1) {
5064
+ const submitButton = document.createElement("button");
5065
+ submitButton.type = "button";
5066
+ submitButton.textContent = "Enviar";
5067
+ submitButton.className = "easy-form-wizard-next";
5068
+ submitButton.addEventListener("click", async () => {
5069
+ const currentFields2 = this.stateManager.getCurrentStepFields();
5070
+ for (const field of currentFields2) {
5071
+ if (this.stateManager.getFieldVisibility(field.name)) {
5072
+ await this.stateManager.validateField(field.name);
5073
+ }
5074
+ }
5075
+ const allErrors = this.stateManager.getAllErrors();
5076
+ const stepErrors = {};
5077
+ for (const field of currentFields2) {
5078
+ if (allErrors[field.name] && allErrors[field.name].length > 0) {
5079
+ stepErrors[field.name] = allErrors[field.name];
5080
+ }
5081
+ }
5082
+ const hasErrors = Object.keys(stepErrors).length > 0;
5083
+ if (!hasErrors) {
5084
+ await this.handleSubmit(new Event("submit"));
5085
+ } else {
5086
+ this.emitError(stepErrors);
5087
+ }
5088
+ });
5089
+ navContainer.appendChild(submitButton);
5090
+ }
4983
5091
  wizardContainer.appendChild(navContainer);
4984
5092
  container.appendChild(wizardContainer);
4985
5093
  }