Skip to content

Commit

Permalink
removed support of phx_* attributes in favor of phx-* attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
cblavier committed Feb 20, 2022
1 parent 922eb83 commit c1e1eda
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- removed interpolation of `raw` attributes as HEEX is now the only templating
engine target by `phx_component_helpers`
- removed support of `phx_*` attributes in favor of `phx-*` attributes

# 0.13.1

Expand Down
6 changes: 3 additions & 3 deletions lib/phx_component_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule PhxComponentHelpers do
It provides following features:
* set HTML or data attributes from component assigns
* set phx_* attributes from component assigns
* set phx-* attributes from component assigns
* set attributes with any custom prefix such as `@click` or `x-bind:` from [alpinejs](https://github.com/alpinejs/alpine)
* encode attributes as JSON from an Elixir structure assign
* validate mandatory attributes
Expand Down Expand Up @@ -103,15 +103,15 @@ defmodule PhxComponentHelpers do
## Example
```
assigns
|> set_phx_attributes(required: [:phx_submit], init: [:phx_change])
|> set_phx_attributes(required: [:"phx-submit"], init: [:"phx-change"])
```
`assigns` now contains `@heex_phx_change`, `@heex_phx_submit`
and `@heex_phx_attributes`.
"""
def set_phx_attributes(assigns, opts \\ []) do
opts = Keyword.put_new(opts, :into, :phx_attributes)
set_prefixed_attributes(assigns, ["phx_"], opts)
set_prefixed_attributes(assigns, ["phx-"], opts)
end

@doc ~S"""
Expand Down
12 changes: 6 additions & 6 deletions test/phx_component_helpers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,14 @@ defmodule PhxComponentHelpersTest do

describe "set_phx_attributes" do
test "with phx assigns it adds the phx-attribute" do
assigns = %{phx_change: "foo", phx_click: "bar", baz: "baz"}
assigns = %{:"phx-change" => "foo", :"phx-click" => "bar", baz: "baz"}
new_assigns = Helpers.set_phx_attributes(assigns)

assert new_assigns ==
assigns
|> Map.put(:heex_phx_attributes, "phx-change": "foo", "phx-click": "bar")
|> Map.put(:heex_phx_change, "phx-change": "foo")
|> Map.put(:heex_phx_click, "phx-click": "bar")
|> Map.put(:"heex_phx-change", "phx-change": "foo")
|> Map.put(:"heex_phx-click", "phx-click": "bar")
end

test "with init attributes it adds empty attribute" do
Expand All @@ -227,9 +227,9 @@ defmodule PhxComponentHelpersTest do
end

test "validates required attributes" do
assigns = %{phx_click: "click"}
new_assigns = Helpers.set_phx_attributes(assigns, required: [:phx_click], into: nil)
assert new_assigns == Map.put(assigns, :heex_phx_click, "phx-click": "click")
assigns = %{:"phx-click" => "click"}
new_assigns = Helpers.set_phx_attributes(assigns, required: [:"phx-click"], into: nil)
assert new_assigns == Map.put(assigns, :"heex_phx-click", "phx-click": "click")
end

test "with missing required attributes" do
Expand Down

0 comments on commit c1e1eda

Please sign in to comment.