ionited / mask-svelte

Create your masks for Svelte easily
13 stars 1 forks source link

feat: early return #2

Closed gabrielcursino closed 5 months ago

gabrielcursino commented 5 months ago
          @gabrielcursino would you consider adding the `if (!options) return; // early return if undefined` line to your wrapper?

The benefit is that I can put the use:mask={maskOptions} inside a component and use it directly from a +page.svelte. If this line is not there, it will throw an error.

TextInput.svelte :

<script>
  import { mask } from "@ionited/mask-svelte";
  export let maskOptions = undefined;
</script>

<input type="text" use:mask={maskOptions} />

+page.svelte :

<script>
  const maskOptions = { mask: "AAA 999" };
</script>

<!-- this will work -->
<TextInput {maskOptions} />

<!-- this will throw an error -->
<TextInput />

Originally posted by @lolcabanon in https://github.com/ionited/mask-svelte/issues/1#issuecomment-2045346721

gabrielcursino commented 5 months ago

@lolcabanon, i will test this!

lolcabanon commented 5 months ago

Hey @gabrielcursino !

Thanks for the follow-up.

It seems like it works as-is. See this REPL.

I will try it in my project with the new version and keep you updated! :smiley:

lolcabanon commented 5 months ago

Ok, it looks like it work at first, but then crashes when calling mask.instance.destroy().

See updated REPL. Uncomment the code in the "Without mask" section and click "Remount" to trigger a rerender.

I guess solutions are either to return early or to mask?.instance.destroy(). I prefer the early return since it avoid calling the Mask function unnecessarily.

gabrielcursino commented 5 months ago

@lolcabanon Check the latest release 0.3.0, i've added dynamic masking, this version fixes the mask destroy call as well.

You can see here the example:

https://svelte.dev/repl/5cdffa2a1da8481d894941cef0014d30?version=4.2.12

lolcabanon commented 5 months ago

Wow cool!

Should you clear the mask when parameters = null? Or trim()?

lolcabanon commented 5 months ago

Should you clear the mask when parameters = null? Or trim()?

Or I can probably just :

$: parameters === null && clearMask();
gabrielcursino commented 5 months ago

I think the input value could be cleared by you. But i can think better alternatives for future releases.

I've updated the REPL with this example:

https://svelte.dev/repl/5cdffa2a1da8481d894941cef0014d30?version=4.2.12

lolcabanon commented 5 months ago

I think the input value could be cleared by you.

Yeah I also think its safer to let the user handle the clear. It would be a risk of loosing value otherwise.

:)

gabrielcursino commented 5 months ago

I think the input value could be cleared by you.

Yeah I also think its safer to let the user handle the clear. It would be a risk of loosing value otherwise.

:)

Yeah, exactly! I agree!