Skip to content

Commit ebd3f35

Browse files
committed
add image_preview field
1 parent 734c0a5 commit ebd3f35

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

lib/kaffy/resource_form.ex

+20-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ defmodule Kaffy.ResourceForm do
4949
opts
5050
end
5151

52-
opts = if options[:value], do: Keyword.put(opts, :value_fn, options.value), else: opts
5352
permission =
5453
case is_nil(changeset.data.id) do
5554
true -> Map.get(options, :create, :editable)
@@ -62,6 +61,12 @@ defmodule Kaffy.ResourceForm do
6261
!is_nil(choices) ->
6362
select(form, field, choices, class: "custom-select")
6463

64+
type == :image_preview ->
65+
build_image_preview(changeset.data, options)
66+
67+
type == :custom_html ->
68+
build_custom_html(changeset.data, options)
69+
6570
true ->
6671
build_html_input(changeset.data, form, field, type, opts, permission == :readonly)
6772
end
@@ -72,6 +77,20 @@ defmodule Kaffy.ResourceForm do
7277
build_html_input(changeset.data, form, field, type, opts)
7378
end
7479

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+
7594
defp build_html_input(schema, form, field, type, opts, readonly \\ false) do
7695
data = schema
7796
{conn, opts} = Keyword.pop(opts, :conn)
@@ -198,8 +217,6 @@ defmodule Kaffy.ResourceForm do
198217
:utc_datetime_usec ->
199218
flatpickr_datetime_usec(form, field, opts)
200219

201-
:safe_html ->
202-
{:safe, opts[:value_fn].(form.data)}
203220
_ ->
204221
text_input(form, field, opts)
205222
end

0 commit comments

Comments
 (0)