import { BaseController } from '../../utilities/base_controller' ; import { requestReset , requestSubmit } from "../../utilities/request_submit" ; export class FormRcController extends BaseController { static targets = [ "form" ] ; static values = { formSelector : String , submitMode : String , } ; declare submitModeValue : "direct" | "request" ; declare readonly hasSubmitModeValue : boolean ; declare readonly formTarget : HTMLFormElement ; declare readonly formSelectorValue : string ; declare readonly hasFormSelectorValue : boolean ; get _mode ( ) : "direct" | "request" { if ( this . hasSubmitModeValue ) { if ( ! [ "direct" , "request" ] . includes ( this . submitModeValue ) ) { throw new Error ( `The modeValue provided '${ this . submitModeValue }' is not one of the recognised configuration options` ) ; } return this . submitModeValue ; } else { return "request" ; } } get form ( ) : HTMLFormElement { let form ; if ( this . hasFormSelectorValue ) { form = document . querySelector ( this . formSelectorValue ) ; if ( ! form ) { throw new Error ( `Could not find a form on the page that matches selector '${ this . formSelectorValue }'` ) ; } } else { form = this . formTarget ; } return form ; } submit ( event ? ) { event ?. preventDefault ( ) ; let el = this . formTarget ; if ( this . _mode == 'request' ) { requestSubmit ( el ) ; } else { el . submit ( ) ; } } reset ( event ? ) { event ?. preventDefault ( ) ; requestReset ( this . form ) ; } }