Module: LesliView::Forms::Fields

Included in:
Builder
Defined in:
lib/lesli_view/forms/fields.rb

Instance Method Summary collapse

Instance Method Details

#field_control(attribute, label: nil, message: nil, category: nil, icon: nil, horizontal: false) ⇒ Object



4
5
6
# File 'lib/lesli_view/forms/fields.rb', line 4

def field_control(attribute, label: nil, message:nil, category:nil, icon:nil, horizontal: false)
    field_control_text(attribute, label:label, message:message, category:category, icon:icon, horizontal:horizontal)
end

#field_control_builder(label_html: nil, control_html: nil, message_text: nil, horizontal: false, category: nil, icon: nil, &block) ⇒ Object



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
# File 'lib/lesli_view/forms/fields.rb', line 90

def field_control_builder(
    label_html:nil, 
    control_html:nil, 
    message_text:nil,
    horizontal:false, 
    category:nil,
    icon:nil, 
    &block
)

    icon_left = icon
    icon_right = nil

    # Conditionally add 'is-horizontal' if horizontal is true
    field_classes = ['field']
    field_classes << 'is-horizontal' if horizontal

    control_classes = ['control']
    control_classes << 'has-icons-left' if icon_left
    control_classes << 'has-icons-right' if icon_right

    help_class = ""

    @template.(:div, class: 'lesli-form-field mb-3') do
        @template.(:div, class: field_classes.join(' ')) do
            @template.(:div, label_html, class: 'field-label is-normal mb-1') +
            @template.(:div, class: 'field-body') do
                @template.(:div, class: 'field') do
                    control_html = @template.(:div, class: control_classes.join(' ')) do
                        content = ''.html_safe
                        content << (block_given? ? @template.capture(&block) : control_html)
                        content << icon_html(icon_left, 'left') if icon_left
                        content << icon_html(icon_right, 'right') if icon_right
                        content
                    end
                    control_html + @template.(:p, message_text, class: "help #{css_category(category)}")
                end
            end
        end
    end
end

#field_control_button(value = nil, options = {}, category: "primary", icon: nil, type: "submit", horizontal: false) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lesli_view/forms/fields.rb', line 73

def field_control_button(value = nil, options = {}, category:"primary", icon:nil, type:"submit", horizontal: false)
    # Merge user-provided classes with default button classes
    button_classes = ["button", "is-#{category}"]

    button_html = @template.(:button, class: button_classes, type: type, **options.except(:class)) do
        html = "".html_safe
        if icon.present?
            html << @template.(:span, class: "icon") do
                @template.(:span, icon, class: "material-symbols")
            end
        end
        html << @template.(:span, value) if value.present?
        html
    end
    field_control_builder(control_html: button_html, icon:nil, horizontal: horizontal)
end

#field_control_checkbox(attribute, label: nil, message: nil, category: nil, icon: nil, horizontal: false) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/lesli_view/forms/fields.rb', line 8

def field_control_checkbox(attribute, label: nil, message:nil, category:nil, icon:nil, horizontal: false)
    label_html = label(attribute, label)
    text_field_html = check_box(attribute, {}, true, false)

    field_control_builder(
        label_html: label_html,
        control_html: text_field_html,
        horizontal: horizontal
    )
end

#field_control_email(attribute, label: nil, message: nil, category: nil, icon: nil, horizontal: false) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lesli_view/forms/fields.rb', line 30

def field_control_email(attribute, label: nil, message:nil, category:nil, icon:nil, horizontal: false)
    label_html = label(attribute, label)
    control_html = email_field(attribute)

    field_control_builder(
        label_html: label_html,
        control_html: control_html,
        horizontal: horizontal,
        icon:icon
    )
end

#field_control_select(attribute, choices, label: nil, message: nil, category: nil, icon: nil, horizontal: false, humanize: true) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lesli_view/forms/fields.rb', line 42

def field_control_select(attribute, choices, label: nil, message:nil, category:nil, icon:nil, horizontal: false, humanize:true)
    choices = choices.map { |k, v| [k.humanize.capitalize, v] } if humanize
    value = @object.send(attribute)
    label_html = label(attribute, label)
    select_html = select(attribute, choices, {}, { value: value })

    field_control_builder(
        label_html: label_html,
        control_html: @template.(:div, select_html, class: "select is-fullwidth"),
        horizontal: horizontal
    )
end

#field_control_submit(value = nil, options = {}, horizontal: false) ⇒ Object



68
69
70
71
# File 'lib/lesli_view/forms/fields.rb', line 68

def field_control_submit(value = nil, options = {}, horizontal: false)
    submit_html = submit(value, options)
    field_control_builder(control_html: submit_html, horizontal: horizontal)
end

#field_control_text(attribute, label: nil, message: nil, category: nil, icon: nil, horizontal: false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/lesli_view/forms/fields.rb', line 19

def field_control_text(attribute, label: nil, message:nil, category:nil, icon:nil, horizontal: false)
    label_html = label(attribute, label)
    text_field_html = text_field(attribute)

    field_control_builder(
        label_html: label_html,
        control_html: text_field_html,
        horizontal: horizontal
    )
end

#field_control_textarea(attribute, label: nil, message: nil, category: nil, icon: nil, horizontal: false) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lesli_view/forms/fields.rb', line 55

def field_control_textarea(attribute, label:nil, message:nil, category:nil, icon:nil, horizontal: false)

    label_html = label == false ? '' : label(attribute, label)
    control_html = rich_textarea(attribute)

    field_control_builder(
        label_html: label_html,
        control_html: control_html,
        horizontal: horizontal,
        icon:icon
    )
end