Skip to content

Commit

Permalink
Embed FormData into ReactDOM
Browse files Browse the repository at this point in the history
  • Loading branch information
davesnx committed Nov 18, 2024
1 parent 05d9f44 commit 2190bb9
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 60 deletions.
53 changes: 0 additions & 53 deletions src/FormData.ml

This file was deleted.

42 changes: 42 additions & 0 deletions src/ReactDOM.re
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
module FormData = {
/* This file is embeded since https://github.com/melange-re/melange/pull/1153 gets merged */

type t;
type file;
type blob;
type entryValue;

[@mel.new] external make: unit => t = "FormData";
[@mel.send.pipe: t] external append: (string, string) => unit = "append";
[@mel.send.pipe: t] external delete: string => unit = "delete";
[@mel.send.pipe: t] external get: string => option(entryValue) = "get";
[@mel.send.pipe: t] external getAll: string => array(entryValue) = "getAll";
[@mel.send.pipe: t] external set: (string, string) => unit = "set";
[@mel.send.pipe: t] external has: string => bool = "has";
[@mel.send] external keys: t => Js.Iterator.t(string) = "keys";
[@mel.send] external values: t => Js.Iterator.t(entryValue) = "values";

[@mel.send.pipe: t]
external appendObject: (string, Js.t({..}), ~filename: string=?) => unit =
"append";

[@mel.send.pipe: t]
external appendBlob: (string, blob, ~filename: string=?) => unit = "append";

[@mel.send.pipe: t]
external appendFile: (string, file, ~filename: string=?) => unit = "append";

[@mel.send.pipe: t]
external setObject: (string, Js.t({..}), ~filename: string=?) => unit =
"set";

[@mel.send.pipe: t]
external setBlob: (string, blob, ~filename: string=?) => unit = "set";

[@mel.send.pipe: t]
external setFile: (string, file, ~filename: string=?) => unit = "set";

[@mel.send]
external entries: t => Js.Iterator.t((string, entryValue)) = "entries";
};

module Style = {
type t;

Expand Down
42 changes: 42 additions & 0 deletions src/ReactDOM.rei
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
module FormData: {
/* This file is embeded since https://github.com/melange-re/melange/pull/1153 gets merged */

type t;
type file;
type blob;
type entryValue;

[@mel.new] external make: unit => t = "FormData";
[@mel.send.pipe: t] external append: (string, string) => unit = "append";
[@mel.send.pipe: t] external delete: string => unit = "delete";
[@mel.send.pipe: t] external get: string => option(entryValue) = "get";
[@mel.send.pipe: t] external getAll: string => array(entryValue) = "getAll";
[@mel.send.pipe: t] external set: (string, string) => unit = "set";
[@mel.send.pipe: t] external has: string => bool = "has";
[@mel.send] external keys: t => Js.Iterator.t(string) = "keys";
[@mel.send] external values: t => Js.Iterator.t(entryValue) = "values";

[@mel.send.pipe: t]
external appendObject: (string, Js.t({..}), ~filename: string=?) => unit =
"append";

[@mel.send.pipe: t]
external appendBlob: (string, blob, ~filename: string=?) => unit = "append";

[@mel.send.pipe: t]
external appendFile: (string, file, ~filename: string=?) => unit = "append";

[@mel.send.pipe: t]
external setObject: (string, Js.t({..}), ~filename: string=?) => unit =
"set";

[@mel.send.pipe: t]
external setBlob: (string, blob, ~filename: string=?) => unit = "set";

[@mel.send.pipe: t]
external setFile: (string, file, ~filename: string=?) => unit = "set";

[@mel.send]
external entries: t => Js.Iterator.t((string, entryValue)) = "entries";
};

module Style: {
type t;

Expand Down
1 change: 0 additions & 1 deletion src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
ReactDOMTestUtils
ReactTestRenderer
ReasonReactRouter
FormData
ReasonReactErrorBoundary)
(preprocess
(pps melange.ppx reason-react-ppx))
Expand Down
12 changes: 6 additions & 6 deletions test/Form__test.re
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ module Thread = {
);

let formAction = formData => {
let formMessage = FormData.get("message", formData);
let formMessage = ReactDOM.FormData.get("message", formData);
switch (formMessage) {
| Some(entry) =>
switch (FormData.classify(entry)) {
| `String(text) =>
switch (Js.Types.classify(entry)) {
| JSString(text) =>
addOptimisticMessage(text);
switch (Js.Nullable.toOption(formRef.current)) {
| Some(form) => reset(form)
Expand Down Expand Up @@ -78,12 +78,12 @@ module App = {
React.useState(() => [{text: "¡Hola!", sending: false, key: 1}]);

let sendMessage = formData => {
let formMessage = FormData.get("message", formData);
let formMessage = ReactDOM.FormData.get("message", formData);
switch (formMessage) {
| Some(message) =>
let.await entry = deliverMessage(message);
switch (FormData.classify(entry)) {
| `String(text) =>
switch (Js.Types.classify(entry)) {
| JSString(text) =>
let _ =
setMessages(messages =>
[{text, sending: true, key: 1}, ...messages]
Expand Down

0 comments on commit 2190bb9

Please sign in to comment.