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

[Bug Report] @click:clear in a v-autocomplete cleareable sets the value to undefined always #5722

Closed
empresarrollo opened this issue Nov 23, 2018 · 13 comments
Assignees
Labels
C: VAutocomplete invalid The issue is missing information or is not a valid bug/feature request
Milestone

Comments

@empresarrollo
Copy link

Versions and Environment

Vuetify: 1.3.9
Vue: 2.5.17
Browsers: Chrome 70.0.3538.102
OS: Windows 8.1

Steps to reproduce

Select an option in the autocomplete, then click the clear button

Expected Behavior

you should see that selectedID is null

Actual Behavior

selectedID is undefined

Reproduction Link

https://codepen.io/anon/pen/mQLdJZ

Other comments

It works OK if you use :clear-icon-cb="()=>selectedID=null" but in console you say is deprecated

@jacekkarczmarczyk
Copy link
Member

jacekkarczmarczyk commented Nov 23, 2018

The difference is that @click:clear is emitted before clearing the value and clear-icon-cb was called after. Event works if you use $nextTick: https://codepen.io/anon/pen/LXmpPJ

See also #4144 and #2752

@gileneusz
Copy link

I've got the same issue 😐

@tafel
Copy link

tafel commented Nov 26, 2018

+1 it should be great to set it to null, or having the choice, maybe with a new event modifier, like v-model="value" @click:clear.nexttick="value = null"

@gileneusz
Copy link

gileneusz commented Nov 26, 2018

I would rather set it to empty object, maybe just adding proper information to docs as a commentary of clearable prop could be enough for now

edit: this of course depend of what you are returning, if there is return-object prop, it should be like:
@click:clear.nexttick="yourObject = {}"

working with chips also gives null values: https://codepen.io/anon/pen/KrByxg?editors=1011

@romucci
Copy link

romucci commented Apr 12, 2019

Any updates on this? Would be really awesome for it to be set to null rather than undefined.

@arcreative
Copy link

Would definitely be nice... for now I'm using a computed property with a custom setter since this is a common component for us:

  computed: {
    // Needed since v-autocomplete sets value to undefined instead of null, which results in it being ignored in serialization
    value: {
      get() {
        return this.model[this.property];
      },
      set(val) {
        this.model[this.property] = val || null;
      }
    }
  },

...but would be nice if our team didn't need to be apprised of this whoopsie. Our API/persistence library ignores undefined values (but not null values) when sending updates.

@KaelWD KaelWD modified the milestones: v2.0.x, v2.1.x Oct 2, 2019
@johnleider johnleider modified the milestones: v2.1.x, v2.2.x Oct 11, 2019
@Webifi
Copy link
Contributor

Webifi commented Jan 17, 2020

Just ran into this.

undefined values are not encoded with JSON.stringify, so the intent to clear the value will not be sent to remote APIs.

How about a "clear-value" prop that allows us to set what it is used when cleared? Or just change it to null rather than undefined? Why undefined as the default behavior here?

@johnleider
Copy link
Member

@Webifi this is the intended functionality per #4649 and #4431. null is a valid value whereas undefined is the absence of a reference to a value.

@johnleider
Copy link
Member

This is intended functionality as described here. The recommended way to handle this at the moment is described above, waiting until the next tick.

If you have any additional questions, please reach out to us in our Discord community.

@johnleider johnleider added invalid The issue is missing information or is not a valid bug/feature request and removed T: bug Functionality that does not work as intended/expected T: regression Something that used to work but we broke labels Jan 20, 2020
@johnleider johnleider self-assigned this Jan 20, 2020
@arcreative
Copy link

arcreative commented Jan 20, 2020

@johnleider I'm not sure those issues are related from my perspective. When you clear a value, I think there’s a pretty big difference between setting it to null vs. removing it from existence. I would argue that setting it to null makes more sense in most cases, and a value of undefined is the edge case. In our app, for instance, having an association set to something that was persisted in the server, and then clearing it means that we can’t now PATCH that back to the API because it’s as if the attribute never existed, rather than existed and was set to null. Am I the odd one out? Seems from some of the comments here that I'm not!!

@Webifi
Copy link
Contributor

Webifi commented Jan 21, 2020

I completly agree with @arcreative.

"null" is a proper state. Undefined is not. In JavaScript, undefined will essentially remove the referenced property. I'd argue that undefined is not the correct state for the vast majority of use cases.

There's no reason not to allow null values to be selected and also allow "clear" to default to null, but have the edge case where people want a clearable select list for a list containing a null to be forced to use the $nextTick(()=>object.value=undefined) hack to set to undefined.

I will strongly argue that setting a value to null on clear is NOT an edge case.

@garaboncias

This comment has been minimized.

@KaelWD
Copy link
Member

KaelWD commented Nov 9, 2020

Duplicate of #7429

@KaelWD KaelWD marked this as a duplicate of #7429 Nov 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: VAutocomplete invalid The issue is missing information or is not a valid bug/feature request
Projects
None yet
Development

No branches or pull requests