Skip to content

Commit

Permalink
Merge pull request #2 from tmepple/master
Browse files Browse the repository at this point in the history
Add option to override default LiveView binding prefix and Readme tweaks
  • Loading branch information
fidr authored Oct 29, 2019
2 parents ab080e7 + 0877d82 commit c9e9c82
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,30 @@ Add to your `mix.exs` and run `mix deps.get`:
```elixir
def deps do
[
{:phoenix_live_react, "~> 0.2.0"}
{:phoenix_live_react, "~> 0.2"}
]
end
```

Then add to your `assets/package.json` and run `npm i` or `yarn`:

```
{
...
"dependencies": {
...
"phoenix": "file:../deps/phoenix",
"phoenix_html": "file:../deps/phoenix_html",
"phoenix_live_view": "file:../deps/phoenix_live_view",
"phoenix_live_react": "file:../deps/phoenix_live_react", <-- ADD THIS!
...
},
...
}
```

Note for umbrella projects the relative file paths should look like `"file:../../../deps/phoenix_live_react"`

## Usage

Add your react components to the window scope (`app.js`):
Expand Down
10 changes: 8 additions & 2 deletions lib/phoenix_live_react.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ defmodule PhoenixLiveReact do
You can also override the tag type with the `:container_tag` and `:receiver_tag` options
By default, LiveView uses `phx-` as the binding prefix. You can override this with the
`:binding_prefix` option.
```
<%=
PhoenixLiveReact.live_react_component("Components.MyComponent", %{},
Expand All @@ -56,14 +59,15 @@ defmodule PhoenixLiveReact do
defp receiver_element(name, props, options) do
attr = Keyword.get(options, :receiver, [])
tag = Keyword.get(options, :receiver_tag, :div)
binding_prefix = Keyword.get(options, :binding_prefix, "phx-")

default_attr = [
style: "display: none;",
data: [
live_react_class: name,
live_react_props: Jason.encode!(props)
],
"phx-hook": "LiveReact"
"#{binding_prefix}hook": "LiveReact"
]

content_tag(tag, "", Keyword.merge(default_attr, attr))
Expand All @@ -72,7 +76,9 @@ defmodule PhoenixLiveReact do
defp container_element(options) do
attr = Keyword.get(options, :container, [])
tag = Keyword.get(options, :container_tag, :div)
default_attr = ["phx-update": "ignore"]
binding_prefix = Keyword.get(options, :binding_prefix, "phx-")

default_attr = ["#{binding_prefix}update": "ignore"]

content_tag(tag, "", Keyword.merge(default_attr, attr))
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule PhoenixLiveReact.MixProject do
def project do
[
app: :phoenix_live_react,
version: "0.2.0",
version: "0.2.1",
elixir: "~> 1.9",
start_permanent: Mix.env() == :prod,
package: package(),
Expand Down

0 comments on commit c9e9c82

Please sign in to comment.