SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel

Tuesday 21 July 2020

Salesforce LWC Tutorial Part 1 | ToDo App Project | Designing the markup in SLDS

Hello Trailblazers,

Welcome to the first tutorial in LWC Tutorial Series. We're focusing on the concept of Learning By Doing in this tutorial series and following that, it's time to get started with our first project in LWC which is a ToDo Application. In this tutorial, we'll be designing the markup in SLDS for our ToDo application and going forward in further tutorials, we'll be adding JavaScript in our application to make it working as shown below:-


So, let's start building the above application and while doing that, we're going to see from very basic, how we can built the HTML structure by looking at a design and we'll learn about various lightning tags and what's their usage on the go.

This tutorial is published on SFDC Stop YouTube Channel. So, if you want to learn in detail, have a look at the tutorial video down below. Otherwise, you can scroll down to have a look at the code gist with explanation where we created the markup of our ToDo Application

Tutorial Video


Code Gist with Explanation

I highly recommend you to have a look at the video to understand the thinking process like:- How I proceeded with creation of this component by just having a look at the design. However, I'll be explaining the code of the same below. So, let's have a look at the code using which we created our first ToDo App LWC component. My component name is todo and the code is given below:-

HTML Snippet (todo.html)

As you can see in the above code, I have first of all created a lightning-card with a title ToDo List. I added a lightning-button-icon in the actions slot of the card with the icon of sync. I used slds icons to find out which icon I should use for this purpose. Next, I created the card body using lightning-layout and also created a paragraph which I added to the footer slot of the card.

Let's learn in detail about the card body first, inside the lightning-layout, I added a lightning-layout-item with padding as around-small in order to give equal padding from the border to all the list items and I specified the size to 12 as each row in lightning-layout can be divided into 12 columns at max, so, I wanted the card body to take/cover the maximum width available.

After that, I created an unordered list by referring to the slds code shown here and under each list item, I again created a lightning-layout as I wanted to divide the list item into two parts, one for the task name and other for the delete button which will be used to delete the task. This lightning-layout has a vertical-align property of center so that the text (task name) is aligned middle in vertical direction to look good with delete button and also, I specified a horizontal-align property to spread as I want the items in the task row i.e. the task name and delete button to spread evenly in the width available.

As specified above, each lightning-layout under the list item has two lightning-layout-item one for task name and one for delete button. I have given the padding as horizontal-small to both these lightning-layout-item so that they're properly spaced from each other and also from the corners. I again used lightning-button-icon in order to create delete button for each task.

Moving onto the footer code, I again used lightning-layout but this time I specified a property named as pull-to-boundary as small in order to make sure that the text field and the button that I am going to create should stick with their boundaries irrespective of the width. I created two lightning-layout-item inside that. Again, these two are having a padding of horizontal-small so that they're placed away from each other and from the footer corners as well.

In the first lightning-layout-item, I added the property of flexibility as grow because I want the input field to grow as the width increases, I didn't add this property to the second lightning-layout-item where I am going to place the button because I want the button to have a fixed width as it shouldn't grow depending on the container width.

Finally, I added a lightning-input field to the first layout item with a placeholder as Enter new task so that it's shown as a hint and I also specified the variant as label-hidden as I didn't want a label for this field. I also made this field required as it's important to add a task name before adding it to the todo list. For the second layout item, I just added a simple lightning-button with variant as brand as I wanted it to be blue in color. I specified the label of this button as Add, as this button will be used to add elements to the todo list.

The output of the above code i.e. the final design is shown below:-



That's all for this tutorial. I hope you liked it. All the code used in this tutorial is available on GitHub and you can have a look at that by clicking here. Make sure to have a look at the video if you want to learn in detail and let me know your feedback in the comments down below.

Happy Trailblazing..!!

Saturday 18 July 2020

Getting Started with LWC | Setup local server for Lightning web components | First LWC Component

Hello Trailblazers,

Welcome to the first tutorial in LWC Tutorial Series. In this tutorial, we'll cover the below points :-

1. What is LWC ?
2. Why should we use LWC when Aura is here ? 😎
3. Comparison between Old and New Web Stacks
4. Setting up Local Server for Lightning web component development
5. Creating your first LWC component with a little bit of HTML and JavaScript
6. Have a look at the references and resources for learning LWC

This tutorial is published on SFDC Stop YouTube Channel. So, if you want to learn in detail, have a look at the tutorial video down below. Otherwise, you can scroll down to the presentation and have a look at the code gist with brief explanation where we created our First LWC Component

Tutorial Video


Presentation

The presentation used in this tutorial is published on Slideshare and can be viewed below.

Code Gist with brief Explanation

Now let's have a look at the code using which we created our first LWC component. My component name is myFirstLWC and the code is given below:-

HTML Snippet (myFirstLWC.html)

As you can see in the above code, I created a simple lighting-card that consist of a lightning-input field with a label Write your Name. This field has an onchange handler associated which is calling my updateName method present in the javascript file. The value of this field is binded with the name variable which we're going to define in our js file. Below that, we just have displayed a text as:- Hello {name}, Welcome to your first LWC Component. In this statement everything is static except the name variable which is declared in the JavaScript file and being used here, by specifying it within curly braces{}.

JavaScript Snippet (myFirstLWC.js)

As you can see above, I have just created a name variable which is having a default value empty. Below that we have our updateName method defined which is responsible to udpate the name variable with the value of the input field as this method will be called automatically whenever the value of input field is changed by reacting to the onchange handler. The onchange event will be passed onto this method automatically and inside this method, we're getting the reference to the HTML element which is changed by using event.target. As, we need the value of that HTML element, we're getting that using event.target.value. We're assigning that value to the name variable that will automatically cause the HTML to re-render as the name variable used in HTML as {name}, is updated now.

The component will look like this initially:-


As you click on the lightning-input field and write anything there. The component will be re-rendered automatically and you'll see your text after hello as shown below:-


I am viewing the LWC component in the LWC Local Server that I've setup in my system. If you've Node.js and SFDX CLI installed in your system, you can also setup the local LWC Server by executing the below commands in the terminal:-

sfdx plugins:install @salesforce/lwc-dev-server

The above command will install lwc-dev-server in your local system. After it's installed, open the terminal again and run the below command:-

sfdx plugins:update

This command will basically update all the sfdx plugins so that you can run your LWC component on the local server installed.

After your local server is installed successfully, you can preview the LWC component by right clicking on the lwc component in your VSCode project and choosing SFDX: Preview Component Locally option as shown below:-


Choose the first option, i.e. Use Desktop Browser to preview the component locally in your default browser as shown below:-


Make sure you have Salesforce Extension Pack installed in VSCode. You can have a look at this tutorial to setup VSCode for Salesforce Development:- How to setup Visual Studio Code for Salesforce ?

You can also use the below command to start the local dev server in case the extensions are not working properly:-

sfdx force:lightning:lwc:start

In case you're follwing some issues, installing the sfdx plugins, read the instructions carefully as it's possible that you're missing some node js packages, you can install those packages by using the below command:-

npm install -g <package name>

-g  is basically used to install the package globally so that you can use it anywhere.

That's all for this tutorial. I hope you liked it. All the code used in this tutorial is available on GitHub and you can have a look at that by clicking here. Make sure to have a look at the video if you want to learn in detail and let me know your feedback in the comments down below.

Happy Trailblazing..!!