Skip to content

Commit 30ef1e3

Browse files
committed
Support readonly for all fields
The `:readonly` field option, being shared among all fields, has been moved to the `@config_schema` of `Backpex.Field`.
1 parent 711d0a7 commit 30ef1e3

21 files changed

+94
-43
lines changed

lib/backpex/field.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ defmodule Backpex.Field do
1111
type: :string,
1212
required: true
1313
],
14+
readonly: [
15+
doc: "Sets the field to readonly. Also see the [panels](/guides/fields/readonly.md) guide.",
16+
type: {:or, [:boolean, {:fun, 1}]},
17+
default: false
18+
],
1419
class: [
1520
type: {:or, [:string, {:fun, 1}]},
1621
doc: """

lib/backpex/fields/belongs_to.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ defmodule Backpex.Fields.BelongsTo do
143143
field={@form[@owner_key]}
144144
options={@options}
145145
prompt={@prompt}
146+
readonly={@readonly}
146147
translate_error_fun={Backpex.Field.translate_error_fun(@field_options, assigns)}
147148
help_text={Backpex.Field.help_text(@field_options, assigns)}
148149
phx-debounce={Backpex.Field.debounce(@field_options, assigns)}

lib/backpex/fields/boolean.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ defmodule Backpex.Fields.Boolean do
4242
<BackpexForm.input
4343
type="toggle"
4444
field={@form[@name]}
45+
readonly={@readonly}
4546
translate_error_fun={Backpex.Field.translate_error_fun(@field_options, assigns)}
4647
help_text={Backpex.Field.help_text(@field_options, assigns)}
4748
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
@@ -98,6 +98,7 @@ defmodule Backpex.Fields.Currency do
9898
<BackpexForm.currency_input
9999
type="text"
100100
field={@form[@name]}
101+
readonly={@readonly}
101102
translate_error_fun={Backpex.Field.translate_error_fun(@field_options, assigns)}
102103
help_text={Backpex.Field.help_text(@field_options, assigns)}
103104
phx-debounce={Backpex.Field.debounce(@field_options, assigns)}

lib/backpex/fields/date.ex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ defmodule Backpex.Fields.Date do
1717
throttle: [
1818
doc: "Timeout value (in milliseconds) or function that receives the assigns.",
1919
type: {:or, [:pos_integer, {:fun, 1}]}
20-
],
21-
readonly: [
22-
doc: "Sets the field to readonly. Also see the [panels](/guides/fields/readonly.md) guide.",
23-
type: {:or, [:boolean, {:fun, 1}]}
2420
]
2521
]
2622

lib/backpex/fields/date_time.ex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ defmodule Backpex.Fields.DateTime do
1717
throttle: [
1818
doc: "Timeout value (in milliseconds) or function that receives the assigns.",
1919
type: {:or, [:pos_integer, {:fun, 1}]}
20-
],
21-
readonly: [
22-
doc: "Sets the field to readonly. Also see the [panels](/guides/fields/readonly.md) guide.",
23-
type: {:or, [:boolean, {:fun, 1}]}
2420
]
2521
]
2622

lib/backpex/fields/email.ex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ defmodule Backpex.Fields.Email do
1111
throttle: [
1212
doc: "Timeout value (in milliseconds) or function that receives the assigns.",
1313
type: {:or, [:pos_integer, {:fun, 1}]}
14-
],
15-
readonly: [
16-
doc: "Sets the field to readonly. Also see the [panels](/guides/fields/readonly.md) guide.",
17-
type: {:or, [:boolean, {:fun, 1}]}
1814
]
1915
]
2016

lib/backpex/fields/has_many.ex

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ defmodule Backpex.Fields.HasMany do
144144
<Layout.input_label as="span" text={@field_options[:label]} />
145145
</:label>
146146
147-
<Backpex.HTML.CoreComponents.dropdown id={"has-many-dropdown-#{@name}"} class="w-full">
147+
<Backpex.HTML.CoreComponents.dropdown id={"has-many-dropdown-#{@name}"} class="w-full" readonly={@readonly}>
148148
<:trigger
149149
class={[
150150
"input block h-fit w-full p-2",
@@ -154,12 +154,13 @@ defmodule Backpex.Fields.HasMany do
154154
aria_labelledby={Map.get(assigns, :aria_labelledby)}
155155
>
156156
<div class="flex h-full w-full flex-wrap items-center gap-1 px-2">
157-
<p :if={@selected == []} class="p-0.5 text-sm">{@prompt}</p>
157+
<p :if={@selected == []} class={["p-0.5 text-sm", @readonly && "text-base-content/40"]}>{@prompt}</p>
158158
<.badge
159159
:for={{label, value} <- @selected}
160160
live_resource={@live_resource}
161161
label={label}
162162
value={value}
163+
readonly={@readonly}
163164
name={@name}
164165
/>
165166
</div>
@@ -302,10 +303,17 @@ defmodule Backpex.Fields.HasMany do
302303
end
303304

304305
attr :live_resource, :atom, required: true
306+
attr :readonly, :boolean, default: false
305307
attr :name, :string, required: true
306308
attr :label, :string, required: true
307309
attr :value, :string, required: true
308310

311+
defp badge(%{readonly: true} = assigns) do
312+
~H"""
313+
<span class="badge badge-sm badge-soft">{@label}</span>
314+
"""
315+
end
316+
309317
defp badge(assigns) do
310318
~H"""
311319
<div class="badge badge-sm badge-soft badge-primary pointer-events-auto pr-0">

lib/backpex/fields/has_many_through.ex

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ defmodule Backpex.Fields.HasManyThrough do
259259
>
260260
{label}
261261
</th>
262-
<th>
262+
<th :if={not @readonly}>
263263
<span class="sr-only">{Backpex.__("Actions", @live_resource)}</span>
264264
</th>
265265
</tr>
@@ -289,7 +289,7 @@ defmodule Backpex.Fields.HasManyThrough do
289289
{assigns}
290290
/>
291291
</td>
292-
<td>
292+
<td :if={not @readonly}>
293293
<div class="flex items-center space-x-2">
294294
<button
295295
class="cursor-pointer"
@@ -360,7 +360,13 @@ defmodule Backpex.Fields.HasManyThrough do
360360
</div>
361361
</.modal>
362362
363-
<button type="button" class="btn btn-sm btn-outline btn-primary" phx-click="new-relational" phx-target={@myself}>
363+
<button
364+
disabled={@readonly}
365+
type="button"
366+
class="btn btn-sm btn-outline btn-primary"
367+
phx-click="new-relational"
368+
phx-target={@myself}
369+
>
364370
{@relational_title}
365371
</button>
366372

lib/backpex/fields/inline_crud.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ defmodule Backpex.Fields.InlineCRUD do
187187
class="hidden"
188188
/>
189189
190-
<div class="btn btn-outline btn-error">
190+
<div class={["btn btn-outline btn-error", @readonly && "btn-disabled"]}>
191191
<span class="sr-only">{Backpex.__("Delete", @live_resource)}</span>
192192
<Backpex.HTML.CoreComponents.icon name="hero-trash" class="size-5" />
193193
</div>
@@ -199,6 +199,7 @@ defmodule Backpex.Fields.InlineCRUD do
199199
<input type="hidden" name={"change[#{@name}_delete][]"} tabindex="-1" aria-hidden="true" />
200200
</div>
201201
<input
202+
disabled={@readonly}
202203
name={"change[#{@name}_order][]"}
203204
type="checkbox"
204205
aria-label={Backpex.__("Add entry", @live_resource)}

0 commit comments

Comments
 (0)