Development
Kumaresan  

How To Create TODO Application Using LWC

Hey There!

Now we gonna build Todo Web App using Lightning web component. It’s just like a step by step tutorial, don’t forget to follow all the step then it will be easy for you.

STEP 1: Create your TODO App

Follow The CLI steps to setup your app.

Note : you must have Node.js installed, with at least npm 5.2+.

npx lwc-create-app you-app-name
cd your-app-name
npm run watch

Now you will have an app folder structure like this.

Todo

STEP 2: Work with your component files.

Now you just check the src folder, Modules and components will be there.

open your follwoing files and paste the code

  • App.html
  • App.js
  • App.css

Note : You can change Component files name  based on your project name

<!--  App.html  -->

<template>
    <div class="center">
        <img src="/resources/lwc.png" />
    </div>

    <div class="toDo">
       
        <div class="header">
            <h2>My To Do List</h2>
            <input 
                type="text"  
                placeholder="Type Here..."
                onchange={handleToDoChanges} 
            >
            <span  class="addBtn"  value={taskDetails} onclick={handleToDo}>Add</span>

        </div>
       
         <ul>
            <template for:each={todos} for:item="todo">
                <li key={todo.id}>
                    {todo.taskDetails}
                </li>
            </template>
        </ul>     
    </div>
   
</template>
// App.js 

import { LightningElement, track } from 'lwc';

export default class App extends LightningElement {

    TodoId = 2;
    @track todos = [
        { id: 1, taskDetails: 'Write Story' },
        { id: 2, taskDetails: 'Build Framework' }
    ];

   
    @track taskDetails;

    handleToDoChanges(event){
        this.taskDetails = event.target.value;
   
    }
    handleToDo(){
    	this.TodoId = this.TodoId + 1;
        this.todos = [
            ...this.todos,
            {
                id: this.TodoId,
                taskDetails: this.taskDetails,
            }
        ];
    }
}
// App.css
.center {
    text-align: center;
}
* {
    box-sizing: border-box;
  }
  
  ul {
    margin: 0;
    padding: 0;
  }
  
  ul li {
    cursor: pointer;
    position: relative;
    padding: 12px 8px 12px 40px;
    background: #eee;
    font-size: 18px;
    transition: 0.2s;
    list-style: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
  }

  ul li:nth-child(odd) {
    background: #f9f9f9;
  }

  ul li:hover {
    background: #ddd;
  }
  

  .header {
    background-color: #00a0df;
    padding: 30px 40px;
    color: white;
    text-align: center;
    border-radius: 15px;
  }
  
  .header:after {
    content: "";
    display: table;
    clear: both;
  }
  input {
    margin: 0;
    border: none;
    border-radius: 0;
    width: 75%;
    padding: 10px;
    float: left;
    font-size: 16px;
  }

  .addBtn {
    padding: 10px;
    width: 25%;
    background: #d9d9d9;
    color: #555;
    float: left;
    text-align: center;
    font-size: 16px;
    cursor: pointer;
    transition: 0.3s;
    border-radius: 0;
  }
  
  .addBtn:hover {
    background-color: #bbb;
  }
  .center img{
    width: 100px;
  }
.toDo{
    width:600px;
    margin: 0 auto;
}  

STEP 3: Run your app

Finally, run your app using CLI

npm run watch

Hope you got the TODO App, you can also download the source code from my GitHub page 🙂

Leave A Comment