Add onChange handler to input in svelte

只愿长相守 提交于 2020-08-10 19:26:46

问题


Im trying to add the asYouType function from libphonenumber-js to my svelte input using an onChange handler as I'm not sure how this can be done with 2 way binding.

I have managed to implement this but it will only format the number onBlur instead of as the user types into the input as is expeccted behaviour as seen here libphonenumber-js

How can I change my input so that it will format as the user types into it?

<script>
  import { AsYouType } from "libphonenumber-js";

  export let value = "";
  export let label = "";

  let isFocused = false;
  let isTooltipVisible = false;

  const onBlur = () => {
    isFocused = false;
    isTooltipVisible = false;
  };

  const handleChange = val => {
    if (localeKey === "dfc.removal_form.phone") {
      const asYouType = new AsYouType("US");
      value = new AsYouType("US").input(val);
    }
  };
</script>

<div class="input-container input__row {isFullWidth ? 'isFullWidth' : ''}">

  <label>{label}</label>

  <input
    id={label}
    {name}
    bind:value
    on:change={e => handleChange(e.target.value)}
    on:focus={() => {
      isFocused = true;
      isTooltipVisible = true;
    }}
    on:blur={onBlur}
    on:input={() => {
      isTooltipVisible = false;
    }}
    data-ipr-selector={dataIprSelector} />
</div>



回答1:


Handle the input event, not the change event: https://svelte.dev/repl/bf21b14cb29e41f28efc802b56e7f152?version=3.23.1



来源:https://stackoverflow.com/questions/62278480/add-onchange-handler-to-input-in-svelte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!