From 6c2fbef0021ffb2e286e993a76b841fdc10068d5 Mon Sep 17 00:00:00 2001 From: mhenrixon Date: Sat, 8 Nov 2025 08:17:34 +0100 Subject: [PATCH] Add Pagination component - Implements Pagination component with DaisyUI styling - Adds comprehensive test coverage - Includes responsive class comments for Tailwind --- lib/phlexy_ui/pagination.rb | 50 ++++++++++++++ spec/lib/phlexy_ui/pagination_spec.rb | 98 +++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 lib/phlexy_ui/pagination.rb create mode 100644 spec/lib/phlexy_ui/pagination_spec.rb diff --git a/lib/phlexy_ui/pagination.rb b/lib/phlexy_ui/pagination.rb new file mode 100644 index 0000000..177ddb6 --- /dev/null +++ b/lib/phlexy_ui/pagination.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +module PhlexyUI + # @component html class="join" + class Pagination < Base + def initialize(*, as: :div, **) + super(*, **) + @as = as + end + + def view_template(&) + generate_classes!( + # "join" + component_html_class: :join, + modifiers_map: modifiers, + base_modifiers:, + options: + ).then do |classes| + public_send(as, class: classes, **options, &) + end + end + + def item(**options, &) + generate_classes!( + # "join-item" + component_html_class: :"join-item", + options: + ).then do |classes| + div(class: classes, **options, &) + end + end + + register_modifiers( + # "sm:join-vertical" + # "@sm:join-vertical" + # "md:join-vertical" + # "@md:join-vertical" + # "lg:join-vertical" + # "@lg:join-vertical" + vertical: "join-vertical", + # "sm:join-horizontal" + # "@sm:join-horizontal" + # "md:join-horizontal" + # "@md:join-horizontal" + # "lg:join-horizontal" + # "@lg:join-horizontal" + horizontal: "join-horizontal" + ) + end +end diff --git a/spec/lib/phlexy_ui/pagination_spec.rb b/spec/lib/phlexy_ui/pagination_spec.rb new file mode 100644 index 0000000..a55984d --- /dev/null +++ b/spec/lib/phlexy_ui/pagination_spec.rb @@ -0,0 +1,98 @@ +require "spec_helper" + +describe PhlexyUI::Pagination do + subject(:output) { render described_class.new } + + it "is expected to match the formatted HTML" do + expected_html = html <<~HTML +
+ HTML + + is_expected.to eq(expected_html) + end + + describe "with item method" do + subject(:output) do + render described_class.new do |p| + p.item { "1" } + p.item { "2" } + p.item { "3" } + end + end + + it "renders items" do + expected_html = html <<~HTML +
+
1
+
2
+
3
+
+ HTML + + expect(output).to eq(expected_html) + end + end + + describe "conditions" do + { + vertical: "join-vertical", + horizontal: "join-horizontal" + }.each do |modifier, css| + context "when given :#{modifier} modifier" do + subject(:output) { render described_class.new(modifier) } + + it "renders it apart from the main class" do + expected_html = html <<~HTML +
+ HTML + + expect(output).to eq(expected_html) + end + end + end + end + + describe "data" do + subject(:output) do + render described_class.new(data: {foo: "bar"}) + end + + it "renders it correctly" do + expected_html = html <<~HTML +
+ HTML + + expect(output).to eq(expected_html) + end + end + + describe "responsiveness" do + %i[sm md lg xl @sm @md @lg @xl].each do |viewport| + context "when given an :#{viewport} responsive option" do + subject(:output) do + render described_class.new(:vertical, responsive: {viewport => :horizontal}) + end + + it "renders it separately with a responsive prefix" do + expected_html = html <<~HTML +
+ HTML + + expect(output).to eq(expected_html) + end + end + end + end + + describe "passing :as option" do + subject(:output) { render described_class.new(as: :nav) } + + it "renders as the given tag" do + expected_html = html <<~HTML + + HTML + + expect(output).to eq(expected_html) + end + end +end