This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import { formatDate } from '../../shared/lib/date.js';
|
||||
|
||||
export function renderArticleList({ articles, container, onSelect, query, selectedSlug }) {
|
||||
const normalizedQuery = query.trim().toLowerCase();
|
||||
const visibleArticles = articles.filter((article) => {
|
||||
if (!normalizedQuery) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return [article.title, article.slug, article.author, ...(article.categories || [])]
|
||||
.join(' ')
|
||||
.toLowerCase()
|
||||
.includes(normalizedQuery);
|
||||
});
|
||||
|
||||
if (visibleArticles.length === 0) {
|
||||
container.innerHTML = '<div class="article-list__empty">Статей не найдено</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
container.replaceChildren(
|
||||
...visibleArticles.map((article) => {
|
||||
const button = document.createElement('button');
|
||||
button.className = 'article-item';
|
||||
button.type = 'button';
|
||||
button.setAttribute('aria-selected', String(article.slug === selectedSlug));
|
||||
button.addEventListener('click', () => onSelect(article.slug));
|
||||
|
||||
const title = document.createElement('strong');
|
||||
title.textContent = article.title;
|
||||
|
||||
const meta = document.createElement('span');
|
||||
meta.textContent = `${formatDate(article.date)} · ${article.slug}`;
|
||||
|
||||
button.append(title, meta);
|
||||
return button;
|
||||
}),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user