Функция принимает массив, как вернуть html?
Делаю свой TODO лист. Пытаюсь сделать, чтобы класс ItemTemplate принимал в себя массив и возвращал html элементы в соответствии с количеством ввода в input, содержащий в себе введенный в input текст и сгенерированный data-атрибут
const todoList = [];
const list = document.querySelector('.todo-app__list');
class ItemTemplate {
listItem(items) {
return items.reduce((i, item) => {
`<li data-id="${item.id}" class="todo-app__list-item">
<input type="checkbox" class="todo-app__list-checkbox">
<label class="todo-app__list-checkbox-label">
${item.title}
</label>
<button class="todo-app__item-destroy"></button>
</li>`
})
}
}
const itemTemplate = new ItemTemplate();
document.addEventListener('keyup', function(e) {
if (e.keyCode === 13) {
const newId = getNewUserID();
const text = document.querySelector('.todo-app__input').value.trim();
document.querySelector('.todo-app__input').value = '';
todoList.push({
id: newId,
title: text,
completed: false
});
list.innerHTML = itemTemplate.listItem(todoList);
}
});
function getNewUserID() {
return Date.now();
}
.todo-app {
display: block;
margin: 0 auto;
}
.todo-app__title {
display: block;
width: 100%;
font-size: 100px;
line-height: 0;
font-weight: 100;
text-align: center;
color: rgba(175, 47, 47, 0.15);
}
.todo-app__input-wrapper {
display: block;
background: #ffffff;
position: relative;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), 0 25px 50px 0 rgba(0, 0, 0, 0.1);
}
.todo-app__input-label {
width: 550px;
}
.todo-app__input {
padding: 16px 16px 16px 60px;
border: none;
background: rgba(0, 0, 0, 0.003);
box-shadow: inset 0 -2px 1px rgba(0, 0, 0, 0.03);
position: relative;
margin: 0;
width: 86%;
font-size: 24px;
line-height: 1.4em;
font-weight: inherit;
font-style: italic;
opacity: 0.5;
outline: none;
}
.todo-app__input-checkbox {
width: 1px;
height: 1px;
border: none;
opacity: 0;
position: absolute;
right: 100%;
bottom: 100%;
&:checked {}
}
.todo-app__input-checkbox:checked {
.todo-app__input-checkbox-label::before {
color: #737373;
}
}
.todo-app__input-checkbox-label {
width: 60px;
height: 34px;
font-size: 0;
position: absolute;
top: 14px;
left: -13px;
transform: rotate(90deg);
cursor: default;
z-index: 1;
&::before {
content: '❯';
font-size: 22px;
color: #e6e6e6;
padding: 10px 27px 10px 27px;
cursor: default;
}
}
.todo-app__list-wrapper {
position: relative;
z-index: 2;
border-top: 1px solid #e6e6e6;
}
.todo-app__list {
margin: 0;
padding: 0;
list-style: none;
}
.todo-app__list-item {
position: relative;
font-size: 24px;
border-bottom: 1px solid #ededed;
&:last-child {
border-bottom: none;
}
}
.todo-app__list-item:hover {
.todo-app__item-destroy {
display: block;
color: #af5b5e;
}
}
.todo-app__list-checkbox {
margin: auto 0;
width: 40px;
height: 40px;
border-radius: 50%;
text-align: center;
position: absolute;
top: 0;
bottom: 0;
appearance: none;
outline: none;
background-image: url('data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%22-10%20-18%20100%20135%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22none%22%20stroke%3D%22%23ededed%22%20stroke-width%3D%223%22/%3E%3C/svg%3E');
background-repeat: no-repeat;
background-position: center left;
}
.todo-app__list-checkbox:checked {
background-image: url('data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%22-10%20-18%20100%20135%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22none%22%20stroke%3D%22%23bddad5%22%20stroke-width%3D%223%22/%3E%3Cpath%20fill%3D%22%235dc2af%22%20d%3D%22M72%2025L42%2071%2027%2056l-4%204%2020%2020%2034-52z%22/%3E%3C/svg%3E');
}
.todo-app__list-checkbox-label {
word-break: break-all;
padding: 15px 15px 15px 60px;
display: block;
line-height: 1.2;
transition: color 0.4s;
&_completed {
color: #d9d9d9;
text-decoration: line-through;
}
}
.todo-app__item-destroy {
display: none;
background: none;
border: none;
outline: none;
position: absolute;
top: 0;
right: 10px;
bottom: 0;
width: 40px;
height: 40px;
margin: auto 11px;
font-size: 30px;
color: #cc9a9a;
opacity: 0.8;
transition: all 0.2s ease-out;
}
.todo-app__item-destroy::after {
content: '×';
}
.todo-app__item-destroy:hover {
opacity: 1;
}
<label class="todo-app__input-label">
<input type="checkbox" class="todo-app__input-checkbox">
<label class="todo-app__input-checkbox-label"></label>
<input class="todo-app__input" placeholder="What needs to be done?" autofocus>
</label>
<div class="todo-app__list-wrapper">
<ul class="todo-app__list">
</ul>
</div>
Ответы (1 шт):
Автор решения: Roman Kozin
→ Ссылка
const todoList = [];
const list = document.querySelector('.todo-app__list');
class ItemTemplate {
listItem(items) {
return items.map((item) => {
return `<li data-id="${item.id}" class="todo-app__list-item">
<input type="checkbox" class="todo-app__list-checkbox">
<label class="todo-app__list-checkbox-label">
${item.title}
</label>
<button class="todo-app__item-destroy"></button>
</li>`;
})
.join("\r\n")
}
}
const itemTemplate = new ItemTemplate();
document.addEventListener('keyup', function(e) {
if (e.keyCode === 13) {
const newId = getNewUserID();
const text = document.querySelector('.todo-app__input').value.trim();
document.querySelector('.todo-app__input').value = '';
todoList.push({
id: newId,
title: text,
completed: false
});
list.innerHTML = itemTemplate.listItem(todoList);
}
});
function getNewUserID() {
return Date.now();
}
.todo-app {
display: block;
margin: 0 auto;
}
.todo-app__title {
display: block;
width: 100%;
font-size: 100px;
line-height: 0;
font-weight: 100;
text-align: center;
color: rgba(175, 47, 47, 0.15);
}
.todo-app__input-wrapper {
display: block;
background: #ffffff;
position: relative;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), 0 25px 50px 0 rgba(0, 0, 0, 0.1);
}
.todo-app__input-label {
width: 550px;
}
.todo-app__input {
padding: 16px 16px 16px 60px;
border: none;
background: rgba(0, 0, 0, 0.003);
box-shadow: inset 0 -2px 1px rgba(0, 0, 0, 0.03);
position: relative;
margin: 0;
width: 86%;
font-size: 24px;
line-height: 1.4em;
font-weight: inherit;
font-style: italic;
opacity: 0.5;
outline: none;
}
.todo-app__input-checkbox {
width: 1px;
height: 1px;
border: none;
opacity: 0;
position: absolute;
right: 100%;
bottom: 100%;
&:checked {}
}
.todo-app__input-checkbox:checked {
.todo-app__input-checkbox-label::before {
color: #737373;
}
}
.todo-app__input-checkbox-label {
width: 60px;
height: 34px;
font-size: 0;
position: absolute;
top: 14px;
left: -13px;
transform: rotate(90deg);
cursor: default;
z-index: 1;
&::before {
content: '❯';
font-size: 22px;
color: #e6e6e6;
padding: 10px 27px 10px 27px;
cursor: default;
}
}
.todo-app__list-wrapper {
position: relative;
z-index: 2;
border-top: 1px solid #e6e6e6;
}
.todo-app__list {
margin: 0;
padding: 0;
list-style: none;
}
.todo-app__list-item {
position: relative;
font-size: 24px;
border-bottom: 1px solid #ededed;
&:last-child {
border-bottom: none;
}
}
.todo-app__list-item:hover {
.todo-app__item-destroy {
display: block;
color: #af5b5e;
}
}
.todo-app__list-checkbox {
margin: auto 0;
width: 40px;
height: 40px;
border-radius: 50%;
text-align: center;
position: absolute;
top: 0;
bottom: 0;
appearance: none;
outline: none;
background-image: url('data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%22-10%20-18%20100%20135%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22none%22%20stroke%3D%22%23ededed%22%20stroke-width%3D%223%22/%3E%3C/svg%3E');
background-repeat: no-repeat;
background-position: center left;
}
.todo-app__list-checkbox:checked {
background-image: url('data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%22-10%20-18%20100%20135%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22none%22%20stroke%3D%22%23bddad5%22%20stroke-width%3D%223%22/%3E%3Cpath%20fill%3D%22%235dc2af%22%20d%3D%22M72%2025L42%2071%2027%2056l-4%204%2020%2020%2034-52z%22/%3E%3C/svg%3E');
}
.todo-app__list-checkbox-label {
word-break: break-all;
padding: 15px 15px 15px 60px;
display: block;
line-height: 1.2;
transition: color 0.4s;
&_completed {
color: #d9d9d9;
text-decoration: line-through;
}
}
.todo-app__item-destroy {
display: none;
background: none;
border: none;
outline: none;
position: absolute;
top: 0;
right: 10px;
bottom: 0;
width: 40px;
height: 40px;
margin: auto 11px;
font-size: 30px;
color: #cc9a9a;
opacity: 0.8;
transition: all 0.2s ease-out;
}
.todo-app__item-destroy::after {
content: '×';
}
.todo-app__item-destroy:hover {
opacity: 1;
}
<label class="todo-app__input-label">
<input type="checkbox" class="todo-app__input-checkbox">
<label class="todo-app__input-checkbox-label"></label>
<input class="todo-app__input" placeholder="What needs to be done?" autofocus>
</label>
<div class="todo-app__list-wrapper">
<ul class="todo-app__list">
</ul>
</div>