Skip to content

Commit

Permalink
fix: wrap links to first and last page in li as well
Browse files Browse the repository at this point in the history
  • Loading branch information
woylie committed Oct 19, 2023
1 parent 827ad51 commit 676ad79
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 52 deletions.
48 changes: 26 additions & 22 deletions lib/flop_phoenix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -487,17 +487,19 @@ defmodule Flop.Phoenix do

~H"""
<ul :if={@opts[:page_links] != :hide} {@opts[:pagination_list_attrs]}>
<.pagination_link
:if={@first > 1}
event={@event}
target={@target}
page={1}
path={@page_link_helper.(1)}
on_paginate={@on_paginate}
{Pagination.attrs_for_page_link(1, @meta, @opts)}
>
1
</.pagination_link>
<li>
<.pagination_link
:if={@first > 1}
event={@event}
target={@target}
page={1}
path={@page_link_helper.(1)}
on_paginate={@on_paginate}
{Pagination.attrs_for_page_link(1, @meta, @opts)}
>
1
</.pagination_link>
</li>
<li :if={@first > 2}>
<span {@opts[:ellipsis_attrs]}><%= @opts[:ellipsis_content] %></span>
Expand All @@ -520,17 +522,19 @@ defmodule Flop.Phoenix do
<span {@opts[:ellipsis_attrs]}><%= @opts[:ellipsis_content] %></span>
</li>
<.pagination_link
:if={@last < @meta.total_pages}
event={@event}
target={@target}
page={@meta.total_pages}
path={@page_link_helper.(@meta.total_pages)}
on_paginate={@on_paginate}
{Pagination.attrs_for_page_link(@meta.total_pages, @meta, @opts)}
>
<%= @meta.total_pages %>
</.pagination_link>
<li>
<.pagination_link
:if={@last < @meta.total_pages}
event={@event}
target={@target}
page={@meta.total_pages}
path={@page_link_helper.(@meta.total_pages)}
on_paginate={@on_paginate}
{Pagination.attrs_for_page_link(@meta.total_pages, @meta, @opts)}
>
<%= @meta.total_pages %>
</.pagination_link>
</li>
</ul>
"""
end
Expand Down
60 changes: 30 additions & 30 deletions test/flop_phoenix_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ defmodule Flop.PhoenixTest do
<Flop.Phoenix.pagination meta={@meta} event="paginate" />
""")

assert [link] = Floki.find(html, "a[aria-label='Go to page 1']")
assert [link] = Floki.find(html, "li a[aria-label='Go to page 1']")
assert Floki.attribute(link, "href") == ["#"]
assert Floki.attribute(link, "phx-click") == ["paginate"]
assert Floki.attribute(link, "phx-value-page") == ["1"]
Expand All @@ -559,7 +559,7 @@ defmodule Flop.PhoenixTest do
<Flop.Phoenix.pagination meta={@meta} event="paginate" target="here" />
""")

assert [link] = Floki.find(html, "a[aria-label='Go to page 1']")
assert [link] = Floki.find(html, "li a[aria-label='Go to page 1']")
assert Floki.attribute(link, "phx-target") == ["here"]
end

Expand Down Expand Up @@ -617,12 +617,12 @@ defmodule Flop.PhoenixTest do
/>
""")

assert [link] = Floki.find(html, "a[aria-label='Go to page 1']")
assert [link] = Floki.find(html, "li a[aria-label='Go to page 1']")
assert Floki.attribute(link, "beep") == ["boop"]
assert Floki.attribute(link, "class") == ["p-link"]

# current link attributes are unchanged
assert [link] = Floki.find(html, "a[aria-label='Go to page 2']")
assert [link] = Floki.find(html, "li a[aria-label='Go to page 2']")
assert Floki.attribute(link, "beep") == []
assert Floki.attribute(link, "class") == ["pagination-link is-current"]
end
Expand All @@ -639,14 +639,14 @@ defmodule Flop.PhoenixTest do
/>
""")

assert [link] = Floki.find(html, "a[aria-label='Go to page 1']")
assert [link] = Floki.find(html, "li a[aria-label='Go to page 1']")
assert Floki.attribute(link, "class") == ["pagination-link"]
assert Floki.attribute(link, "data-phx-link") == ["patch"]
assert Floki.attribute(link, "data-phx-link-state") == ["push"]
assert Floki.attribute(link, "href") == ["/pets?page_size=10"]
assert String.trim(Floki.text(link)) == "1"

assert [link] = Floki.find(html, "a[aria-label='Go to page 2']")
assert [link] = Floki.find(html, "li a[aria-label='Go to page 2']")
assert Floki.attribute(link, "beep") == ["boop"]
assert Floki.attribute(link, "class") == ["link is-active"]
assert Floki.attribute(link, "data-phx-link") == ["patch"]
Expand Down Expand Up @@ -721,7 +721,7 @@ defmodule Flop.PhoenixTest do
assert [href] = Floki.attribute(previous, "href")
assert_urls_match(href, "/pets", expected_query.(1))

assert [one] = Floki.find(html, "a[aria-label='Go to page 1']")
assert [one] = Floki.find(html, "li a[aria-label='Go to page 1']")
assert Floki.attribute(one, "class") == ["pagination-link"]
assert Floki.attribute(one, "data-phx-link") == ["patch"]
assert Floki.attribute(one, "data-phx-link-state") == ["push"]
Expand Down Expand Up @@ -842,7 +842,7 @@ defmodule Flop.PhoenixTest do
assert [href] = Floki.attribute(previous, "href")
assert_urls_match(href, "/pets", expected_query.(1))

assert [one] = Floki.find(html, "a[aria-label='Go to page 1']")
assert [one] = Floki.find(html, "li a[aria-label='Go to page 1']")
assert Floki.attribute(one, "class") == ["pagination-link"]
assert Floki.attribute(one, "data-phx-link") == ["patch"]
assert Floki.attribute(one, "data-phx-link-state") == ["push"]
Expand Down Expand Up @@ -898,13 +898,13 @@ defmodule Flop.PhoenixTest do
<Flop.Phoenix.pagination meta={@meta} path="/pets" opts={@opts} />
""")

assert html |> Floki.find(".pagination-ellipsis") |> length() == 1
assert html |> Floki.find(".pagination-link") |> length() == 6
assert html |> Floki.find("li .pagination-ellipsis") |> length() == 1
assert html |> Floki.find("li .pagination-link") |> length() == 6

assert Floki.find(html, "a[aria-label='Go to page 20']")
assert Floki.find(html, "li a[aria-label='Go to page 20']")

for i <- 1..5 do
assert Floki.find(html, "a[aria-label='Go to page #{i}']")
assert Floki.find(html, "li a[aria-label='Go to page #{i}']")
end
end

Expand All @@ -922,10 +922,10 @@ defmodule Flop.PhoenixTest do
assert html |> Floki.find(".pagination-ellipsis") |> length() == 1
assert html |> Floki.find(".pagination-link") |> length() == 6

assert Floki.find(html, "a[aria-label='Go to page 1']")
assert Floki.find(html, "li a[aria-label='Go to page 1']")

for i <- 16..20 do
assert Floki.find(html, "a[aria-label='Go to page #{i}']")
assert Floki.find(html, "li a[aria-label='Go to page #{i}']")
end
end

Expand All @@ -943,11 +943,11 @@ defmodule Flop.PhoenixTest do
assert html |> Floki.find(".pagination-ellipsis") |> length() == 2
assert html |> Floki.find(".pagination-link") |> length() == 8

assert Floki.find(html, "a[aria-label='Go to page 1']")
assert Floki.find(html, "a[aria-label='Go to page 20']")
assert Floki.find(html, "li a[aria-label='Go to page 1']")
assert Floki.find(html, "li a[aria-label='Go to page 20']")

for i <- 10..15 do
assert Floki.find(html, ".a[aria-label='Go to page #{i}']")
assert Floki.find(html, "li a[aria-label='Go to page #{i}']")
end
end

Expand All @@ -965,11 +965,11 @@ defmodule Flop.PhoenixTest do
assert html |> Floki.find(".pagination-ellipsis") |> length() == 2
assert html |> Floki.find(".pagination-link") |> length() == 7

assert Floki.find(html, "a[aria-label='Go to page 1']")
assert Floki.find(html, "a[aria-label='Go to page 20']")
assert Floki.find(html, "li a[aria-label='Go to page 1']")
assert Floki.find(html, "li a[aria-label='Go to page 20']")

for i <- 9..13 do
assert Floki.find(html, "a[aria-label='Go to page #{i}']")
assert Floki.find(html, "li a[aria-label='Go to page #{i}']")
end
end

Expand All @@ -987,11 +987,11 @@ defmodule Flop.PhoenixTest do
assert html |> Floki.find(".pagination-ellipsis") |> length() == 2
assert html |> Floki.find(".pagination-link") |> length() == 7

assert Floki.find(html, "a[aria-label='Go to page 1']")
assert Floki.find(html, "a[aria-label='Go to page 20']")
assert Floki.find(html, "li a[aria-label='Go to page 1']")
assert Floki.find(html, "li a[aria-label='Go to page 20']")

for i <- 8..12 do
assert Floki.find(html, "a[aria-label='Go to page #{i}']")
assert Floki.find(html, "li a[aria-label='Go to page #{i}']")
end
end

Expand All @@ -1009,11 +1009,11 @@ defmodule Flop.PhoenixTest do
assert html |> Floki.find(".pagination-ellipsis") |> length() == 2
assert html |> Floki.find(".pagination-link") |> length() == 7

assert Floki.find(html, "a[aria-label='Go to page 1']")
assert Floki.find(html, "a[aria-label='Go to page 20']")
assert Floki.find(html, "li a[aria-label='Go to page 1']")
assert Floki.find(html, "li a[aria-label='Go to page 20']")

for i <- 9..13 do
assert Floki.find(html, "a[aria-label='Go to page #{i}']")
assert Floki.find(html, "li a[aria-label='Go to page #{i}']")
end
end

Expand All @@ -1031,10 +1031,10 @@ defmodule Flop.PhoenixTest do
assert html |> Floki.find(".pagination-ellipsis") |> length() == 1
assert html |> Floki.find(".pagination-link") |> length() == 6

assert Floki.find(html, "a[aria-label='Go to page 20']")
assert Floki.find(html, "li a[aria-label='Go to page 20']")

for i <- 1..5 do
assert Floki.find(html, "a[aria-label='Go to page #{i}']")
assert Floki.find(html, "li a[aria-label='Go to page #{i}']")
end
end

Expand All @@ -1052,10 +1052,10 @@ defmodule Flop.PhoenixTest do
assert html |> Floki.find(".pagination-ellipsis") |> length() == 1
assert html |> Floki.find(".pagination-link") |> length() == 6

assert Floki.find(html, "a[aria-label='Go to page 1']")
assert Floki.find(html, "li a[aria-label='Go to page 1']")

for i <- 16..20 do
assert Floki.find(html, "a[aria-label='Go to page #{i}']")
assert Floki.find(html, "li a[aria-label='Go to page #{i}']")
end
end

Expand Down

0 comments on commit 676ad79

Please sign in to comment.