@@ -49,7 +49,6 @@ defmodule Kaffy.ResourceForm do
49
49
opts
50
50
end
51
51
52
- opts = if options [ :value ] , do: Keyword . put ( opts , :value_fn , options . value ) , else: opts
53
52
permission =
54
53
case is_nil ( changeset . data . id ) do
55
54
true -> Map . get ( options , :create , :editable )
@@ -62,6 +61,12 @@ defmodule Kaffy.ResourceForm do
62
61
! is_nil ( choices ) ->
63
62
select ( form , field , choices , class: "custom-select" )
64
63
64
+ type == :image_preview ->
65
+ build_image_preview ( changeset . data , options )
66
+
67
+ type == :custom_html ->
68
+ build_custom_html ( changeset . data , options )
69
+
65
70
true ->
66
71
build_html_input ( changeset . data , form , field , type , opts , permission == :readonly )
67
72
end
@@ -72,6 +77,20 @@ defmodule Kaffy.ResourceForm do
72
77
build_html_input ( changeset . data , form , field , type , opts )
73
78
end
74
79
80
+ defp build_image_preview ( data , opts ) do
81
+ url = extract_value ( data , opts )
82
+ content_tag :img , "" , src: url , style: Map . get ( opts , :inline_css )
83
+ end
84
+
85
+ defp build_custom_html ( data , opts ) do
86
+ value = extract_value ( data , opts )
87
+ { :safe , value }
88
+ end
89
+
90
+ defp extract_value ( data , % { value_fn: fun } ) when is_function ( fun ) , do: fun . ( data )
91
+ defp extract_value ( _data , % { value: value } ) , do: value
92
+ defp extract_value ( _data , _ ) , do: raise ( ":value or :value_fn is missing" )
93
+
75
94
defp build_html_input ( schema , form , field , type , opts , readonly \\ false ) do
76
95
data = schema
77
96
{ conn , opts } = Keyword . pop ( opts , :conn )
@@ -198,8 +217,6 @@ defmodule Kaffy.ResourceForm do
198
217
:utc_datetime_usec ->
199
218
flatpickr_datetime_usec ( form , field , opts )
200
219
201
- :safe_html ->
202
- { :safe , opts [ :value_fn ] . ( form . data ) }
203
220
_ ->
204
221
text_input ( form , field , opts )
205
222
end
0 commit comments