Skip to content

Commit a5a3504

Browse files
committed
worked on button
1 parent 6953fe2 commit a5a3504

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

lib/client/vanilla/button.tsx

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,32 @@ const Dialog: React.FC<DialogProps> = ({ open, setOpen, selectedElement }) => {
5252
selectedElement.setAttribute('type', 'button')
5353
} else if (type === 'submit') {
5454
if (submitAsync) {
55-
// TODO submit with fetch
56-
// selectedElement.setAttribute('onclick', `alert('submit async')`)
57-
// selectedElement.setAttribute('type', 'submit')
55+
const source = `
56+
const form = e.target.closest('form')
57+
58+
const formData = new FormData()
59+
for (let e of form.elements) {
60+
if (e.type !== 'submit') {
61+
formData.append(e.id, e.type === 'radio' ? e.checked : e.value)
62+
}
63+
}
64+
65+
const options = { method: '${submitMethod}', ...('${submitMethod}' !== 'GET' ? { body: formData } : {}), }
66+
fetch('${submitUrl}', options)
67+
.then((e) => e.text().then((d) => ({ ok: e.ok, text: d })))
68+
.then(({ ok, text }) => {
69+
alert(ok ? text ?? 'Success' : 'Something went wrong')
70+
})
71+
`
72+
selectedElement.setAttribute('onclick', source)
73+
selectedElement.setAttribute('type', 'button')
5874
} else {
59-
// TODO form submit
60-
// selectedElement.setAttribute('onclick', `alert('submit')`)
61-
// selectedElement.setAttribute('type', 'submit')
75+
const source = `
76+
const form = e.target.closest('form')
77+
form.submit()
78+
`
79+
selectedElement.setAttribute('onclick', source)
80+
selectedElement.setAttribute('type', 'button')
6281
}
6382
}
6483
}

0 commit comments

Comments
 (0)