Skip to content

Commit

Permalink
worked on button
Browse files Browse the repository at this point in the history
  • Loading branch information
LiveDuo committed Nov 8, 2023
1 parent 6953fe2 commit a5a3504
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions lib/client/vanilla/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,32 @@ const Dialog: React.FC<DialogProps> = ({ open, setOpen, selectedElement }) => {
selectedElement.setAttribute('type', 'button')
} else if (type === 'submit') {
if (submitAsync) {
// TODO submit with fetch
// selectedElement.setAttribute('onclick', `alert('submit async')`)
// selectedElement.setAttribute('type', 'submit')
const source = `
const form = e.target.closest('form')
const formData = new FormData()
for (let e of form.elements) {
if (e.type !== 'submit') {
formData.append(e.id, e.type === 'radio' ? e.checked : e.value)
}
}
const options = { method: '${submitMethod}', ...('${submitMethod}' !== 'GET' ? { body: formData } : {}), }
fetch('${submitUrl}', options)
.then((e) => e.text().then((d) => ({ ok: e.ok, text: d })))
.then(({ ok, text }) => {
alert(ok ? text ?? 'Success' : 'Something went wrong')
})
`
selectedElement.setAttribute('onclick', source)
selectedElement.setAttribute('type', 'button')
} else {
// TODO form submit
// selectedElement.setAttribute('onclick', `alert('submit')`)
// selectedElement.setAttribute('type', 'submit')
const source = `
const form = e.target.closest('form')
form.submit()
`
selectedElement.setAttribute('onclick', source)
selectedElement.setAttribute('type', 'button')
}
}
}
Expand Down

0 comments on commit a5a3504

Please sign in to comment.