Merge pull request #35 from strfx/feat/include-submitter-info

feat: include submitter info.
This commit is contained in:
Dusty Greif 2024-05-06 10:29:06 -07:00 коммит произвёл GitHub
Родитель b417792acd 23e97222f8
Коммит 697e317426
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
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()
})
})