Skip to content
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

Allow for passing null / undefined into a typed, none required prop #2411

Closed
rmp135 opened this issue Feb 28, 2016 · 6 comments
Closed

Allow for passing null / undefined into a typed, none required prop #2411

rmp135 opened this issue Feb 28, 2016 · 6 comments

Comments

@rmp135
Copy link

rmp135 commented Feb 28, 2016

If a prop is declared with type validation but required:false, the validation will fail when passing either null or undefined as the value.

I think that the type validation should only occur on none required props if an actual value (not null or undefined) has been passed. I believe this is similar to issue #1070.

Here's a JSBin demonstrating this.

@rmp135
Copy link
Author

rmp135 commented Mar 5, 2016

Shouldn't the prop check for a default value first? It would keep it more in line with what happens if you omit the prop completely.

Here's another JSBin showing the different ways to pass a non value prop through. I'm thinking it should fall back to the default value in all cases.

@yyx990803
Copy link
Member

I have thought about this before, and didn't implement it because user may explicitly want to pass down undefined - but now I think it is appropriate to map it to the semantics of JS function calls so that default value would be used if the passed down prop is undefined.

@yyx990803
Copy link
Member

See 04116fc

@rmp135
Copy link
Author

rmp135 commented Mar 5, 2016

Thanks. I was going to suggest a nullable prop option for allowing nulls to be passed down in the case of none required props. In this case, the default would only apply if the prop was completely absent.

@lopugit
Copy link

lopugit commented Oct 10, 2017

@yyx990803 I'm a bit confused by the commit, it says

`if (raw === null) {
// initialize absent prop

  •    initProp(vm, prop, getDefault(vm, options))
    
  •    initProp(vm, prop, undefined)`
    

but I can't see any code for handling undefined, and the commit actually says that if it is null, then it's set to default.

when is undefined passed as a prop handled?

@lopugit
Copy link

lopugit commented Oct 10, 2017

// check default value
if (value === undefined) {
value = getPropDefaultValue(vm, prop, key);
// since the default value is a fresh copy,
// make sure to observe it.
var prevShouldConvert = observerState.shouldConvert;
observerState.shouldConvert = true;
observe(value);
observerState.shouldConvert = prevShouldConvert;
}
{
assertProp(prop, key, value, vm, absent);
}

I found this bit in vue.js v2 dist, is it around this part that defaults are now handled?

I can't find a comment of

Build a function that applies props to a vm.

anywhere in the current v2 vue.js build

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