Skip to content

input type="number" bindings can be null and undefined #8095

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

Open
probablykasper opened this issue Dec 8, 2022 · 7 comments
Open

input type="number" bindings can be null and undefined #8095

probablykasper opened this issue Dec 8, 2022 · 7 comments

Comments

@probablykasper
Copy link

probablykasper commented Dec 8, 2022

Describe the bug

The documentation says:

If the input is empty or invalid (in the case of type="number"), the value is undefined.

When the value is initialized, it is indeed undefined. However, when later emptying the input, it becomes null. In previous versions it did become undefined - #1701 seems to be what changed it in order to fix a warning.

Reproduction

https://svelte.dev/repl/58ce9182263245d0b646d115d7e5e1e7?version=3.54.0

Logs

No response

System Info

System:
    OS: macOS 10.15.7
    CPU: (8) x64 Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
    Memory: 1.92 GB / 32.00 GB
    Shell: 3.5.1 - /usr/local/bin/fish
  Binaries:
    npm: 8.19.2 - ~/Library/Application Support/Volta/tools/image/node/18.12.1/bin/npm
  Browsers:
    Brave Browser: 107.1.45.131
    Chrome: 108.0.5359.94
    Firefox: 107.0
    Firefox Developer Edition: 108.0
    Safari: 15.6.1
  npmPackages:
    svelte: ^3.54.0 => 3.54.0

Severity

annoyance

@phamduylong
Copy link

I've tried with 3 different browsers (Chrome, Edge and Firefox), all results in undefined rather than null

@probablykasper
Copy link
Author

It's undefined initially, but when emptying the input, it becomes null. Updated the REPL to make that clearer

@probablykasper probablykasper changed the title <input type="number" value is null, not undefined input type="number" bindings can be null and undefined Feb 5, 2023
@brunnerh
Copy link
Member

To me this feels more like a documentation issue - validity does not change the bound value either (unless the docs talk about some kind of validity I am unaware of).

If you want a more consistent value, initialize the state to null.
Having it initially as undefined can otherwise be used to tell whether the input has been changed yet since it will either be a number or null after interaction.

@probablykasper
Copy link
Author

The docs were indeed updated to null (#9941).

Is it standard behaviour that undefined can be bound to a value that cannot be undefined?

@brunnerh
Copy link
Member

undefined when passed in for the most part means "ignore".
E.g. with fallback values, the fallback will apply.

<script>
	import Component from './Component.svelte';
	let value = undefined;
</script>

<Component {value} />
<!-- Component.svelte -->
<script>
	export let value = 1;
</script>

Value: {value}

REPL

There is a change in Svelte 5 that when a binding is used, undefined is no longer allowed when a fallback exists (REPL).

De facto the same should be true for input elements, they always have a fallback value.
For type="number" that would be null, for text-based inputs it's ''.

@probablykasper
Copy link
Author

Yep, that makes sense. The input's value is null while the bound variable is undefined, aka the binding isn't working.

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

3 participants