All files / src/lib/validation isFormSubmitDisabled.ts

77.77% Statements 7/9
85.71% Branches 6/7
100% Functions 1/1
77.77% Lines 7/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57                                                              2x         64x   64x 64x   64x                       64x 64x    
/**
 * Determines whether a form submit button should be disabled.
 *
 * Why this helper exists
 * ─────────────────────
 * Mantine Form v9 (alpha) changed the return type of `form.isValid()` to
 * `boolean | Promise<boolean>` when async validation rules are possible.
 * Putting that value directly into a boolean conditional (e.g. `!form.isValid()`)
 * triggers SonarQube rule typescript:S6544 ("Expected non-Promise value in a
 * boolean conditional") and is also subtly wrong at runtime: a Promise object
 * is always truthy, so `!form.isValid()` would always be `false` when async
 * rules resolve to a Promise.
 *
 * We unwrap the result explicitly with an `instanceof Promise` guard:
 *   - Sync validator  → plain `boolean` → use directly
 *   - Async validator → `Promise`       → treat as "not yet valid" (disabled)
 *
 * This is safer than checking `Object.keys(form.errors)`, which only reflects
 * errors that have already been surfaced via a blur/submit event – meaning the
 * button could incorrectly appear enabled before the user interacts with the form.
 */
 
import type { FormLike } from './types';
 
/**
 * Returns `true` when the submit button should be **disabled**.
 *
 * @param form     - The Mantine `useForm` instance.
 * @param loading  - Whether an async operation (mutation / request) is in flight.
 * @param extra    - Optional additional loading flags (e.g. `isLoggingIn`).
 */
export const isFormSubmitDisabled = (
  form: FormLike,
  loading: boolean,
  ...extra: boolean[]
): boolean => {
  let isFormValid = false;
 
  try {
    const validResult = form.isValid();
    // Safely unwrap: async validators (Promise) count as "not yet valid".
    isFormValid = validResult instanceof Promise ? false : validResult;
  } catch (err: unknown) {
    // Some Mantine versions may expect internal structures (like rules)
    // to exist when `isValid()` is called; in those cases fall back to a
    // conservative behaviour: treat the form as invalid so the submit stays
    // disabled. Also log the error to surface unexpected failures instead of
    // silently swallowing them (helps debugging and satisfies S2486).
    // eslint-disable-next-line no-console
    console.error('isFormSubmitDisabled: form.isValid() threw:', err);
    isFormValid = false;
  }
 
  const isLoading = loading || extra.some(Boolean);
  return isLoading || !isFormValid || !form.isDirty();
};