You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I think these new comments should prevent the bot from closing this issue. FWIW, we're planning on coming back to this soon after we finish everything off for the next stable release of Lit.
Reacted by Fernando Pasik, Abdón Rodríguez, South Drifter, Cory LaViska, JC Franco and Yuki Nishijima
I have been playing about with a very light polyfill for this. I have only attempted to support appending data since that is probably the 99% use case, especially for web components.
The FormData class is basically available everywhere, the only missing part is the corresponding formdata event. So we can utilise this to keep the polyfill small. I add a hidden input to the form when data is appended.
Here's what i have so far, including a feature test. It works in safari. I'm going to experiment with it further.
functionsupportsFormDataEvent({ document }){letisSupported=falseconstform=document.createElement("form")document.body.appendChild(form)form.addEventListener("submit",e=>{e.preventDefault()// this dispatches formdata event in browsers that support itnewFormData(e.target)})form.addEventListener("formdata",()=>{isSupported=true})form.dispatchEvent(newEvent("submit"))form.remove()returnisSupported}exportfunctionpolyfillFormData(win){if(!win.FormData){return}if(supportsFormDataEvent(win)){return}classFormDataPolyextendsFormData{constructor(form){super(form)this.form=form}append(name,value){super.append(name,value)letinput=this.form.elements[name]if(!input){input=document.createElement("input")input.type="hidden"input.name=name}input.value=valuethis.form.appendChild(input)}}classFormDataEventextendsEvent{constructor(form){super("formdata")this.formData=newFormDataPoly(form)}}win.addEventListener("submit",e=>{if(!e.defaultPrevented){e.target.dispatchEvent(newFormDataEvent(e.target))}})}
Reacted by South Drifter, JC Franco and Matt Driscoll
I just noticed a polyfill has already been added to the repo, which is obviously more complete (at the cost of file size/invasiveness). Perhaps this issue can be closed if it is good to go :)
There were actually some small issues with the code I posted above. It didn't correctly handle anything but the first form submission, ending up with duplicate entries on subsequent submissions. Nor did it support manually calling new FormData(form) to gather form values outside of a submit event.
The version below fixes these issues, and works well in my testing.
/* eslint-disable max-classes-per-file */classFormDataEventextendsEvent{constructor(formData){super("formdata")this.formData=formData}}classFormDataPolyfilledextendsFormData{constructor(form){super(form)this.form=formform.dispatchEvent(newFormDataEvent(this))}append(name,value){letinput=this.form.elements[name]if(!input){input=document.createElement("input")input.type="hidden"input.name=namethis.form.appendChild(input)}// if the name already exists, there is already a hidden input in the dom// and it will have been picked up by FormData during construction.// in this case, we can't just blindly append() since that will result in two entries.// nor can we blindly delete() the entry, since there can be multiple entries per name (e.g. checkboxes).// so we must carefully splice out the old value, and add back in the new valueif(this.has(name)){constentries=this.getAll(name)constindex=entries.indexOf(input.value)if(index!==-1){entries.splice(index,1)}entries.push(value)this.set(name,entries)}else{super.append(name,value)}input.value=value}}functionsupportsFormDataEvent({ document }){letisSupported=falseconstform=document.createElement("form")document.body.appendChild(form)form.addEventListener("submit",e=>{e.preventDefault()// this dispatches formdata event in browsers that support itnewFormData(e.target)// eslint-disable-line no-new})form.addEventListener("formdata",()=>{isSupported=true})form.dispatchEvent(newEvent("submit",{cancelable: true}))form.remove()returnisSupported}functionpolyfillFormData(win){if(!win.FormData||supportsFormDataEvent(win)){return}window.FormData=FormDataPolyfilledwin.addEventListener("submit",e=>{if(!e.defaultPrevented){// eslint-disable-next-line no-newnewFormData(e.target)}})}polyfillFormData(window)
Reacted by South Drifter, Matt Driscoll, JC Franco, Yuki Nishijima, Elwin van Eede, Primož and Cory LaViska
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
New version of React official document is dropping Class components, Web components is the right way to write Class components, hope to improve these details.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Activity
YonatanKra commentedon Aug 4, 2020
Note this is not supported in Safari
stale commentedon Aug 4, 2021
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
fernandopasik commentedon Aug 4, 2021
would it be possible to avoid this one from going stale?
bicknellr commentedon Aug 4, 2021
I think these new comments should prevent the bot from closing this issue. FWIW, we're planning on coming back to this soon after we finish everything off for the next stable release of Lit.
WickyNilliams commentedon Sep 21, 2021
I have been playing about with a very light polyfill for this. I have only attempted to support appending data since that is probably the 99% use case, especially for web components.
The
FormDataclass is basically available everywhere, the only missing part is the correspondingformdataevent. So we can utilise this to keep the polyfill small. I add a hidden input to the form when data is appended.Here's what i have so far, including a feature test. It works in safari. I'm going to experiment with it further.
WickyNilliams commentedon Sep 21, 2021
I just noticed a polyfill has already been added to the repo, which is obviously more complete (at the cost of file size/invasiveness). Perhaps this issue can be closed if it is good to go :)
WickyNilliams commentedon Sep 29, 2021
There were actually some small issues with the code I posted above. It didn't correctly handle anything but the first form submission, ending up with duplicate entries on subsequent submissions. Nor did it support manually calling
new FormData(form)to gather form values outside of a submit event.The version below fixes these issues, and works well in my testing.
Here's a gist of the code with an explicit license: https://gist.github.com/WickyNilliams/eb6a44075356ee504dd9491c5a3ab0be
stale commentedon Jan 21, 2023
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
TechQuery commentedon Jan 23, 2023
Any update in 2023 ?
New version of React official document is dropping Class components, Web components is the right way to write Class components, hope to improve these details.
stale commentedon Apr 26, 2025
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
TechQuery commentedon May 15, 2025
List some downstream issues:
formdataevent jsdom/jsdom#3799formdataevent when FormData is created from form JavaScriptRegenerated/yieldmachine#31