all repos — h3 @ cf18fcdb77466464bfe4adc7622efd5ecf740a33

A tiny, extremely minimalist JavaScript microframework.

example/assets/js/components/paginator.js

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
import h3 from "../h3.js";

export default function Paginator(data, actions) {
  const { refresh } = actions;
  let page = data.page;
  const size = data.size;
  const total = data.total;
  const pages = Math.ceil(total / size) || 1;
  const previousClass = page > 1 ? ".link" : ".disabled";
  const nextClass = page < pages ? ".link" : ".disabled";
  function setPreviousPage() {
    page = page - 1;
    window.location.hash = `/?page=${page}`;
    refresh();
  }
  function setNextPage() {
    page = page + 1;
    window.location.hash = `/?page=${page}`;
    refresh();
  }
  return h3("div.paginator", [
    h3(
      `span.previous-page${previousClass}`,
      {
        onclick: setPreviousPage,
      },
      ["←"]
    ),
    h3("span.current-page", [`${String(page)}/${String(pages)}`]),
    h3(
      `span.next-page${nextClass}`,
      {
        onclick: setNextPage,
      },
      ["→"]
    ),
  ]);
}