-
-
Notifications
You must be signed in to change notification settings - Fork 224
/
edit_component.html.erb
131 lines (131 loc) · 5.03 KB
/
edit_component.html.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<% if is_polymorphic? %>
<div class="divide-y"
data-controller="belongs-to-field"
data-searchable="<%= @field.is_searchable? %>"
data-association="<%= @field.id %>"
data-association-class="<%= @field&.target_resource&.model_class || nil %>"
>
<%= field_wrapper **field_wrapper_args, help: @field.polymorphic_help || '' do %>
<%= @form.select @field.type_input_foreign_key, @field.types.map { |type| [Avo.resource_manager.get_resource_by_model_class(type.to_s).name, type.to_s] },
{
value: @field.value,
include_blank: @field.placeholder,
},
{
class: classes("w-full"),
data: {
**@field.get_html(:data, view: view, element: :input),
action: "change->belongs-to-field#changeType #{field_html_action}",
'belongs-to-field-target': "select",
},
disabled: disabled
}
%>
<%
# If the select field is disabled, no value will be sent. It's how HTML works.
# Thus the extra hidden field to actually send the related id to the server.
if disabled %>
<%= @form.hidden_field @field.type_input_foreign_key %>
<% end %>
<% end %>
<% @field.types.each do |type| %>
<div class="hidden"
data-belongs-to-field-target="type"
data-type="<%= type %>"
>
<%= field_wrapper **field_wrapper_args.merge!(data: reload_data), label: Avo.resource_manager.get_resource_by_model_class(type.to_s).name do %>
<% if @field.is_searchable? %>
<%= render Avo::Pro::SearchableAssociations::AutocompleteComponent.new form: @form,
disabled: disabled,
field: @field,
foreign_key: @field.id_input_foreign_key,
model_key: model_keys[type.to_s],
polymorphic_record: polymorphic_record,
resource: @resource,
style: @field.get_html(:style, view: view, element: :input),
type: type,
classes: classes("w-full"),
view: view
%>
<% else %>
<%= @form.select @field.id_input_foreign_key,
options_for_select(@field.values_for_type(type), @resource.present? && @resource.record.present? ? @resource.record[@field.id_input_foreign_key] : nil),
{
value: @resource.record[@field.id_input_foreign_key].to_s,
include_blank: @field.placeholder,
},
{
class: classes("w-full"),
data: @field.get_html(:data, view: view, element: :input),
disabled: disabled
}
%>
<%
# If the select field is disabled, no value will be sent. It's how HTML works.
# Thus the extra hidden field to actually send the related id to the server.
if disabled %>
<%= @form.hidden_field @field.id_input_foreign_key %>
<% end %>
<% end %>
<% if field.can_create? %>
<% create_href = create_path(Avo.resource_manager.get_resource_by_model_class(type.to_s)) %>
<% if !disabled && create_href.present? %>
<%= link_to t("avo.create_new_item", item: type.model_name.human.downcase),
create_href,
class: "text-sm",
data: {
turbo_prefetch: false
}
%>
<% end %>
<% end %>
<% end %>
</div>
<% end %>
</div>
<% else %>
<%= field_wrapper **field_wrapper_args.merge!(data: reload_data) do %>
<% if @field.is_searchable? %>
<%= render Avo::Pro::SearchableAssociations::AutocompleteComponent.new form: @form,
field: @field,
model_key: @field.target_resource&.model_key,
foreign_key: @field.id_input_foreign_key,
resource: @resource,
disabled: disabled,
classes: classes("w-full"),
view: view,
style: @field.get_html(:style, view: view, element: :input)
%>
<% else %>
<%= @form.select @field.id_input_foreign_key, @field.options,
{
include_blank: @field.placeholder,
value: @field.value
},
{
class: classes("w-full"),
data: @field.get_html(:data, view: view, element: :input),
disabled: disabled,
style: @field.get_html(:style, view: view, element: :input)
}
%>
<%
# If the select field is disabled, no value will be sent. It's how HTML works.
# Thus the extra hidden field to actually send the related id to the server.
if disabled %>
<%= @form.hidden_field @field.id_input_foreign_key %>
<% end %>
<% end %>
<% if field.can_create? %>
<% if !disabled && create_path.present? %>
<%= link_to t("avo.create_new_item", item: @field.name.downcase),
create_path,
class: "text-sm",
data: {
turbo_prefetch: false
}
%>
<% end %>
<% end %>
<% end %>
<% end %>