зеркало из https://github.com/github/remote-form.git
feat: include submitter info.
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:
Родитель
b417792acd
Коммит
23e97222f8
18
src/index.ts
18
src/index.ts
|
@ -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
|
||||
|
|
11
test/test.js
11
test/test.js
|
@ -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()
|
||||
})
|
||||
})
|
||||
|
|
Загрузка…
Ссылка в новой задаче