-
Notifications
You must be signed in to change notification settings - Fork 64
Support readonly for all fields #1677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
lib/backpex/html/core_components.ex
Outdated
| </.dropdown> | ||
| """ | ||
| attr :id, :string, required: true, doc: "unique identifier for the dropdown" | ||
| attr :readonly, :boolean, default: false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| attr :readonly, :boolean, default: false | |
| attr :readonly, :boolean, default: false, doc: "whether the dropdown is readonly" |
lib/backpex/html/form.ex
Outdated
| assigns = | ||
| if Map.has_key?(assigns.rest, :disabled) do | ||
| assigns | ||
| else | ||
| put_in(assigns, [:rest, :disabled], Map.get(assigns.rest, :readonly) || false) | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| assigns = | |
| if Map.has_key?(assigns.rest, :disabled) do | |
| assigns | |
| else | |
| put_in(assigns, [:rest, :disabled], Map.get(assigns.rest, :readonly) || false) | |
| end |
Why do we want to set disabled based on the readonly attribute? If you want to set the input to disabled, you should explicitly pass the disabled attribute, in my opinion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://github.com/naymspace/backpex/blob/0.16.3/lib/backpex/fields/text.ex#L58
I noticed that we do not set dsiabled in every field (e.g. Backpex.Fields.Select)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah you're right, I'll review your comments to this PR asap (some time has passed and I can't remember right now). Anyways I was confused by the fact that in Backpex almost all fields have
<BackpexForm.input
...
readonly={@readonly}
disabled={@readonly}
>Do you think it makes sense to have two separate options, :readonly and :disabled? After all, they serve different purposes. If that's the case I can work on that and add the :disabled option to Backpex.Field.
lib/backpex/html/form.ex
Outdated
| assigns = | ||
| if Map.has_key?(assigns.rest, :disabled) do | ||
| assigns | ||
| else | ||
| put_in(assigns, [:rest, :disabled], Map.get(assigns.rest, :readonly) || false) | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| trigger_class = (assigns.trigger && assigns.trigger[:class]) || "" | ||
|
|
||
| trigger_class = | ||
| if assigns.readonly do | ||
| ["cursor-not-allowed bg-base-200"] ++ | ||
| (trigger_class | ||
| |> Enum.join(" ") | ||
| |> String.split() | ||
| |> List.delete("bg-transparent") | ||
| |> List.delete("input")) | ||
| else | ||
| trigger_class | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks complicated. Can't we just add additional classes in the markup in case the dropdown is readonly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is why. This is how the dropdown is rendered with the code above:
Whereas this code:
trigger_class =
if assigns.readonly do
["cursor-not-allowed bg-base-200"] ++ trigger_class
else
trigger_class
endresults in this, and the field can be even highlighted on mouse click (although the dropdown does not open):
| </:label> | ||
| <Form.multi_select | ||
| field={@form[@name]} | ||
| readonly={@readonly} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
undefined attribute "readonly" for component Backpex.HTML.Form.multi_select/1
lib/backpex/html/form.ex
Outdated
| @doc type: :component | ||
|
|
||
| attr :prompt, :string, required: true, doc: "string that will be shown when no option is selected" | ||
| attr :readonly, :boolean, default: false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| attr :readonly, :boolean, default: false | |
| attr :readonly, :boolean, default: false, doc: "whether the dropdown is readonly" |
| assigns = | ||
| if assigns[:readonly] == true do | ||
| assigns | ||
| else | ||
| assign(assigns, :readonly, Backpex.Field.readonly?(field_options, assigns)) | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For which cases do we need this?
And can we simplify the if-condition?
| assigns = | |
| if assigns[:readonly] == true do | |
| assigns | |
| else | |
| assign(assigns, :readonly, Backpex.Field.readonly?(field_options, assigns)) | |
| end | |
| assigns = | |
| if assigns[:readonly] do | |
| assigns | |
| else | |
| assign(assigns, :readonly, Backpex.Field.readonly?(field_options, assigns)) | |
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, the if condition can be simplified.
The reason for this code is because the :readonly assign is not always defined when calling the callback Backpex.Field.render_form/1 and also to allow individual fields in Backpex.Fields.InlineCRUD to be set readonly when the parent InlineCRUD field is not readonly. I will add a comment in the code to clarify this.
The `:readonly` field option, being shared among all fields, has been moved to the `@config_schema` of `Backpex.Field`.
30ef1e3 to
c2ab647
Compare
Support
:readonlyoption for all fields.The
:readonlyfield option, being shared among all fields, has been moved to the@config_schemaofBackpex.Field. This should not be a breaking change.Example of readonly fields: