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

JSX: disabled should accept true/false instead of unit #289

Open
thangngoc89 opened this issue Jul 22, 2021 · 1 comment
Open

JSX: disabled should accept true/false instead of unit #289

thangngoc89 opened this issue Jul 22, 2021 · 1 comment

Comments

@thangngoc89
Copy link

thangngoc89 commented Jul 22, 2021

Given this code:

<button disabled={()}> "my button" </button>

To have a button that isn't disabled, I have to duplicate the code logic or use pure function syntax instead of doing it directly in JSX syntax.


Update: This is a very awkward Button component that were written to allow this:

module Button = {
  open Tyxml.Html;
  let createElement = (~title=?, ~cls=?, ~disabled=?, ~children, ()) => {
    let a = {
      let or_append = (value, cb, attrs) => {
        switch (value) {
        | None => attrs
        | Some(value) => [cb(value), ...attrs]
        };
      };
      let attrs = [];
      let attrs =
        attrs
        |> or_append(title, title => a_title(title))
        |> or_append(cls, cls => a_class(cls));
      let attrs =
        switch (disabled) {
        | Some(true) => [a_disabled(), ...attrs]
        | Some(false)
        | None => attrs
        };
      attrs;
    };
    button(~a, children);
  };
};
@Drup
Copy link
Member

Drup commented Feb 10, 2022

I agree this is quite inconvenient, but I'm not sure I have a satisfying solution.

We could turn all the unit taking attributes into boolean ones. This is a slight departure from the spec, but maybe a reasonable one.

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

No branches or pull requests

2 participants