Buttons allow you to overwrite a forms `method` with the `formmethod`
attribute. `buildRequest` now incorporates submitter info when
determining the submit requests method.
This commit is contained in:
Claudio Pilotti 2024-05-02 10:29:27 +02:00
Родитель b417792acd
Коммит 23e97222f8
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: FA4729D9130879FE
2 изменённых файлов: 26 добавлений и 3 удалений

Просмотреть файл

@ -65,6 +65,9 @@ export type RemoteFormHandler = (form: HTMLFormElement, kicker: Kicker, req: Sim
// element.
type Installable = string | HTMLFormElement
// Any element that submits a <form>
type HTMLSubmitElement = HTMLButtonElement | HTMLInputElement
let formHandlers: Map<Installable, RemoteFormHandler[]>
type Handler = (form: HTMLFormElement) => void
@ -133,7 +136,16 @@ function handleSubmit(event: Event) {
return
}
const req = buildRequest(form)
let submitter
if (event instanceof SubmitEvent) {
submitter = event.submitter as HTMLSubmitElement
} else {
submitter = null
}
const req = buildRequest(form, submitter)
const [kickerPromise, ultimateResolve, ultimateReject] = makeDeferred<SimpleResponse>()
event.preventDefault()
@ -202,9 +214,9 @@ async function processHandlers(
return kickerWasCalled
}
function buildRequest(form: HTMLFormElement): SimpleRequest {
function buildRequest(form: HTMLFormElement, submitter: HTMLSubmitElement | null): SimpleRequest {
const req: SimpleRequest = {
method: form.method || 'GET',
method: submitter?.formMethod || form.method || 'GET',
url: form.action,
headers: new Headers({'X-Requested-With': 'XMLHttpRequest'}),
body: null

Просмотреть файл

@ -164,4 +164,15 @@ describe('remoteForm', function () {
assert.isFalse(handlerCalled)
document.removeEventListener('submit', defaultPreventHandler, {capture: true})
})
it('overwrites form method with buttons formmethod', function (done) {
remoteForm(htmlForm, async (form, wants, req) => {
assert.equal(req.method.toUpperCase(), 'GET')
done()
})
const button = document.querySelector('button[type=submit]')
button.formMethod = 'get'
button.click()
})
})