Syntax Video
Modern Vue.js Crash Course | with TypeScript + script setup + Composition API
- 0:00 intro
- 1:47 create vue app with vite
- 4:18 vue templates are HTML with magic
- 4:33 component styles can be scoped
- 5:16 component script / state
- 6:14 vue dev tools
- 7:34 form input state
- 9:15 dom events
- 10:26 defining components
- 12:00 emitting events from child components
- 13:08 listening for events in parent component
- 14:01 reactive state with arrays
- 15:57 form reset / validation
- 19:18 list rendering
- 20:25 passing props to components
- 22:48 conditional rendering
- 24:05 updating state in child
- 24:53 modifying props is an anti-pattern
- 25:12 emit events instead of changing props
- 26:53 css class binding
- 27:59 computed state
- 30:02 remove elements from an array
- 32:11 vue transition group
- 33:58 filtering array by type
- 41:23 thanks!
intro
What's up? CJ here. In this video, we're going to talk all about Vue.js. So, next week, I'm going to be releasing a video here on the channel that shows you how to build a full stack app from scratch using Nux. So, Nux is known as the progressive web framework and and it's actually built on top of Vue.js. So, if you're familiar with Nex.js, it's built on top of React. Nux is built on top of Vue. And so that video that comes out next week is going to show you how to do all the things inside of Nux and showcase all of its features like file-based routing and data loading and API routes and all that other all those other goodies that we like in a meta framework. But today I want to focus on Vue.js itself because Vue is the front-end framework that's used with Nux. And in this video I want to showcase all of its features and show you how to build front-end applications with Vue. And for that we have this application here that allows you to manage tasks. So for instance, I can add a task. It animates in. We can see the total number of tasks that we have. We can add more tasks and we can mark tasks
as done. So if I mark this as done, you'll notice it gets a little line through it. Also the total countd done updates here. And we also have filtering. So I can filter to show only the items that are still to-do. Show only the items that are done. I can also remove all of the items. And then we have conditional rendering. So that only displays if there are items. If there aren't items, this displays. We also show things like state management. So when you're typing in the form here, we're actually keeping track of that state. I'll show you how to break things down into components because this form and the list are actually separate components. So we need to share data and emit events through them. So this one application shows many of the things that you would want to do in front-end development and shows how Vue.js makes that really easy. So let's dive in.
[Music] So to get started, we'll create a Vue app using Vit. So I'll do PNPM create VIT at latest and then we'll give it a name. I'm going to call this the tasks app. And we'll select Vue as the framework. And then we're going to use the TypeScript template. But before we jump into that, I do want to show you if you're going to be building a more full-fledged Vue app, you should use the official Vue starter. And if you choose this, you'll actually see one of the cool things about the Vue ecosystem. So when you're generating a Vue app, you can add JSX support. You can also automatically set up a router. And in the Vue ecosystem, there is just the Vue router. There's not a bunch of choices and people don't argue about what to use. Everyone uses Vue router. Same thing for global state management. There aren't a bunch of choices. If you need global state management in your view app, you use Pina. And then you can also set up testing and linting and formatting with their CLI as well. So if you are going to build a more full-fledged view app and you're not going to use NX, definitely use this CLI. And like I said, so much of it is just built in. It's one of the things I really like about the Vue.js ecosystem.
create vue app with vite
But like I said, we're just going to choose TypeScript. We're going to keep it simple with just Vit. We'll head into that directory and then install dependencies. We'll then open this up with VS Code. Now, here in our Vit generated app, we have the standard setup. We've got a Vit config that has the Vue plugin and then we have our entry point index.html and then we have this element with ID app. This is where our entire Vue.js application will live.
Now, while I'm in here, I'm going to go ahead and update the title to be view tasks app. And then I want to add some default styling. And for that we're going to use Pico CSS which is a mostly classless CSS framework. Essentially you just add it to your page and everything gets styles by default. So I like to use this for simple prototype simple demos like this because then I don't have to worry around too much with styling. And I'm just going to add that in the head here. If we head into the source directory, we can see that we have our main entry point. And this imports our main app component and then mounts it on that element with ID app. The app also comes with some default styles. I'm just going to get rid of all of these. And then if we take a look at app.view, we can see we have our single file component. We have all of our script up here, our template, which is going to be rendered out, and then some styles, which are scoped to this component. To start out, we're going to keep this super simple. I'm going to get rid of everything except for just an H1 right here that says hello world. And then we're also going to get rid of these styles. So, our app is literally just an H1 that says hello world. And then from here, I'm going to get rid of this
default hello world component. And we're also going to get rid of this view.s SVG. Now, with this, let's go ahead and start up the app, and we should see a hello world. So, we'll do pmppm rundev. Open this up in the browser. Look at that. We've got a hello world, and it has a nice default dark mode because we're using pico CSS. Now, one of the things I love about Vue is your template is just HTML with some magic things sprinkled in for interactivity. But for now, let's actually create a main element and then move that H1 inside of it. And then we'll actually add some styles here. So, I'm going to add that style tag. And what's also nice about Vue, it has the ability to scope styles specifically to this component. So if you add the word scoped to this style tag, all of the styles we define here will only work for this component. They will not leak out. So for instance, I could target the main element here. And if there's a main element in any other component, the styles that I set here will not apply. They will not leak out.
vue templates are HTML with magic
component styles can be scoped
These are only going to target this specific component. And for this, let's just set this up as a container. So let's give it a max width. We'll do max width of 800 pixels. And we'll also give it some margin. We'll give it one rim on the top and bottom and then automatic margin on the sides so it centers the element. So if we save that, head back to the browser. Look at that. I've got myself a little container here. Now, if we want to set up some state, let's add a script tag. So we'll add a script.
component script / state
We'll set lang equals ts because we're using TypeScript. And we're going to use this setup keyword. So this is a new thing that was introduced in Vue3, but it essentially makes it so there's a whole lot less boilerplate when we're defining all of the things for our component. And just as a simple example, let's extract out this text here into a state variable. So I'll create a message variable here. And we're going to use ref. So ref gets imported from Vue. And this is how we define reactive state values inside of Vue. Now, if you're familiar with React, it's not exactly like a ref inside of React. You could think of ref more like use state, but essentially this is a state value that can be updated later on. And if I want to use this inside of my template, we use handlebars. So double curly braces.
And then inside of the double curly braces, we can write JavaScript expressions, but we can also reference our variables just like that. So this is a pretty contrived example because it's literally a state variable that doesn't update. But now if we head back to the browser, it's displaying that value there. Now, if you're working with Vue.js, you definitely want to install the Vue dev tools. And we'll take a look at that here. It shows us our entire component hierarchy and if we click on a component we can see all of the state and we can update that state. So it will say wow and it will update the page. And so I manually edited this state variable here in the dev tools but I could actually write JavaScript code that changes that variable and the page would automatically update. So that's the basics of our app component here. This is known as an SFC or single file component because this script, this template and these styles are all wrapped up into this one app component.
vue dev tools
And all of the components we define will have their own template style and script. So we're making a tasks app. Let's update our message here to be tasks app. And then I want a form where the user can input a new task. So I'm going to define a form here. We'll have a label that says new task. And then we'll have an input here. And then we'll give this a name of new task. Now this is another thing I like about Vue. It uses the standard. So an input tag in HTML is a self-closing tag. does not need that slash on there that you've gotten really used to adding if you've ever worked in React. So that's one of the other cool things about Vue is you don't need that because it's just HTML.
So just like this, we should have a nice little form. Great. And I want to be able to type into this form add a new task like learn view and then click submit and add it. So to keep track of the value of this input, let's create another state variable. We're going to call this new task and it's going to start off as the empty string. Now, if I want to bind this input to that state variable, there's a built-in directive in Vue called Vodel. We can pass in a ref here. And now, whenever we type into this input, it will actually update that state variable behind the scenes. Now, you might think of this as two-way data binding, but this is just syntactic sugar, right? This is literally just adding a change handler and then updating the state variable. But we do this so often, we don't want to have to do that manually every time. So, we can just use vodel. To see this in action, let's actually add just an element down here that references the current value of new task. And now as I start typing, you'll see the current value is displayed here. And then let's refresh our dev tools as well. So we'll actually see that state value down here. So as I start typing, you can see it's updated
form input state
in the dev tools and we see the latest value here on the page. So this is how great view is. I just love how little boiler playthrough is. Now there are some things to get used to, right? You have to learn about these directives. You have to get used to using handlebars. But once you learn about all the built-in things, and there's not a lot of them, once you learn them, your apps are so much simpler in my opinion than React components. Okay? So, we don't want to render that new task out there anymore. We actually want to have a button that we can click and then add that new task. So, inside this form, I'm going to add a button. This is just going to say add. And then I'm actually going to wrap this in a container and give it a class of button container. And then I'm just going to make this a flexbox and push that button to the right. So, we're going to style button container here. We'll give it a display of flex and we'll do justify content end. And that should push that button to the right. Just like that. Sweet. Now, we want to listen for when this form is submitted. And in view, if you want to add a DOM event to an element, you can use the at sign. And you can see here we
dom events
have access to all these DOM events. The one that we care about is submit. And in this case, we want to call a function. So, in my script, I'm going to define a function called form submitted. And then I can just reference that right here. So when the submit event is fired, it's going to call this function. And then inside of here to see this in action, we'll actually just log out new task. So earlier we created this state variable. If we want to reference the current value, we do have to say value because this is essentially an object wrapper around a primitive value, right?
A string is a primitive. It creates that object wrapper. And if we want to get the current value, we do have to say value. Now, one thing we typically always want to do when we're handling forms with JavaScript is prevent the default action, and view has a built-in helper for that. So, right here on submit, I can just press dot. You can see all of these other helpers, but the one that I want is prevent. And so, now this will automatically prevent the default action when calling that function. And so, just like that, if we take a look at the console, whenever I submit a task, that function runs and we log out the current value. Now, from here, I would want to put this into a list because we are creating a tasks app. But before we get any further, let's actually define a component that has all of this functionality instead of defining it here at the app level. So in my components directory, I'm going to create a new file called task form.view.
defining components
And we'll add our template. And then here in my app component, I'm just going to rip out that form, throw it in here, and then we'll go grab the state variable and function that we need from the script. And I'll also note, as you can see, I'm getting some errors here because it doesn't know what these are. So, if you have the Vue extension installed in VS Code, you're going to get really good type completion and errors even inside of your Vue components here. So, this looks good.
And then back in my app, I can rip that out of here because we only need that message state variable. And so, now I want to render that task form right here inside of my app element. So, we'll just import it in like any other JavaScript module. So, I'll say import task form from components task form. And then if I'd like to reference it, I can just create an instance of it right there.
And now our app should behave in exactly the same way except that button container class is defined in the parent component. And this is actually demonstrating scoped styles, right? We defined this button container class here, but inside the task form, it's not defined. And we put scoped on it. So this class actually only exists inside the app component. Now I put scoped on here just to demonstrate it. I'm okay with these being global styles. So if I remove this, that makes these global styles. And now that button container class will be accessible inside of the form component. So separating out functionality into new components is as simple as that. Just create a new single file component file, add all of your functionality, and then import it and render it out. But now that my app architecture is a little more complex, we need this task form to communicate with the parent component. So I need to pass some data up when this form is submitted. Now there are two ways to do this. You can define a prop and say this function accepts a prop that we could then invoke whenever the form is submitted. But the preferred way of
emitting events from child components
doing this in the world of Vue is to emit events. So we can do this. We can say emit equals define emits. Define emits is a compiler macro built into Vue. And inside of here we can pass in a TypeScript type to define our types of emits. And in this case we want to emit an event called add task. And it is going to emit a new task which is of type string. So we have declared that this component will emit the add task event. And then here instead of just logging it out to the console. Let's emit it and then we can listen for that emit in the parrot component. So right here I can say emit. We get some nice type completion as to what emits are available to us. And then we can specifically pass in new task. Because we're going to pass in what was entered here. And now we'll actually listen for this emit in the parent. So here on app.view, we can use the at sign to tap into that event. And look at that. We have some nice type completion that says, "Hey, would you like to tap into this add task event?"
listening for events in parent component
Why, yes, I would. And I'm going to define a method here in this component called add task. It's going to take in that new task, which is a string. And then now we'll log it out here. So we're essentially just juggling the data around. But essentially what we've done is we say if this task form emits the add task event, I want you to call this function and then we can actually use that data. So if we've done everything correct now when I attempt to add a new task that fires off the event the parent component was listening for it and now it has access to that data. So this is one way to communicate between components in a parent child relationship. Like I said you could also do it with props but I'll show you how we can use props in a different component. For now let's start keeping track of these tasks. So here in the parent component I'm going to define a tasks list. This is going to be a ref that starts off as an empty array. Now, I do want to type this. So, I'm actually going to create just a new file here called types. And we're going to define
reactive state with arrays
an interface for our task. It will have an ID, which is a string, a title, which is a string, and then we also want a done property, which is a boolean, like so. Now, in our app.view, we want to say that this ref should be an array of tasks. So, we can just use a TypeScript generic here to pass in our task type.
And then, I'll just import that in. And so, now task is appropriately typed. And we can actually store tasks inside of there. So whenever we go to add a task, we want to put it into this list here. And one of the nice things about reactive variables in Vue is you can just modify them. You don't have to worry about any sort of immutability. So right now you can see tasks.value is the actual list. We can literally just call push on this and then push in a brand new object. And behind the scenes, Vue is smart enough to know that this reactive variable has changed and it will update any place that references it. Uh, but in this case, we need to give this an ID. I'm just going to use crypto.random UYU ID. And then we'll give it a title. Its title is that new task value that we passed in. And then done will default to false. So just like that, whenever this function is called, we're pushing a value into the tasks array. To see that it actually happened, let's actually display how many tasks are in the list. So right below here, let's just add a message that says there are. And then we want to list the number
of tasks. So we'll say tasks.length tasks. So when the page loads there are zero tasks. But if I add a new one like learn view there is one task. If I add another one like learn there are two tasks. So it's keeping track of that behind the scenes. If we take a look in the view dev tools we can see it's an array. It has both of those values inside of it. Now you might have noticed that we're not actually clearing out the input here. We can do that inside of the task form. So right here after emitting a value, we can just reset the value to the empty string. So we'll emit and then we'll set that back to the empty string. And now the form will be cleared out. Another thing we could do is prevent ourselves from emitting if the form is empty because right now technically I can just press enter and then it just adds a bunch of empty tasks. But let's prevent users from doing that. So we'll say if new task value is actually a thing then you should emit it and then reset. And on top of that, we could prevent someone from putting a bunch of spaces and then
form reset / validation
hitting enter. So we could trim this. And then also the value that we pass up, we could trim as well. So that will prevent someone from just putting a bunch of spaces in here and then clicking add. That won't work because when we trim, then it becomes the empty string and it's a falsy value. Now, while we're here, let's go ahead and add an error message. So if someone tries submitting an empty task, let's display an error message here. So in pico CSS, we can add an element with ID invalid helper. And this will actually show up as kind of like an error message right below our input. So right about here, we'll add our invalid helper. And in this case, we actually will keep track of an error value and display it here.
So I want a new state variable which will be an error. It'll start off as the empty string. But if they try submitting a task that is completely empty, then we'll set the error value. So here I can say task cannot be empty like so. And now if I attempt to submit an empty task, that error message pops up. Now we should only render this if there actually is an error. And this is where conditional rendering comes into play.
So built into view is a v if directive. So if error has a value then render out this invalid helper. Another thing we can do is add the validation state to the input itself. So you can see in pico CSS if we set Arya invalid to true that will actually render it out with like a red border. So let's add this prop as well. So on the input we'll say Arya invalid should be true if error is true.
And if error does not have a value then it should just be undefined. I don't want it to appear valid even if it's empty. Now Arya invalid is expecting a boolean. So we can actually just do a a not knot here. This is just JavaScript to turn that string into a boolean. And now you'll see it turns the input red. So if if I do a refresh of the page and I try adding an empty task, you can see that it turns the element red, shows this error here. So that's awesome. But one thing we should do is once they start typing again, we should clear out that error. So in this case, I will add a change event handler and then just clear the error anytime the input value changes. Just going to format the input here so it's a little bit easier to read. But now we can tap into that input event. And anytime there is input, we just want to set error to nothing. And so now if I try submitting nothing, we see the error, but the moment I start typing, it resets error to nothing and removes that error state. But then I can add that and then it clears it out. So we just saw a whirlwind tour of conditional rendering. We have an input
listener. We're also binding to other attributes. I kind of glossed over it, but anytime you want an attribute to have an expression inside of it, like it needs to reference state variables, you use the colon because if you remove the colon, then it's just a string. And you can see my syntax highlighting helps out with that. But if you use a colon, it will actually evaluate this expression.
list rendering
Okay, so this is looking good. Now, let's actually display the tasks down below. So in our app component, we'll get rid of this status message for now. And then we just want to iterate over these tasks and display a card for each one. So right here, I'm going to use the article element inside of Pico CSS. An article will appear as a card. And I want to repeat this for every element in the tasks array. Inside of view, there's a built-in directive called V4. So I can say V4 task in tasks. So for every task this article is going to render out and I want to just right now display the title. So I'll say task.title. And just like that, now as I add things to the page, they appear. Oh, now Vue.js actually is using a virtual DOM under the hood. So we do want to add a key just like you would inside of React. So on this article element here, we can say colon key and then just set that equal to task ID. Now we talked about this a
second ago because I want this to be evaluated as an expression. I use a colon. If I remove that colon, it's just a string. I want it to evaluate the expression like that. Now let's extract this list out into its own component and I'll show you how we can actually pass this list of tasks to that component. So here in components, I'll create a new one called the task list. We'll give it a template. We're going to pull this out of here, drop it in here, and now we need to define our props so that way we get access to that list of tasks. So we'll add our script setup here. And right here I can say props is define props. So this is another compiler macro built into Vue that lets us define if we have any props. And I can also type this using TypeScript and say that we're expecting a list of tasks. And I can pull in that type, say it's an array, just like that. Right now I am getting a TypeScript error that says we're never reading this variable. Technically this code will still work. I won't change this for now. So you'll see that it will
passing props to components
work. But technically we also can say props.task. But because dealing with props is such a common thing, it makes it so that if any variable exists on props, you don't actually have to say props dot. But I'll show you that in a second. First, let's create an instance of this. So over here in app.view, we're going to want to bring in that task list like so that performed an autoimp import. So that exists here and then you'll notice we're getting a type error because we need to pass the tasks down.
So we do colon tasks and that's actually all we need. So this is a newer feature in Vue. It's a shorthand. So if a component accepts a prop that's exactly the same as the name of the variable that you're passing down, you actually don't need to pass it in. So it's it's a little nicity. If you want to type it out the long way, you can say tasks equals tasks like that. Or you could just use the shortand. But now this should work. So if we try adding a task, it shows up just like we expected. And I do not like seeing this error here. And I do like things to be a bit more clear.
So I am going to reference this from props.tasks. But like I said, it is a nicity. And there's probably a way to set up the TypeScript compiler so it doesn't complain about that so that you can access your props without having to do props dot. Okay, this is looking good. I do want a little bit of spacing right here. So I'm just going to give some margin top to our task list. So let's add a style tag here. I'll create a CSS class called task list. And then just give this a margin top of one rim like that. And then we'll wrap all of these in a div. And we'll give this a class of task list like so. So that just pushes it down just a little bit. All right. Now let's work on displaying a status message as well. So if the user has not added any tasks, I want to show a message that says add a task to get started. So, right here above the task list, we're going to have just a nice little H3. And this will say add a task to get started. Now, use your mind brain. How are we going to conditionally render this with a V if we did it
conditional rendering
earlier? So, only show this if tasks.length does not have a value. And so, now when the page loads, it says add a task to get started. And then if I add a task like so, that message disappears. But we can also do conditional else rendering. So if I want to display an H3 when this is not true, we can actually just use the v else directive. And here is where we'll add a status message. So right here I want to say X out of X tasks completed where this value is the total number of tasks. So right here we can render out tasks.length and then here we want to display the number of tasks that are done. Now we aren't able to mark task done just yet. So I'm just going to keep that as zero, but we'll come back to this. But we should see that conditionally rendered, right? So when the app loads, it says add a task to get started. If I add a task, that disappears. And now we see the total number of tasks. And then as we add more tasks, that number should go up. And it does. All right. Next up, I want to be
updating state in child
able to mark these tasks as done. So in our task list, I want to render out a checkbox next to each of these items here. So I'm going to create a label like so. And then inside of that I'll add an input with a type of checkbox like this. So now I should be able to check these items and then behind the scenes that should update their done property. And this is actually very easy because we have that directive vodels.
So here I can say bind this to task.done. And now automatically when I check this behind the scenes it's going to update that done property. And so now whenever I check this you'll see there is an update in the task list. If we take a look at its props, this actually got set to true. And if I uncheck it, that gets set to false. Now, while this works, it's technically an antiattern or a bad practice because we're modifying state that was actually defined in a parent component. So, this works, but probably the better thing to do would be to emit an event and then change that underlying data in the component that defined the state, in this case, the parent component. So, let's go ahead and define an emit here. And we'll just have an emit that is called toggle done. And this will take in the ID which is a string. So the parent component can then look for that task by ID and then it can in turn update the done property. So now instead of directly binding this with vmodel whenever this changes we want to
modifying props is an anti-pattern
emit events instead of changing props
emit toggle done and we're going to pass in the task ID. Now because we removed that vodel we also need to now bind the checked attribute. So checked needs to be bound to task.done like so. So while you can use v modelel technically you should only modify state in the place that it's defined. So now that we're emitting this event we can tap into that event in the parent component. So over here on the task list we can tap into toggle done.
And in this case we'll just define a method called toggle done. And this will take in the ID which is a string. So first we'll find that task inside of the tasks array. So we'll say task is tasks dov valueue.find and we're going to look at each individual task and we want the one where task do ID is equal to the ID that we're looking for and then if we found it then we can directly modify it. Then we can say task.done is equal to not task.done. So that'll just flop it. And so this is the preferred way of managing state essentially because that task originates here. We should really only modify it in this spot because otherwise our codebase could get pretty unruly if we're modifying that state all over the place. And so this should behave just as before. Let's give it a try. We'll start from scratch. We'll say learn view learn next. And so if I check it, it checks.
It unchecks. And we can see in the dev tools here the state is updating as well. Now next up, I want to show you how we can do conditional class binding. So whenever this element is marked as done, I actually want to put a line through it. So here in the task list, let's first create a class called done. And this will just set the text decoration to line through. And then we want to apply this done class if this task is marked as done. So I'm going to wrap the title here in a span. And then I want to apply that class if it's done.
css class binding
To do this, we can use a class binding. So we'll do colon class and then this accepts an object where each key in the object is the CSS class that you want to apply. In this case we want to apply the CSS class called done and we want to apply it when task.done is true. So essentially this object can have any number of properties which are CSS classes that you would like to apply to it and then the value for each one of these is an expression where if it is truthy that class will be applied but if it's falsy that class will not be applied. So just like that now when something is marked as done you can see that it has that class applied to it and the line through it. Okay. So now that we can actually mark tasks as done let's get this value right here updated. This needs to be the total number of tasks where done is set to true. So if we head back to the app component we want to calculate that right here. And built into view is a composition function called computed. So let's create a variable called total
computed state
done. And we're going to use computed which we import from view. This accepts a callback and whatever this returns will get stored in this variable as a reactive variable. And if any of the dependencies in this expression change, this will always have the latest value. So essentially what we can do is a simple little reduce that iterates over tasks and then returns the number of them that are done. So let's do that really quick. So we'll say tasks.value.reduce. We're going to get the total here and access to each task.
And if that task.done is true, we want to return total plus one. But if it's not true, we're just going to return the existing value of total. And this reduce will start off as the value zero. So this is essentially going to recalculate any time its dependencies change. And in this case, its dependencies are tasks. That's the only thing that it is referencing that's a state value. And so total done is always going to have the total number that are done. But what's nice about this is this only reruns if its dependencies change. So this isn't like React where it's like running on every render. This is essentially like use memo inside of React where if its dependencies change, rerun. But you'll notice we don't have to define the dependencies. Vue is able to figure that out automatically. But now that we have this computed value, we can render it out in our template here. So we'll add handlebars total done. And now this will keep track of the total number of done tasks. So it will say learn view, learn
next, learn js, watch syntax, all the things. But now as we check that computed value reruns and we always get the total number done displayed in the view here. So now I'd like to be able to remove tasks from the list. So if we head here to our task list component inside of the article here, we're going to add a button that will say remove.
remove elements from an array
That looks like that. I'm going to make this a flexbox and push that button to the end. So this article I'll give a class of task and then I'll make task a flexbox and we'll do justify content space between. So now that pushes them apart and then I want this text to be in the center. So we'll do align items center. So that looks good. Now Pico CSS actually has a class built in called outline and I'll add that just so this button looks a little bit different. So right here on the remove button, let's give this a class of outline. Like so.
That looks good. And then now when I click this button, I want to emit a remove task event. So we'll add another emits here called remove task. This will accept the ID of the task. And then when this button is clicked, we will emit the remove task event and pass in the task ID. So now in the parent component, we'll listen for that event. So we'll say on remove task, we will remove the task. We'll then define a function to remove it. And in this case, I just want to splice that task out of the array.
You could use a filter and reassign, but because our data is mutable, I just want to splice it out. So first I'll find the index of that task. So let's say the index is task.value.findex. And we want to find the task where its id is equal to that ID. And then if we found it, meaning the index was not negative 1, then we can say tasks.value.splice. We want to remove the task at that index. And we want to remove one of them. So just like that, when I click the button, it should remove them. So let's add a few tasks.
And now if I click remove, it gets removed. We'll see the total count gets updated as well. So let's add a few more. And as we remove them, the count goes down. And then when there are none, we get some nice status text. But now, instead of the tasks just immediately disappearing when they get removed, I want a nice animation when they disappear. And for that, we'll use transition group, which is built into Vue.js. So you can essentially wrap a list of elements that is changing with transition group and you can add a custom animation. So you can see here how it animates as things get added and removed. We want to do the same thing.
vue transition group
So, we're going to need to wrap our element that has the V4 in a transition group. So, here inside of our task list, right here is where we have the V4. We want our transition group to exist right there. So, we'll wrap that. Now, a transition group will create a parent element. And you can see in their example, the tag was a ul. In this example, I want the tag to be a div. So, instead of having this wrapper element, our transition group is going to be the wrapper element. So, I'm going to set the tag to div. And then we'll move this class onto the transition group. And then we can remove that wrapper div because now the transition group is our wrapper element. From there, if you look at their example, we just add some CSS classes that define the animation that should happen for that specific transition with that name. So let's copy these classes and then down here in our CSS, we'll throw them in. And the way this works is these class names start with the name of the transition group.
So by default, the example we copied had a name of list. So these CSS classes line up with that. But if we wanted to give it a different name like task list, we would need to make sure that each one of these classes uses the name of the transition group like this. And for our X transition, these are pretty wide elements. So let's do like 300 pixels.
So they should slide in and out. And just like that, now as we add and remove items, they slide away and they will also transition in. So if we read some items, you'll see that they slide in just like that. Fantastic. So we've got a pretty functional app. The last thing I want to do is add some filter buttons, right? So, we're going to add several tasks here. And then I want to be able to filter these to first be able to see all of them. Then I want to show only the ones that are marked as done. And then also be able to show just the ones that are to-do. So for that, we're just going to add some buttons here. So if we head to the app component right here below this status message, we'll just add a div. We'll give it that class of button container. So it pushes those buttons to the right. And then we'll add a button which will filter for all of them. One that will filter for just the ones that are to-do. And then one that shows us only the ones that are done.
filtering array by type
Now I'm going to give this a class of secondary. So it's a nice gray button. And I'll add this class to each of these buttons. Like so. So that looks good. But I do want to add some gap between there. So on the button container class, let's just add a gap for our flexbox to be let's say.5 rim. And so that looks like a good amount of spacing to me.
Now, we only want to show these buttons if there are tasks. So, we'll just add a conditional render on this. We'll say if tasks.length is not zero. And that way, those buttons only show up if we actually have some tasks in the list. So, let's hook these buttons up. Now, in my state, I now need to keep track of what we are currently filtered by. So, I'm going to have another state variable called filter. And this will be a ref, and we'll default to filtering by all of them. But the type here is just going to be a string union. So, you should be able to filter by all or by to-do or by done. And I'll pull this out into a type. So, that way we can use it in multiple spots. Let's call this the task filter. And then in my types file, we'll then export a type task filter. Just like that. And then back on our app.view, we will import this in. So, that looks good. And now, anytime I click one of those buttons, I want to update this filter value. So down below, when I click the all button, we want to
set the filter to all. And then when I click the to-do button, we want to set the filter to to-do. And then when I click done, we want to set the filter to done. Now, this is already getting a bit too repetitive for me. So this is a great indication that we should actually pull this button out into its own component. So let's actually do that.
We'll grab this. We'll create a new component called filter button. This component will need a template. We'll have the button inside of there. And then it needs to accept a prop which is the filter type. So we'll define a script and then here we can define our props. We'll use that define props macro. And then in this case we want to get access to that filter value which is of type task filter. And so that will allow us to specify the filter right here. So right here we'll just do props.filter. And we want to style it to be uppercased. So let's actually just add some scoped styles to this component. and we'll say this button should have a text transform of capitalize. So that'll capitalize the first letter of all to-do or done. Now when we click it, we're going to want to emit an event that updates the filter.
So we'll also define an emits here. In this case, it's just going to emit set filter. And this will accept a filter value which is of type task filter. And now whenever it's clicked, we will say emits set filter. And then we want to pass in the specific filter for this button. So now we have a nice component that we can reuse in all three of those spots. So right here we'll add our filter button. Import it in. We need to define the filter and this is going to be all. And then we need another one for to-do. And then we need another one for done. So this was a perfect use case for refactoring because we didn't want to have to redefine that logic for every single one. But now we want to bind to that event, the set filter event. And here we'll just define a method called set filter. and we'll bind that to each one of these and then we'll go define that method. So set filter super simple it just takes in a value that's a task filter and then it updates the filter state variable inside of here to be that
value. So here we'll say filter value is equal to the value that got passed in. Okay. So now when we click any one of these buttons it's setting this filter state variable. But in order to actually filter the task we're going to need another computed property. So here I'll define a computed called filtered tasks and this will be a computed but the nice thing about computed is we can define an entire function. So instead of just immediately returning a value in this case I'll just do a switch statement. So we'll switch over filter.value and in the case where the filter is all we just want to return all the tasks. So we'll do tasks.value like so. And then in the case where the filter is done we want to return all the tasks that are marked as done. So, we'll just do a filter. We'll say take all the tasks and then filter them where task.done is true. And then in the case of to-do, we want to filter them as well, but we want to filter the ones that are not done. And then the default case, if for whatever reason the
filter doesn't match, we'll just return all the tasks. And so now I have this nice computed variable where filtered task will be a list of tasks that is dependent on the current filter value. So, when it's all, it will be all the tasks. When it's done, it's only the ones that are marked done. And when it's to-do, it's only the ones that are marked as to-do. Now, to use this, we'll just pass this down to our task list component instead of all the tasks. So, down here, you'll see we were passing in tasks using that shorthand, but now instead, we'll actually pass down the filtered tasks. And that should do it.
So, let's add a few tasks like so. Now, all of our existing functionality should work. But if I head to to-do, we should only see the ones that are not marked as done. If I head to done, we should only see the ones that are marked as done. And we still get that nice animation. And if we head back to all, we see all of them. On any one of these pages, I can remove them. And it behaves just as it should. And we've got a fully working app. I think the last thing I want to do is indicate to the user which one of the filters they currently have selected because right now they're all the same color. So to do this, there's a couple of ways. We actually could put a class binding directly on the component here.
I think the best way to do it is pass down what the current filter value is and then inside of that component we'll have that conditional class binding. So let's define a new prop called current filter and then pass down filter. And we'll do that for each one of these. Just do a little bit of formatting. But now inside of the filter button component, we want another prop which is current filter. This is of type task filter. And then we can add that conditional class binding on the button.
And right here, we're going to bind to class. And in this case, the buttons actually already have the secondary class. So we want to add the contrast class if this filter is the currently selected one. So we'll add the contrast class if props.curren filter is equal to the filter value for this button. And just like that, now the currently selected filter is highlighted. So, let's finally add our last few tasks.
And now we can go through. We can mark these off as done. And as we toggle between the different states, we see that indication on the button. And over here, we can remove all of our done tasks. And finally, if you enjoyed this video, please definitely leave a comment down below and also subscribe because uh we sure do appreciate it. Now, if you enjoyed this video, you will definitely enjoy the video I'm releasing next week.
thanks!
So, if you're excited about that, leave a comment down below and let me know what you think of Vue.js. I personally think it's a joy to work with. As you can see, I'm pretty excited about building this app. But when I'm working with it, I just feel like they've thought of everything, right? All of everything I showed you today is just Vue.js, right? We didn't bring any extra libraries. This is all just Vue.js, the framework. And when you combine this with Nux, which gives you all the meta framework features, you get a really powerful experience. So, I love working with Vue. I've loved working with it for a long time. And if you saw anything interesting in this video, let me know down in the comments as well. I know Vue has changed a lot over the years and so maybe you're watching this video after not touching Vue for a while and there's definitely a lot of new stuff in Vue.js as well. Okay, so that's all I have for this one. Like I said, stay tuned for next week where we'll be diving deep into building a full stack app with Nux and Vue. And thank you so much for watching. All right, I'll see you in the next one.
Quote it with credit and a link. Quoting Syntax