Skip to content

Commit c2ab647

Browse files
committed
Support readonly for all fields: changes for PR review
1 parent 21cfefe commit c2ab647

9 files changed

Lines changed: 10 additions & 18 deletions

File tree

lib/backpex/fields/belongs_to.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ defmodule Backpex.Fields.BelongsTo do
144144
options={@options}
145145
prompt={@prompt}
146146
readonly={@readonly}
147+
disabled={@readonly}
147148
translate_error_fun={Backpex.Field.translate_error_fun(@field_options, assigns)}
148149
help_text={Backpex.Field.help_text(@field_options, assigns)}
149150
phx-debounce={Backpex.Field.debounce(@field_options, assigns)}

lib/backpex/fields/boolean.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ defmodule Backpex.Fields.Boolean do
4242
<BackpexForm.input
4343
type="toggle"
4444
field={@form[@name]}
45-
readonly={@readonly}
45+
disabled={@readonly}
4646
translate_error_fun={Backpex.Field.translate_error_fun(@field_options, assigns)}
4747
help_text={Backpex.Field.help_text(@field_options, assigns)}
4848
phx-debounce={Backpex.Field.debounce(@field_options, assigns)}

lib/backpex/fields/currency.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ defmodule Backpex.Fields.Currency do
9999
type="text"
100100
field={@form[@name]}
101101
readonly={@readonly}
102+
disabled={@readonly}
102103
translate_error_fun={Backpex.Field.translate_error_fun(@field_options, assigns)}
103104
help_text={Backpex.Field.help_text(@field_options, assigns)}
104105
phx-debounce={Backpex.Field.debounce(@field_options, assigns)}

lib/backpex/fields/inline_crud.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ defmodule Backpex.Fields.InlineCRUD do
180180
<div class={if f_nested.index == 0, do: "mt-5", else: nil}>
181181
<label for={"#{@name}-checkbox-delete-#{f_nested.index}"}>
182182
<input
183+
:if={not @readonly}
183184
id={"#{@name}-checkbox-delete-#{f_nested.index}"}
184185
type="checkbox"
185186
name={"change[#{@name}_delete][]"}

lib/backpex/fields/select.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ defmodule Backpex.Fields.Select do
7878
options={@options}
7979
prompt={@prompt}
8080
readonly={@readonly}
81+
disabled={@readonly}
8182
translate_error_fun={Backpex.Field.translate_error_fun(@field_options, assigns)}
8283
help_text={Backpex.Field.help_text(@field_options, assigns)}
8384
phx-debounce={Backpex.Field.debounce(@field_options, assigns)}

lib/backpex/fields/url.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ defmodule Backpex.Fields.URL do
6363
phx-throttle={Backpex.Field.throttle(@field_options, assigns)}
6464
aria-labelledby={Map.get(assigns, :aria_labelledby)}
6565
readonly={@readonly}
66+
disabled={@readonly}
6667
/>
6768
</Layout.field_container>
6869
</div>

lib/backpex/html/core_components.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ defmodule Backpex.HTML.CoreComponents do
3838
</.dropdown>
3939
"""
4040
attr :id, :string, required: true, doc: "unique identifier for the dropdown"
41-
attr :readonly, :boolean, default: false
41+
attr :readonly, :boolean, default: false, doc: "whether the dropdown is readonly"
4242
attr :class, :any, default: nil, doc: "additional classes for the outer container element"
4343

4444
slot :trigger, doc: "the trigger element to be used to toggle the dropdown menu" do

lib/backpex/html/form.ex

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,6 @@ defmodule Backpex.HTML.Form do
4747
slot :inner_block
4848

4949
def input(%{field: %Phoenix.HTML.FormField{} = field} = assigns) do
50-
assigns =
51-
if Map.has_key?(assigns.rest, :disabled) do
52-
assigns
53-
else
54-
put_in(assigns, [:rest, :disabled], Map.get(assigns.rest, :readonly) || false)
55-
end
56-
5750
assigns
5851
|> prepare_field_assigns(field, assigns.translate_error_fun)
5952
|> assign_new(:name, fn -> if assigns.multiple, do: field.name <> "[]", else: field.name end)
@@ -212,13 +205,6 @@ defmodule Backpex.HTML.Form do
212205
multiple pattern placeholder readonly required rows size step)
213206

214207
def currency_input(%{field: %Phoenix.HTML.FormField{} = field} = assigns) do
215-
assigns =
216-
if Map.has_key?(assigns.rest, :disabled) do
217-
assigns
218-
else
219-
put_in(assigns, [:rest, :disabled], Map.get(assigns.rest, :readonly) || false)
220-
end
221-
222208
assigns
223209
|> prepare_field_assigns(field, assigns.translate_error_fun)
224210
|> assign_new(:name, fn -> field.name end)
@@ -300,7 +286,7 @@ defmodule Backpex.HTML.Form do
300286
@doc type: :component
301287

302288
attr :prompt, :string, required: true, doc: "string that will be shown when no option is selected"
303-
attr :readonly, :boolean, default: false
289+
attr :readonly, :boolean, default: false, doc: "whether the dropdown is readonly"
304290
attr :help_text, :string, default: nil, doc: "help text to be displayed below input"
305291
attr :not_found_text, :string, required: true, doc: "string that will be shown when there are no options"
306292
attr :options, :list, required: true, doc: "a list of options for the select"

lib/backpex/html/resource.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ defmodule Backpex.HTML.Resource do
176176
|> assign(:field_options, field_options)
177177
|> assign(:type, :form)
178178

179+
# this is needed to apply `:readonly` to individual fields in `Backpex.Fields.InlineCRUD`
179180
assigns =
180-
if assigns[:readonly] == true do
181+
if assigns[:readonly] do
181182
assigns
182183
else
183184
assign(assigns, :readonly, Backpex.Field.readonly?(field_options, assigns))

0 commit comments

Comments
 (0)