Svelte: Input modifiers

Created on 16 Nov 2019  路  7Comments  路  Source: sveltejs/svelte

https://github.com/sveltejs/svelte/pull/3527#issuecomment-534531584

Is your feature request related to a problem? Please describe.
Date inputs have string bindings, which are rarely helpful.

Describe the solution you'd like
It should be possible to specify the type of binding:

<input type="date" bind:value|date={date_as_date}>
<input type="date" bind:value|number={date_as_number}>
<input type="date" bind:value|string={date_as_string}>

In v4, we can switch the default over to the value|date behaviour.

Describe alternatives you've considered
Adding valueAsNumber and valueAsDate bindings. There are a couple of reasons not to do this:

  • It would introduce an inconsistency with how number and range inputs are currently handled
  • Resolving that inconsistency would mean expecting people to type valueAsNumber instead of just value. It's unergonomic
  • Having multiple binding options makes it easy for someone to use them all simultaneously, which could lead to subtle bugs from not having a single source of truth

How important is this feature to you?
Medium

enhancement

Most helpful comment

Might it be nice to add |number and |string to type='number' as well?

All 7 comments

Might it be nice to add |number and |string to type='number' as well?

Would be good for <select> which doesn't have a type attribute.

I still would prefer bind : valueAsDate/valueAsNumber

I also like bind:value|date syntax, as I think it's a lot cleaner than the messy API exposed by browsers.

Is there a workaround for now?

@cupcakearmy

<input type="date" bind:value={date}>

$: dateAsDate= date && new Date(...date.split('-'))

Rendered

@antony Thanks for the answer, but I needed 2 way binding.
Got it working though :)
https://svelte.dev/repl/dc963bbead384b69aad17824149d6d27?version=3.25.1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidcallanan picture davidcallanan  路  3Comments

AntoninBeaufort picture AntoninBeaufort  路  3Comments

bestguy picture bestguy  路  3Comments

plumpNation picture plumpNation  路  3Comments

thoughtspile picture thoughtspile  路  3Comments