Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

How to use HtmlInputElement (focus, select) ? #205

Open
@Qziem

Description

@Qziem

I want to do simple thing. Just focus and select input field after click button. I have code like this:

[@react.component]
let make = () => {
  let inputRef = React.useRef(Js.Nullable.null);

  let handleSelect = _event => {
    let element =
      inputRef.current->Js.toOption->Belt.Option.flatMap(Webapi.Dom.HtmlInputElement.ofElement);

    switch (element) {
    | Some(input) =>
      Webapi.Dom.HtmlInputElement.focus(input);
      Webapi.Dom.HtmlInputElement.select(input);
    | None => ()
    };
  };

  <>
    <input
      className="new-leon-input"
      type_="text"
      value="abcd"
      onChange={_ => ()}
      ref={ReactDOMRe.Ref.domRef(inputRef)}
    />
    <button onClick=handleSelect> "select"->React.string </button>
  </>;
};

Compiler return a bug:

  We've found a bug for you!
  src/components/TestComponent.re 11:41-45
  
   9 ┆ switch (element) {
  10 ┆ | Some(input) =>
  11 ┆   Webapi.Dom.HtmlInputElement.focus(input);
  12 ┆   Webapi.Dom.HtmlInputElement.select(input);
  13 ┆ | None => ()
  
  This has type:
    Dom.htmlElement (defined as
      Dom.eventTarget_like(Dom._node(Dom._element(Dom._htmlElement(Dom._baseClass)))))
  But somewhere wanted:
    Webapi.Dom.HtmlInputElement.t_htmlElement (defined as
      Dom.eventTarget_like(Dom._node(Dom._element(Dom._htmlElement(Dom._htmlInputElement)))))
  
  The incompatible parts:
    Dom._baseClass
    vs
    Dom._htmlInputElement

What is wrong in this code ? This code was working with bs-webapi 0.15.9, with newest: 0.19.1 i have an error above. Maybe there is a bug in library ?
I use bs-platform: 7.3.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions