scala-js / scala-js-dom

Statically typed DOM API for Scala.js
Other
315 stars 160 forks source link

It looks we are missing some methods from the FormData class #798

Closed G-yhlee closed 1 year ago

G-yhlee commented 1 year ago

In js world ,

FormData Object have append and get method,

// example ) https://developer.mozilla.org/en-US/docs/Web/API/FormData/get
const formData = new FormData(event.target);
formData.append("username", "Chris");
formData.append("username", "Bob");
formData.get("username"); // Returns "Chris"

but scalajs FormData class has only append method, we need to add get method like this...

// current  
formData.asInstanceOf[js.Dynamic].get("username").asInstanceOf[String]

// todo :  add get method
formData.get("username")

ref. https://github.com/scala-js/scala-js-dom/blob/d157768ac7def180e0ba00fd5e9bdb20920e159a/dom/src/main/scala/org/scalajs/dom/FormData.scala#L18

faveoled commented 1 year ago

Here's the variant generated by AI (untested):

@js.native
@JSGlobal
class FormData(form: HTMLFormElement = js.native) extends js.Object {

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

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

  /** Returns an iterator that iterates through all key/value pairs contained in the FormData. */
  def entries(): Iterator[js.Tuple2[String, String | Blob]] = 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 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 whether a FormData object contains a certain key. */
  def has(name: String): Boolean = js.native

  /** Returns an iterator iterates through all keys of the key/value pairs contained in the FormData. */
  def keys(): Iterator[String] = 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

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

This is updated and merged at https://github.com/scala-js/scala-js-dom/pull/800

Thanks all !