V Switch Case and Default added to core #482
Replies: 3 comments 2 replies
-
The detailed design is based on Limiting values to |
Beta Was this translation helpful? Give feedback.
-
How would you go about debugging this? also how would it work with typescript? |
Beta Was this translation helpful? Give feedback.
-
Start Date: 2023/1/23
Summary
This is a set of directives that allow the user to evaluate a condition based on whether or not it is a string number or array. The first one is called
v-switch
it takes away child nodes based on the value passed to it. The second one is calledv-case
is a directive that allows the user to send values forv-switch
to find. The last one isv-default
. It's the one that will render a DOM node ifv-switch
finds none from the values from v-case.Here is the link to a prototype that I created to make life easier https://stackblitz.com/edit/vitejs-vite-b6zaq8. It's in the
App.vue
file.Basic example
If the proposal involves a new or changed API, include a basic code example.
Omit this section if it's not applicable.
Motivation
Why are we doing this?
Having to use if statements for situations where you just want to know if a value is a certain value is tedious? When you have to use if statements to find out whether or not a value is in certain set of values you have to write
\===
all the time. Giving developers a strict and type safe way of evaluating strings will save time and reduce verbosity.What use cases does it support?
It support's cases where you need to find out if a string is of a certain value or a variable is a number. There are times where we use certain keywords to decide whether or not to render UI.
For instance lets say that we are loading information from a Promise. The state could be pending, resolved , rejected. The code to represent rendering UI based on this would be like this.
What is the expected outcome?
The community will be able to write code where a UI is rendered based on a small set of values in a simple and a reusable way.
Detailed design
This proposal involves three compile-time directives that are used for rendering.
v-switch
v-case
andv-default
Thev-switch
directive takes in a string or a number then looks at all of it's rendered children. If one of them contains a value that was sent to the switch directive the switch directive will preserve that element if not it will destroy it. Thev-case
directives are responsible for sending values forv-switch
to use the way this happens is the v-case statement sets a propertySymbol('v-case')
inside of the element's dataset as a key with the value being the one that was passed to the directive. Thev-switch
directive find the value for each of them inside of the dataset. Whatv-default
does is put aSymbol('v-default')
as a key inside of it's elements dataset with the value of true.V-switch
V switch does the following things
V-case
V case does the following things
V-default
V default does the following things
How it works with typescript
I don't know how the Volar works but volar does support the use of conditional's that emulate typescript features. The way v-switch would work is the same way that switch works in typescript. It would have the same features except in the template instead of in the DOM. If a union type is put inside of the switch statement the user will be forced to put it each value in the union in the case statements and write as many cases as there are in the union types.
This switch directive will work like this
Drawbacks
A breaking change is considered to because to a change that changes software regardless of whether or not something is removed or not? But developers will have to learn another alternative to evaluating conditions. Teachers of Vue will have to add new syntax to their curriculum.
Alternatives
What is the impact of not doing this?
People will have to write verbose if statements in order to evaluate values with a certain set of constraints.
Adoption strategy
This change is simply additive it will not force any changes to the ecosystem.
Unresolved questions
How will types be inferred I don't how how that would work?
Can other Data Structures be supported as well?
Beta Was this translation helpful? Give feedback.
All reactions