Skip to content

Commit

Permalink
Merge pull request #31 from dwyl/input-default-opts
Browse files Browse the repository at this point in the history
Input default opts
  • Loading branch information
nelsonic authored Dec 4, 2018
2 parents 67c75aa + 1208aab commit fd5b73f
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lib/views/custom_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ defmodule Autoform.CustomView do
Phoenix.HTML.Form.input_type(form, field)
end

# Apply any sensible defaults here, but we should allow configuration of individual input options
opts =
Keyword.merge(
opts,
case schema.__schema__(:type, field) do
:float -> [step: 0.01]
_ -> []
end
)

apply(Phoenix.HTML.Form, type, [String.to_existing_atom(name), field, opts])
end
end
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 Autoform.MixProject do
def project do
[
app: :autoform,
version: "0.4.0",
version: "0.5.0",
elixir: "~> 1.7",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down
12 changes: 12 additions & 0 deletions test/custom_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,16 @@ defmodule CustomTest do
assert response =~ "textarea"
end
end

describe "Product" do
test "renders number input with step of 0.01", %{conn: conn} do
assert response =
conn
|> get(custom_path(conn, :new_product))
|> html_response(200)

assert response =~ "type=\"number\""
assert response =~ "step=\"0.01\""
end
end
end
17 changes: 17 additions & 0 deletions test/support/test_autoform/lib/test_autoform/product.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule TestAutoform.Product do
use Ecto.Schema
import Ecto.Changeset

schema "products" do
field(:price, :float)

timestamps()
end

@doc false
def changeset(product, attrs) do
product
|> cast(attrs, [:price])
|> validate_required([:price])
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule TestAutoformWeb.CustomController do
use TestAutoformWeb, :controller
alias TestAutoform.Address
alias TestAutoform.{Address, Product}

def new(conn, _params) do
changeset = Address.changeset(%Address{}, %{})
Expand All @@ -13,4 +13,10 @@ defmodule TestAutoformWeb.CustomController do

render(conn, "new_no_path.html", changeset: changeset)
end

def new_product(conn, _params) do
changeset = Product.changeset(%Product{}, %{})

render(conn, "new_product.html", changeset: changeset)
end
end
1 change: 1 addition & 0 deletions test/support/test_autoform/lib/test_autoform_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule TestAutoformWeb.Router do
resources("/addresses", AddressController)
resources("/custom", CustomController)
get("/custom_no_path", CustomController, :new_no_path)
get("/custom_new_product", CustomController, :new_product)
end

# Other scopes may use custom stacks.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= custom_render_autoform(@conn, :create, [TestAutoform.Product]) %>

0 comments on commit fd5b73f

Please sign in to comment.