06-04-renderFunction

by ZooeyLai

HTML

<div class="wrap">
        <div class="form-group">
            <input type="text" id="newTask" class="form-input">
            <select name="statusType" id="statusType" class="form-input" >
                <option value="" selected disabled>狀態</option>
                <option value="normal" selected>一般</option>
                <option value="important">重要</option>
                <option value="urgent">緊急</option>
            </select>
            <button class="btn btn-primary" onclick="add()">新增</button>
        </div>

        <div id="root"></div>
    </div>

CSS

:root{
        --primary: #4f46e5;
        --secondary:#2563eb;
        --secondary100: #dbeafe;
        --gray: #e2e8f0;
        --gray800: #333;
        --white: #fff;
        --background:#e4e4e4;
        --warning100 :#ffedd5;
        --warning : #ea580c;
        --danger100 :#fee2e2;
        --danger : #dc2626;
        --success : #059669;
        --success100: #ecfdf5;
    }    
    *,
    *::after,
    *::before {
        padding: 0px;
        margin: 0px;
        /* font-family: inherit; */
        box-sizing: border-box;
        line-height: 1.5;
        font-size: 1rem;
    }
    html,
    body {
        height: 100vh;
        
    }

    body {
        font-family: sans-serif;
        
        display: flex;
        /* flex-direction: column;
        align-items: center;
        justify-content: center; */
        background: var(--background);
    }
    .wrap{
        width: 600px;
        margin: 100px auto;
    }
    ul,li{
        list-style: none;
    }
    .form-group{
        margin-bottom: 1rem;
        display: flex;
        flex-flow: row;
        justify-content: space-between;
        gap: 1rem;
    }
    .form-input {
        border: 1px solid #94a3b8;
        padding: 0.5rem 1rem;
        border-radius: 0.25rem;
        flex: auto;
    }

        .form-input:focus {
        border-color: var(--primary);
        box-shadow: 0px 0px 0px 3px rgba(79, 70, 229, 0.4);
        outline: none;
    }

    .task-list li{
        padding: 1.5rem 1rem;
        display: flex;
        align-items: center;
        gap: 0.75rem;
        border-left: 4px solid #c1c1c1;
        border-radius: 0.25rem;
        background-color: var(--white);
    }
    .task-list li span{
        flex: auto;
    }
    .task-list li + li{
        margin: 0.5rem 0;
    }
    .task-list li:last-child{
        margin-bottom: 1rem;
    }
    .task-list li span.done{
        flex: none;
        color: var(--success);
        display: none;
        font-weight: 600;
    }
    .btn...

JavaScript

'use strict'; //嚴格模式
        // let todos =[];
        let todos = [
            { 
                title: '倒垃圾' ,
                category: "normal"
            },
            { 
                title: '繳電話費' ,
                category: "important"
            },
            { 
                title: '採買本週食材' ,
                category: "urgent"
            },
        ];
        let root = document.querySelector('#root');
        let tasksHolder = document.createElement('ul');

        function render(){
            
            tasksHolder.textContent = ''; //重新渲染之前清除任務列表
            tasksHolder.classList.add('task-list');

            root.append(tasksHolder);
            
            todos.forEach((task, index) => {
                let taskList = document.createElement('li'); 
                let newTaskEl = document.createElement('span'); 
                let deleteBtn = document.createElement('button');

                newTaskEl.classList.add('task-label');
                deleteBtn.classList.add('btn');
                tasksHolder.append(taskList);
                taskList.append(newTaskEl);
                taskList.append(deleteBtn);
                newTaskEl.textContent = task.title;
                deleteBtn.textContent = '刪除';
                deleteBtn.classList.add('btn','btn-outline-danger');
                
                if(task.category === 'important'){
                    taskList.classList.add('status-warning');
                    // newTaskEl.style.color = 'red';
                }else if(task.category === 'urgent'){
                    taskList.classList.add('status-danger');
                    // newTaskEl.style.color = 'orange';
                }

                deleteBtn.onclick = () => {
                    // 請寫出此 arrow function 內容(更新 todos 陣列)
                    todos.splice(index, 1);
                    render();
                    console.log(todos);
                }
            })
            
        }
        render();
   ...