-
Notifications
You must be signed in to change notification settings - Fork 147
/
multiple_select.ex
58 lines (42 loc) · 1.42 KB
/
multiple_select.ex
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
defmodule Surface.Components.Form.MultipleSelect do
@moduledoc """
Defines a select.
Provides a wrapper for Phoenix.HTML.Form's `multiple_select/4` function.
All options passed via `opts` will be sent to `multiple_select/4`, `class` can
be set directly and will override anything in `opts`.
"""
use Surface.Component
import Phoenix.HTML.Form, only: [multiple_select: 4]
import Surface.Components.Form.Utils
alias Surface.Components.Form.Input.InputContext
@doc "The form identifier"
prop form, :form
@doc "The field name"
prop field, :string
@doc "The id of the corresponding select field"
prop id, :string
@doc "The name of the corresponding select field"
prop name, :string
@doc "The CSS class for the underlying tag"
prop class, :css_class
@doc "The options in the select"
prop options, :any, default: []
@doc "The default selected option"
prop selected, :any
@doc "Options list"
prop opts, :keyword, default: []
def render(assigns) do
helper_opts = props_to_opts(assigns, [:selected])
attr_opts = props_to_attr_opts(assigns, class: get_config(:default_class))
opts =
assigns.opts
|> Keyword.merge(helper_opts)
|> Keyword.merge(attr_opts)
assigns = assign(assigns, opts: opts)
~F"""
<InputContext assigns={assigns} :let={form: form, field: field}>
{multiple_select(form, field, @options, @opts)}
</InputContext>
"""
end
end