gyosjs 0.2.1 → 0.2.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/gyos.esm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * GyosJS v0.2.1
2
+ * GyosJS v0.2.2
3
3
  * (c) 2026
4
4
  * @license MIT
5
5
  */
@@ -3018,6 +3018,33 @@ validator('between', (value, min, max) => {
3018
3018
  return (num >= minVal && num <= maxVal) || `Must be between ${minVal} and ${maxVal}`;
3019
3019
  });
3020
3020
 
3021
+ const managedForms = new WeakSet();
3022
+ const approvedSubmissions = new WeakSet();
3023
+ // Coordinates the validation listener on a form with the Router's earlier
3024
+ // document-capture listener without exposing state on application elements.
3025
+ function registerValidatedForm(form) {
3026
+ managedForms.add(form);
3027
+ }
3028
+ function unregisterValidatedForm(form) {
3029
+ managedForms.delete(form);
3030
+ approvedSubmissions.delete(form);
3031
+ }
3032
+ function approveNextFormSubmission(form) {
3033
+ approvedSubmissions.add(form);
3034
+ }
3035
+ function cancelApprovedFormSubmission(form) {
3036
+ approvedSubmissions.delete(form);
3037
+ }
3038
+ function shouldDeferToFormValidation(form) {
3039
+ if (!managedForms.has(form))
3040
+ return false;
3041
+ if (approvedSubmissions.has(form)) {
3042
+ approvedSubmissions.delete(form);
3043
+ return false;
3044
+ }
3045
+ return true;
3046
+ }
3047
+
3021
3048
  /**
3022
3049
  * Form Validation Directives
3023
3050
  * Provides g-form, g-validate, and g-errors directives for reactive form validation
@@ -3032,6 +3059,23 @@ function getFieldName(el) {
3032
3059
  const modelAttr = Array.from(el.attributes).find(attr => attr.name === 'g-model' || attr.name.startsWith('g-model.'));
3033
3060
  return modelAttr?.value || el.getAttribute('name') || el.id;
3034
3061
  }
3062
+ function getControlValue(el, form) {
3063
+ if (!(el instanceof HTMLInputElement))
3064
+ return el.value;
3065
+ if (el.type === 'checkbox')
3066
+ return el.checked ? el.value : '';
3067
+ if (el.type !== 'radio')
3068
+ return el.value;
3069
+ const fieldName = getFieldName(el);
3070
+ const checked = Array.from(form.querySelectorAll('input[type="radio"]')).find(candidate => {
3071
+ if (isInIgnoredTree(candidate) || !candidate.checked)
3072
+ return false;
3073
+ if (el.name && candidate.name === el.name)
3074
+ return true;
3075
+ return getFieldName(candidate) === fieldName;
3076
+ });
3077
+ return checked?.value ?? '';
3078
+ }
3035
3079
  /**
3036
3080
  * Get or create form validation state
3037
3081
  */
@@ -3083,6 +3127,7 @@ function processFormDirective(form, scope) {
3083
3127
  return;
3084
3128
  form.removeAttribute('g-form');
3085
3129
  const state = getFormState(form);
3130
+ registerValidatedForm(form);
3086
3131
  // Create validateAll function
3087
3132
  const validateAll = async () => {
3088
3133
  const validators = formValidators.get(form) || [];
@@ -3116,30 +3161,77 @@ function processFormDirective(form, scope) {
3116
3161
  // Merge signals into existing object
3117
3162
  Object.assign(scope[formName], formStateObj);
3118
3163
  }
3119
- // Prevent default form submission and validate first
3164
+ let replayingSubmission = false;
3165
+ let validatingSubmission = false;
3166
+ let replayScheduled = false;
3167
+ let replayTimer;
3168
+ let active = true;
3169
+ // Hold the first submit while validation runs, then replay a normal submit
3170
+ // so MPA Boost, native navigation, and user submit listeners keep working.
3120
3171
  const submitListener = async (e) => {
3121
- e.preventDefault();
3122
- const isValid = await validateAll();
3123
- if (!isValid)
3172
+ if (replayingSubmission) {
3173
+ replayingSubmission = false;
3174
+ cancelApprovedFormSubmission(form);
3124
3175
  return;
3125
- const submitMethod = form.getAttribute('g-submit');
3126
- // If not defined, submit the form normally
3127
- if (!submitMethod) {
3128
- form.submit();
3176
+ }
3177
+ e.preventDefault();
3178
+ e.stopImmediatePropagation();
3179
+ if (validatingSubmission || replayScheduled)
3129
3180
  return;
3181
+ validatingSubmission = true;
3182
+ try {
3183
+ const isValid = await validateAll();
3184
+ if (!active || !isValid)
3185
+ return;
3186
+ const submitMethod = form.getAttribute('g-submit');
3187
+ // If not defined, replay through the normal submit event pipeline.
3188
+ if (!submitMethod) {
3189
+ const submitter = typeof SubmitEvent !== 'undefined' && e instanceof SubmitEvent && (e.submitter instanceof HTMLButtonElement || e.submitter instanceof HTMLInputElement) ? e.submitter : undefined;
3190
+ // Browsers ignore a re-entrant requestSubmit while the original
3191
+ // submission algorithm is still finishing. Replay in the next task.
3192
+ replayScheduled = true;
3193
+ replayTimer = setTimeout(() => {
3194
+ replayScheduled = false;
3195
+ replayTimer = undefined;
3196
+ if (!active)
3197
+ return;
3198
+ approveNextFormSubmission(form);
3199
+ replayingSubmission = true;
3200
+ try {
3201
+ form.requestSubmit(submitter);
3202
+ }
3203
+ finally {
3204
+ // requestSubmit dispatches synchronously. If native validation or
3205
+ // another capture listener stopped it, do not leak approval state.
3206
+ if (replayingSubmission) {
3207
+ replayingSubmission = false;
3208
+ cancelApprovedFormSubmission(form);
3209
+ }
3210
+ }
3211
+ }, 0);
3212
+ return;
3213
+ }
3214
+ // Trigger custom submit handler if exists
3215
+ const submitHandler = scope[submitMethod];
3216
+ if (typeof submitHandler === 'function') {
3217
+ submitHandler.call(scope);
3218
+ }
3130
3219
  }
3131
- // Trigger custom submit handler if exists
3132
- const submitHandler = scope[submitMethod];
3133
- if (typeof submitHandler === 'function') {
3134
- submitHandler.call(scope);
3220
+ finally {
3221
+ validatingSubmission = false;
3135
3222
  }
3136
3223
  };
3137
- form.addEventListener('submit', submitListener);
3224
+ form.addEventListener('submit', submitListener, true);
3138
3225
  if (!form.__gyos_effects__) {
3139
3226
  form.__gyos_effects__ = [];
3140
3227
  }
3141
3228
  form.__gyos_effects__.push(() => {
3142
- form.removeEventListener('submit', submitListener);
3229
+ active = false;
3230
+ if (replayTimer !== undefined)
3231
+ clearTimeout(replayTimer);
3232
+ replayScheduled = false;
3233
+ form.removeEventListener('submit', submitListener, true);
3234
+ unregisterValidatedForm(form);
3143
3235
  formStates.delete(form);
3144
3236
  formValidators.delete(form);
3145
3237
  });
@@ -3175,7 +3267,7 @@ function processValidateDirective(el) {
3175
3267
  state.touched.value[modelAttr] = false;
3176
3268
  // Validation function
3177
3269
  const validateField = async () => {
3178
- const value = el.value;
3270
+ const value = getControlValue(el, form);
3179
3271
  // Get form context for validators that need it (like 'same')
3180
3272
  const formData = {};
3181
3273
  const formInputs = form.querySelectorAll('input, textarea, select');
@@ -3185,7 +3277,7 @@ function processValidateDirective(el) {
3185
3277
  if (input instanceof HTMLInputElement || input instanceof HTMLTextAreaElement || input instanceof HTMLSelectElement) {
3186
3278
  const fieldName = getFieldName(input);
3187
3279
  if (fieldName) {
3188
- formData[fieldName] = input.value;
3280
+ formData[fieldName] = getControlValue(input, form);
3189
3281
  }
3190
3282
  }
3191
3283
  });
@@ -3689,6 +3781,9 @@ function processModel(element, scope, root) {
3689
3781
  if (inputElement.type === 'checkbox') {
3690
3782
  inputElement.checked = !!initialValue;
3691
3783
  }
3784
+ else if (inputElement.type === 'radio') {
3785
+ inputElement.checked = String(unref(initialValue)) === inputElement.value;
3786
+ }
3692
3787
  else {
3693
3788
  inputElement.value = unref(initialValue);
3694
3789
  }
@@ -3711,6 +3806,8 @@ function processModel(element, scope, root) {
3711
3806
  };
3712
3807
  const eventInput = (e) => {
3713
3808
  const target = e.target;
3809
+ if (target.type === 'radio' && !target.checked)
3810
+ return;
3714
3811
  const value = target.type === 'checkbox' ? target.checked : target.value;
3715
3812
  debouncedUpdate(value);
3716
3813
  };
@@ -3730,6 +3827,9 @@ function processModel(element, scope, root) {
3730
3827
  if (inputElement.type === 'checkbox') {
3731
3828
  inputElement.checked = !!newValue;
3732
3829
  }
3830
+ else if (inputElement.type === 'radio') {
3831
+ inputElement.checked = String(unref(newValue)) === inputElement.value;
3832
+ }
3733
3833
  else if (inputElement.value !== newValue) {
3734
3834
  inputElement.value = unref(newValue);
3735
3835
  }
@@ -5369,6 +5469,7 @@ function autoBindModelProp(el, scope) {
5369
5469
  const attrs = node.getAttributeNames();
5370
5470
  return attrs.some(attr => attr.startsWith('g-model'));
5371
5471
  });
5472
+ const radioDefaults = new Map();
5372
5473
  for (const node of walker) {
5373
5474
  if (isInIgnoredTree(node))
5374
5475
  continue;
@@ -5377,13 +5478,34 @@ function autoBindModelProp(el, scope) {
5377
5478
  if (!modelAttr)
5378
5479
  continue;
5379
5480
  // If prop exists or is nested, skip
5380
- if (modelAttr.value in scope || modelAttr.value.includes('.'))
5481
+ if (modelAttr.value.includes('.'))
5381
5482
  continue;
5382
5483
  const element = node;
5484
+ if (element.type === 'radio') {
5485
+ if (modelAttr.value in scope)
5486
+ continue;
5487
+ if (!radioDefaults.has(modelAttr.value))
5488
+ radioDefaults.set(modelAttr.value, '');
5489
+ if (element.checked) {
5490
+ const value = modelAttr.name.split('.').includes('number')
5491
+ ? Number(element.value)
5492
+ : element.value;
5493
+ radioDefaults.set(modelAttr.value, value);
5494
+ }
5495
+ continue;
5496
+ }
5497
+ if (modelAttr.value in scope)
5498
+ continue;
5383
5499
  const valueAuto = element.type === 'checkbox' ? element.checked : element.value;
5384
5500
  scope[modelAttr.value] = valueAuto;
5385
5501
  DEBUG() && console.log(`[g-model] Auto-bound property "${modelAttr.value}" with initial value:`, valueAuto);
5386
5502
  }
5503
+ for (const [prop, value] of radioDefaults) {
5504
+ if (prop in scope)
5505
+ continue;
5506
+ scope[prop] = value;
5507
+ DEBUG() && console.log(`[g-model] Auto-bound radio property "${prop}" with initial value:`, value);
5508
+ }
5387
5509
  }
5388
5510
  function getPersistKey$1(el, callback) {
5389
5511
  if (!el.hasAttribute('g-scope-persist'))
@@ -7763,6 +7885,8 @@ function onSubmitCapture(event) {
7763
7885
  return;
7764
7886
  if (!(form.hasAttribute('g-boost') || hasGlobalBoost()))
7765
7887
  return;
7888
+ if (shouldDeferToFormValidation(form))
7889
+ return;
7766
7890
  event.preventDefault();
7767
7891
  const action = form.action || window.location.href;
7768
7892
  const method = (form.method || 'GET').toUpperCase();
@@ -7879,7 +8003,7 @@ const Gyos = {
7879
8003
  onAfterNavigate,
7880
8004
  portalCreate,
7881
8005
  portalDestroy,
7882
- version: '0.2.1'
8006
+ version: '0.2.2'
7883
8007
  };
7884
8008
 
7885
8009
  export { applyDirective, applyTransitionStyles, batch, disposeEffects as cleanup, clearAllEvents, computed, debounce, Gyos as default, directive, effect, emit, getEventListeners, getGlobalContainer, getStoreNames, getTransitionConfig, getValidator, getValidatorNames, hasStore, inject, isComputed, isSignal, markRaw, mount, mountAll, mountTree, getAllMountedScopes as mountedScopes, nextTick, off, on, onAfterNavigate, onBeforeNavigate, once, pipe, portalCreate, portalDestroy, provide, ready, registerTransition, removeStore, scope, shallow, signal, startRouter, store, throttle, unref, untrack, useAsync, useCounter, useDebounce, useFetch, useInterval, useLocalStorage, useMediaQuery, useMouse, useThrottle, useTimeout, useToggle, useWindowSize, validate, validator };