Skip to content

Commit 4825aa6

Browse files
authored
Merge pull request #1621 from gmazzamuto/inline-crud-all-fields
Add support for most field types in `Backpex.Fields.InlineCRUD`
2 parents ed7727c + 85b5d59 commit 4825aa6

27 files changed

Lines changed: 198 additions & 61 deletions

demo/lib/demo/ecto_factory.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ defmodule Demo.EctoFactory do
5151
def supplier_factory do
5252
%Supplier{
5353
name: Faker.Company.name(),
54-
url: "https://example.com/"
54+
url: "https://example.com/",
55+
country: Enum.random(Supplier.countries()),
56+
contract_date: Faker.Date.backward(365 * 3),
57+
minimum_order: Enum.random([100_000, 200_000, 300_000, 400_000, 500_000]),
58+
preferred: boolean()
5559
}
5660
end
5761

demo/lib/demo/supplier.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,29 @@ defmodule Demo.Supplier do
66

77
@primary_key {:id, :binary_id, autogenerate: true}
88

9+
@countries ["Austria", "France", "Germany", "Italy", "Spain", "Switzerland"]
10+
11+
def countries, do: @countries
12+
913
schema "suppliers" do
1014
field :name, :string
1115
field :url, :string
16+
field :country, Ecto.Enum, values: Enum.map(@countries, &String.to_atom/1)
17+
field :contract_date, :date
18+
field :minimum_order, Money.Ecto.Amount.Type
19+
field :preferred, :boolean, default: false
1220

1321
belongs_to :product, Demo.Product, type: :binary_id
1422

1523
timestamps()
1624
end
1725

1826
@required_fields ~w[name url]a
27+
@optional_fields ~w[country contract_date minimum_order preferred]a
1928

2029
def changeset(supplier, attrs) do
2130
supplier
22-
|> cast(attrs, @required_fields)
31+
|> cast(attrs, @required_fields ++ @optional_fields)
2332
|> validate_required(@required_fields)
2433
end
2534
end

demo/lib/demo_web/live/product_live.ex

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,26 @@ defmodule DemoWeb.ProductLive do
9696
label: "Name"
9797
},
9898
url: %{
99-
module: Backpex.Fields.Text,
99+
module: Backpex.Fields.URL,
100100
label: "URL"
101+
},
102+
country: %{
103+
module: Backpex.Fields.Select,
104+
label: "Country",
105+
prompt: "—",
106+
options: Demo.Supplier.countries()
107+
},
108+
contract_date: %{
109+
module: Backpex.Fields.Date,
110+
label: "Contract Date"
111+
},
112+
minimum_order: %{
113+
module: Backpex.Fields.Currency,
114+
label: "Minimum Order"
115+
},
116+
preferred: %{
117+
module: Backpex.Fields.Boolean,
118+
label: "Preferred"
101119
}
102120
]
103121
},
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
defmodule Demo.Repo.Migrations.AddExtraSuppliersFields do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:suppliers) do
6+
add :country, :string, null: true
7+
add :contract_date, :date, null: true
8+
add :minimum_order, :integer, default: 0, null: false
9+
add :preferred, :boolean, null: true
10+
end
11+
end
12+
end

guides/upgrading/v0.17.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@ end
2121
- `"Toggle menu"`
2222

2323
See the [the backpex.pot file for v0.17.0](https://github.com/naymspace/backpex/blob/0.17.0/priv/gettext/backpex.pot) in our GitHub repository for all available translations to match on.
24+
25+
## Remove `input_type` option for `Backpex.Fields.InlineCRUD`
26+
27+
The `:input_type` option in the child fields of `Backpex.Fields.InlineCRUD` is no longer supported. To update, simply remove that option or you will get a validation error.
28+
29+
With this version of Backpex, `Backpex.Fields.InlineCRUD` supports most field types out of the box.

lib/backpex/field.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ defmodule Backpex.Field do
1111
type: :string,
1212
required: true
1313
],
14+
class: [
15+
type: {:or, [:string, {:fun, 1}]},
16+
doc: """
17+
Class or classes to be applied when the field is used as a child field in `Backpex.Fields.InlineCRUD`. The class
18+
can be a string or a function that takes the assigns and must return a string.
19+
"""
20+
],
1421
help_text: [
1522
doc: "A text to be displayed below the input on form views.",
1623
type: {:or, [:string, {:fun, 1}]}

lib/backpex/fields/belongs_to.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ defmodule Backpex.Fields.BelongsTo do
135135
~H"""
136136
<div>
137137
<Layout.field_container>
138-
<:label align={Backpex.Field.align_label(@field_options, assigns)}>
138+
<:label :if={not @hide_label} align={Backpex.Field.align_label(@field_options, assigns)}>
139139
<Layout.input_label for={@form[@owner_key]} text={@field_options[:label]} />
140140
</:label>
141141
<BackpexForm.input
@@ -147,6 +147,7 @@ defmodule Backpex.Fields.BelongsTo do
147147
help_text={Backpex.Field.help_text(@field_options, assigns)}
148148
phx-debounce={Backpex.Field.debounce(@field_options, assigns)}
149149
phx-throttle={Backpex.Field.throttle(@field_options, assigns)}
150+
aria-labelledby={Map.get(assigns, :aria_labelledby)}
150151
/>
151152
</Layout.field_container>
152153
</div>

lib/backpex/fields/boolean.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ defmodule Backpex.Fields.Boolean do
3636
~H"""
3737
<div>
3838
<Layout.field_container>
39-
<:label align={Backpex.Field.align_label(@field_options, assigns, :top)}>
39+
<:label :if={not @hide_label} align={Backpex.Field.align_label(@field_options, assigns, :top)}>
4040
<Layout.input_label for={@form[@name]} text={@field_options[:label]} />
4141
</:label>
4242
<BackpexForm.input
@@ -46,6 +46,7 @@ defmodule Backpex.Fields.Boolean do
4646
help_text={Backpex.Field.help_text(@field_options, assigns)}
4747
phx-debounce={Backpex.Field.debounce(@field_options, assigns)}
4848
phx-throttle={Backpex.Field.throttle(@field_options, assigns)}
49+
aria-labelledby={Map.get(assigns, :aria_labelledby)}
4950
/>
5051
</Layout.field_container>
5152
</div>

lib/backpex/fields/currency.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ defmodule Backpex.Fields.Currency do
9292
~H"""
9393
<div>
9494
<Layout.field_container>
95-
<:label align={Backpex.Field.align_label(@field_options, assigns)}>
95+
<:label :if={not @hide_label} align={Backpex.Field.align_label(@field_options, assigns)}>
9696
<Layout.input_label for={@form[@name]} text={@field_options[:label]} />
9797
</:label>
9898
<BackpexForm.currency_input
@@ -107,6 +107,7 @@ defmodule Backpex.Fields.Currency do
107107
unit={@field_options[:unit]}
108108
unit_position={@field_options[:unit_position]}
109109
symbol_space={@field_options[:symbol_space]}
110+
aria-labelledby={Map.get(assigns, :aria_labelledby)}
110111
/>
111112
</Layout.field_container>
112113
</div>

lib/backpex/fields/date.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ defmodule Backpex.Fields.Date do
102102
~H"""
103103
<div>
104104
<Layout.field_container>
105-
<:label align={Backpex.Field.align_label(@field_options, assigns, :top)}>
105+
<:label :if={not @hide_label} align={Backpex.Field.align_label(@field_options, assigns, :top)}>
106106
<Layout.input_label for={@form[@name]} text={@field_options[:label]} />
107107
</:label>
108108
<BackpexForm.input
@@ -114,6 +114,7 @@ defmodule Backpex.Fields.Date do
114114
phx-throttle={Backpex.Field.throttle(@field_options, assigns)}
115115
readonly={@readonly}
116116
disabled={@readonly}
117+
aria-labelledby={Map.get(assigns, :aria_labelledby)}
117118
/>
118119
</Layout.field_container>
119120
</div>

0 commit comments

Comments
 (0)