avo icon indicating copy to clipboard operation
avo copied to clipboard

Add the ability to change the view type of a field

Open adrianthedev opened this issue 1 year ago • 4 comments
trafficstars

From here.

Maybe you must use a computed field on an Edit view. You can fake it like in that comment or you could pass in a computed field with the view set to :show. This will render that field as a show field in the edit view.

Example:

Image

Rough PoC:

        <% if main_panel.present? %>
          <% c.with_body do %>
            <div class="divide-y">
              <% main_panel.items.each_with_index do |field, index| %>
              <% v = field.id == :user_label ? :show : view %>
                <%= render field
                  .hydrate(resource: @resource, model: @resource.model, user: @resource.user, view: view_for(field, forced_view: v))
                  .component_for_view(view_for(field, forced_view: v))
                  .new(field: field, resource: @resource, index: index, form: form, compact: sidebar.present?)
                %>
              <% end %>
            </div>
          <% end %>
        <% end %>

adrianthedev avatar Dec 18 '23 10:12 adrianthedev

I'm not so sure about "faking" or forcing views. It can easily get confusing. view.show? can return true on forms etc... Or things that are supposed to work on form views will not work. Having 2 views view and forced_view it's also not ideal...

WDYT about using the build in components feature instead?

      field :name, as: :text, components: {
        edit_component: Avo::Fields::TextField::ShowComponent
      } do
        "Test"
      end

components accepts a proc that ca have some logic and apply this configuration only when user is admin or some other condition.

Also, we can consider adding support for computed fields on form views

Paul-Bob avatar Dec 18 '23 10:12 Paul-Bob

@Paul-Bob this works, but breaks on as: :external_image

RocKhalil avatar Feb 05 '24 19:02 RocKhalil

How does it break?

adrianthedev avatar Feb 05 '24 19:02 adrianthedev

@adrianthedev

undefined method `as_html' for #<Avo::Fields::ExternalImageField:0x0000000115eb2088

RocKhalil avatar Feb 06 '24 08:02 RocKhalil