Skip to content

Commit e94dca3

Browse files
committed
Update docs and upgrade guide
1 parent 693bbaf commit e94dca3

File tree

2 files changed

+66
-34
lines changed

2 files changed

+66
-34
lines changed

guides/fields/readonly.md

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Readonly
22

3-
Fields can be configured to be readonly. In edit view, these fields are rendered with the additional HTML attributes `readonly` and `disabled`, ensuring that the user cannot interact with the field or change its value.
3+
Fields can be configured to be readonly. In edit view, these fields are rendered with the additional HTML attributes `readonly` and `disabled`, ensuring that users cannot interact with the field or change its value.
44

5-
In index view, if readonly and index editable is set to `true`, forms will be rendered with the `readonly` HTML attribute.
5+
In index view, if readonly and index editable are both set to true, forms will be rendered with the `readonly` HTML attribute.
66

77
## Supported fields
88

9-
On index view, read-only is supported for all fields with the index editable option (see [Index Edit](index-edit.md)).
9+
On index view, readonly is supported for all fields with the index editable option (see [Index Edit](index-edit.md)).
1010

11-
On edit view, read-only is supported for:
11+
On edit view, readonly is supported for:
1212
- `Backpex.Fields.Date`
1313
- `Backpex.Fields.DateTime`
1414
- `Backpex.Fields.Number`
@@ -17,19 +17,19 @@ On edit view, read-only is supported for:
1717

1818
## Configuration
1919

20-
To enable read-only for a field, you need to set the `readonly` option to `true` in the field configuration. This key must contain either a boolean value or a function that returns a boolean value.
20+
To enable readonly for a field, you need to set the `readonly` option to true in the field configuration. This key must contain either a boolean value or a function that returns a boolean value.
2121

2222
```elixir
2323
# in your resource configuration file
2424
def fields do
2525
[
26-
rating: %{
27-
module: Backpex.Fields.Text,
28-
label: "Rating",
29-
readonly: fn assigns ->
30-
assigns.current_user.role in [:employee]
31-
end
32-
}
26+
rating: %{
27+
module: Backpex.Fields.Text,
28+
label: "Rating",
29+
readonly: fn assigns ->
30+
assigns.current_user.role in [:employee]
31+
end
32+
}
3333
]
3434
end
3535
```
@@ -38,41 +38,54 @@ end
3838
# in your resource configuration file
3939
def fields do
4040
[
41-
rating: %{
42-
module: Backpex.Fields.Text,
43-
label: "Rating",
44-
readonly: true
45-
}
41+
rating: %{
42+
module: Backpex.Fields.Text,
43+
label: "Rating",
44+
readonly: true
45+
}
4646
]
4747
end
4848
```
4949

5050
## Readonly for custom fields
5151

52-
You can also add readonly functionality to a custom field. To do this, you need to define a [`render_form_readonly/1`](Backpex.Field.html#c:render_form_readonly/1) function. This function must return markup to be used when readonly is enabled.
52+
You can also add readonly functionality to a custom field. To do this, you need to handle the readonly state in the `c:Backpex.Field.render_form/1` function of your custom field. You can access the readonly value from the assigns, which will be `true` or `false`.
5353

5454
```elixir
5555
@impl Backpex.Field
56-
def render_form_readonly(assigns) do
56+
def render_form(assigns) do
5757
~H"""
5858
<div>
59-
<Layout.field_container>
60-
<:label>
61-
<Layout.input_label text={@field[:label]} />
62-
</:label>
63-
<BackpexForm.input
64-
type="text"
65-
field={@form[@name]}
66-
translate_error_fun={Backpex.Field.translate_error_fun(@field_options, assigns)}
67-
phx-debounce={Backpex.Field.debounce(@field_options, assigns)}
68-
phx-throttle={Backpex.Field.throttle(@field_options, assigns)}
69-
readonly
70-
disabled
71-
/>
72-
</Layout.field_container>
59+
<Layout.field_container>
60+
<:label>
61+
<Layout.input_label text={@field[:label]} />
62+
</:label>
63+
<BackpexForm.input
64+
type="text"
65+
field={@form[@name]}
66+
translate_error_fun={Backpex.Field.translate_error_fun(@field_options, assigns)}
67+
phx-debounce={Backpex.Field.debounce(@field_options, assigns)}
68+
phx-throttle={Backpex.Field.throttle(@field_options, assigns)}
69+
readonly={@readonly}
70+
disabled={@readonly}
71+
/>
72+
</Layout.field_container>
7373
</div>
7474
"""
7575
end
7676
```
7777

78-
When defining a custom field with index editable support, you need to handle the readonly state in the index editable markup. There is a `readonly` value in the assigns, which will be `true` or `false`.
78+
If your readonly logic is more complex, you can also use a dedicated function that returns the markup for the readonly state.
79+
80+
```elixir
81+
@impl Backpex.Field
82+
def render_form(%{readonly: true} = assigns) do
83+
# Return readonly markup
84+
end
85+
86+
def render_form(%{readonly: false} = assigns) do
87+
# Return editable markup
88+
end
89+
```
90+
91+
When defining a custom field with index editable support, you need to handle the readonly state in `c:Backpex.Field.render_index_form/1`. There is also a readonly value in the assigns, which will be `true` or `false`.

guides/upgrading/v0.14.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,22 @@ def changeset(change, attrs, _metadata) do
8181
|> Ecto.Changeset.validate_required([:field1])
8282
end
8383
```
84+
85+
## `render_form_readonly/1` callback has been removed
86+
87+
We removed the `render_form_readonly/1` callback from fields. Instead, `readonly` must be handled directly in the `c:Backpex.Field.render_form/1` callback.
88+
89+
Make sure to update your custom fields accordingly. The `readonly` value will be available in the assigns, which will be `true` or `false`.
90+
91+
```elixir
92+
@impl Backpex.Field
93+
def render_form(%{readonly: true} = assigns) do
94+
# render readonly field
95+
end
96+
97+
def render_form(%{readonly: false} = assigns) do
98+
# render editable field
99+
end
100+
```
101+
102+
See the [Readonly guide](readonly.md) for more details.

0 commit comments

Comments
 (0)