cds-snc / gcds-components

GC Design System Components is a monorepo managing the web components of the GC Design System. | « GC Design System Components » (Composants de Système de design GC) est un référentiel unique qui gère les composants Web de Système de design GC.
https://design-system.alpha.canada.ca/
MIT License
37 stars 13 forks source link

bug: Required Text Input does not prevent a form from being submitted #667

Closed grathomp closed 3 weeks ago

grathomp commented 1 month ago

Prerequisites | Prérequis

GC Design System Components Package and Version | Paquet et version des composants de Système de design GC

gcds-components-react": "^0.25.1

Current Behavior | Comportement observé

In version 0.15.0, when an required Input Box was in a (non-gcds) form, it would prevent submission when empty. Now it lets the form be submitted, and after submission all the 'Enter Information to continue.' errors show up.

Expected Behavior | Comportement attendu

Expected behaviour is when its a required Input Box is in a form trying to be submitted, it would prevent submission when empty.

System Info | Information sur le système

Microsoft Edge Browser, Using a react framework and version 0.25.1

Steps to Reproduce | Étapes pour reproduire le bogue

Creating a required Input box within a form

Code Reproduction URL | URL de reproduction du code

https://codesandbox.io/p/sandbox/bug-report-4d3jl9

Additional Information | Informations supplémentaires

No response

daine commented 1 month ago

@grathomp Thanks for the report!

daine commented 1 month ago

@grathomp the sandbox link is broken, could you verify it's correct? TIA!

grathomp commented 1 month ago

Try again now - I've updated permissions

ethanWallace commented 1 month ago

Hey @grathomp, since the v0.15.0 we have made some big updates to our form components. Specifically in our v0.21.0 release we transitioned to use shadow-dom and form-associated components for our form components. This change made it so we would no longer use the browser validation and rely mainly on the built in validation for each form component. The submit event on the form may fire but it does not complete as it is prevented by the form component with the error message.

If you'd like to have additional logic (validation or other use cases), here's an example of how you should be able to wait for the required form elements to be validated first, and adding your logic/code on top of that before it gets submitted to the server.

  const handleSubmit = (event: any) => {
    // Timeout for 50ms to allow components to validate
    setTimeout(() => {
      // If the nativeEvent "submit" has not been prevented
      if(!event.nativeEvent.defaultPrevented) {
        setSubmitted("true");
      }
    }, 50);
  };
ethanWallace commented 3 weeks ago

Closing this issue due to inactivity