Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle DomProps with both jsx and html attribute name #171

Merged
merged 3 commits into from
Oct 30, 2024

Conversation

pedrobslisboa
Copy link
Collaborator

@pedrobslisboa pedrobslisboa commented Oct 15, 2024

Description

Before, we had the HTML attribute for those React.JSX.<type> names, as the JSX isn't needed for SSR.
However, we must have this JSX attribute name for the RSC component.

How

This PR adds to the DomProps the JSX attribute name data for React.JSX.Bool and React.JSX.String
Instead of holding only the HTML attribute name, React.JSX.Bool and React.JSX.String now hold both the HTML attribute name and the JSX attribute.

  type prop =
-    | Bool of (string * bool)
+    | Bool of ((string * string) * bool)
-    | String of (string * string)
+    | String of ((string * string) * string)
     | Style of string
     | DangerouslyInnerHtml of string
     | Ref of Ref.t
     | Event of string * event

Discussion

@davesnx We talked about it to be a tuple, but could it be a record for a better read? What do you think?

  type prop_name = {
     html: string;
     jsx: string
  }

  type prop =
    | Bool of (prop_name * bool)
    | String of (prop_name * string)
    | Style of string
    | DangerouslyInnerHtml of string
    | Ref of Ref.t
    | Event of string * event

How to test

  • Run make test-watch into this branch
  • Change whatever you want related to this subject, E.g.:
    • Breaking the code:
    -  React.JSX.String (("data-user-path", "data-user-path"), "what/the/path");
    +  React.JSX.String (("data-user-path", "data-user-path-error"), "what/the/path");
  • Run make demo and see that nothing was changed

@@ -1659,7 +1659,14 @@ let camelcaseToKebabcase str =
in
str |> chars_of_string |> loop [] |> List.rev |> string_of_chars

let findByName tag jsxName =
let findByName name =
Copy link
Member

Choose a reason for hiding this comment

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

Where this is being used?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It was used on getPropByName for domProps on ReactDom. But removed due to this comment: #171 (comment)

@@ -339,8 +339,8 @@ module JSX = struct
| Inline of string

type prop =
| Bool of (string * bool)
| String of (string * string)
| Bool of ((string * string) * bool)
Copy link
Member

Choose a reason for hiding this comment

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

I would use string * string * bool instead, wdyt?

Copy link
Collaborator Author

@pedrobslisboa pedrobslisboa Oct 17, 2024

Choose a reason for hiding this comment

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

The idea was to contextualize and have a better comprehension of what a prop name is (HTML and JSX) and its value. I left this comment on the description to another approach:
#171

Copy link
Member

Choose a reason for hiding this comment

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

It's better to keep a single tuple for perf, I haven't tested and just guessing but contextualisation here doesn't make much difference

| Bool of (string * bool)
| String of (string * string)
| Bool of ((string * string) * bool)
| String of ((string * 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.

Same here, not big reason to create a new tuple

Comment on lines 253 to 256
let getPropByName name =
match DomProps.findByName name with
| Ok prop -> (name, DomProps.getJSXName prop)
| Error _ -> failwith "Invalid prop"
Copy link
Member

Choose a reason for hiding this comment

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

can you move this into domprops, and remove the failwith?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It was removed due to this comment: #171 (comment)

Comment on lines 258 to 265
let string name v = React.JSX.string (getPropByName name) v
let int name v = React.JSX.int (getPropByName name) v
let bool name v = React.JSX.bool (getPropByName name) v

let booleanish_string name v =
React.JSX.string (getPropByName name) (string_of_bool v)

let float name v = React.JSX.float (getPropByName name) v
Copy link
Member

Choose a reason for hiding this comment

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

We really don't want this, getPropByName is more expensive than the direct name, we always prefer to keep it static here

@davesnx davesnx merged commit 904d2ca into ml-in-barcelona:main Oct 30, 2024
2 checks passed
davesnx added a commit that referenced this pull request Oct 30, 2024
… into rsc

* 'main' of github.com:ml-in-barcelona/server-reason-react:
  Handle DomProps with both jsx and html attribute name (#171)
  Change setSearch to handle SearchParams.t (#176)
  doc: add @@platform attribute documentation (#175)
  Support non-ascii on JS.String.toLowerCase and toUpperCase (#173)
  Fix snapshot test for external
  Upperbound reason to 3.10
  feat: add nested mel.obj support (#169)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants