Skip to content
33 changes: 32 additions & 1 deletion dom/src/main/scala/org/scalajs/dom/FormData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,48 @@ package org.scalajs.dom

import scala.scalajs.js
import scala.scalajs.js.annotation._
import scala.scalajs.js.|

/** XMLHttpRequest Level 2 adds support for the new FormData interface. FormData objects provide a way to easily
* construct a set of key/value pairs representing form fields and their values, which can then be easily sent using
* the XMLHttpRequest send() method.
*/
@js.native
@JSGlobal
class FormData(form: HTMLFormElement = js.native) extends js.Object {
class FormData(form: HTMLFormElement = js.native) extends js.Iterable[js.Tuple2[String, String]] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class FormData(form: HTMLFormElement = js.native) extends js.Iterable[js.Tuple2[String, String]] {
class FormData(form: HTMLFormElement = js.native) extends js.Iterable[js.Tuple2[String, String | Blob]] {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clear

Copy link
Member

@armanbilge armanbilge Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it seems there are three possible constructors.

new FormData() new FormData(form) new FormData(form, submitter)
class FormData extends ... { def this(form: HTMLFormElement) = this() def this(form: HTMLFormElement, submitter: HTMLElement) = this() }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class FormData(form: HTMLFormElement = js.native) extends ... { def this(form: HTMLFormElement) = this(form) def this(form: HTMLFormElement, submitter: HTMLElement) = this(form) }

I think this is good point !
I've looked at other api codes using this() and i make this code,
but I'm not sure if this change is correct.

I'd be grateful if someone could fix this part for sure

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've looked at other api codes using this() and i make this code,

That won't work, because there are two constructors that take a form. How wil it know which one to use?

I believe the correct change is the one I suggested in #800 (comment).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it !
I will add this part :)


/** Appends a key/value pair to the FormData object. */
def append(name: js.Any, value: js.Any, blobName: String = js.native): Unit = js.native

/** Deletes a key/value pair from the FormData object. */
def delete(name: String): Unit = js.native

/** Returns the first value associated with a given key from within a FormData object. */
def get(name: String): String | Blob = js.native

/** Returns whether a FormData object contains a certain key. */
def has(name: String): Boolean = js.native

/** Sets a new value for an existing key inside a FormData object, or adds the key/value if it does not already exist.
*/
def set(
name: String, value: String | Blob, blobName: String = js.native
): Unit = js.native

@JSName(js.Symbol.iterator)
override def jsIterator(): js.Iterator[js.Tuple2[String, String]] = js.native

/** Returns an iterator that iterates through all key/value pairs contained in the FormData. */
def entries(): js.Iterator[js.Tuple2[String, String | Blob]] = js.native

/** Returns an array of all the values associated with a given key from within a FormData. */
def getAll(name: String): js.Array[String | Blob] = js.native

/** Returns an iterator iterates through all keys of the key/value pairs contained in the FormData. */
def keys(): js.Iterator[String] = js.native

/** Returns an iterator that iterates through all values contained in the FormData. */
def values(): js.Iterator[String | Blob] = js.native
}

@js.native
Expand Down