Class: Views::Docs::Form
- Defined in:
- lib/ruby_ui/form/form_docs.rb
Instance Method Summary collapse
Methods inherited from Base
#Alert, #AlertDescription, #AlertTitle, #Heading, #InlineCode, #InlineLink, #Text, #component_files, #docs_accordion_path, #docs_alert_dialog_path, #docs_alert_path, #docs_aspect_ratio_path, #docs_avatar_path, #docs_badge_path, #docs_installation_path, #docs_separator_path, #docs_sheet_path
Instance Method Details
#view_template ⇒ Object
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/ruby_ui/form/form_docs.rb', line 4 def view_template component = "Form" div(class: "max-w-2xl mx-auto w-full py-10 space-y-10") do render Docs::Header.new(title: "Form", description: "Building forms with built-in client-side validations.") Heading(level: 2) { "Usage" } render Docs::VisualCodeExample.new(title: "Example", context: self) do <<~RUBY Form(class: "w-2/3 space-y-6") do FormField do FormFieldLabel { "Default error" } Input(placeholder: "Joel Drapper", required: true, minlength: "3") { "Joel Drapper" } FormFieldHint() FormFieldError() end Button(type: "submit") { "Save" } end RUBY end render Docs::VisualCodeExample.new(title: "Disabled", context: self) do <<~RUBY FormField do FormFieldLabel { "Disabled" } Input(disabled: true, placeholder: "Joel Drapper", required: true, minlength: "3") { "Joel Drapper" } end RUBY end render Docs::VisualCodeExample.new(title: "Aria Disabled", context: self) do <<~RUBY FormField do FormFieldLabel { "Aria Disabled" } Input(aria: {disabled: "true"}, placeholder: "Joel Drapper", required: true, minlength: "3") { "Joel Drapper" } end RUBY end render Docs::VisualCodeExample.new(title: "Custom error message", context: self) do <<~RUBY Form(class: "w-2/3 space-y-6") do FormField do FormFieldLabel { "Custom error message" } Input(placeholder: "joel@drapper.me", required: true, data_value_missing: "Custom error message") FormFieldError() end Button(type: "submit") { "Save" } end RUBY end render Docs::VisualCodeExample.new(title: "Backend error", context: self) do <<~RUBY Form(class: "w-2/3 space-y-6") do FormField do FormFieldLabel { "Backend error" } Input(placeholder: "Joel Drapper", required: true) FormFieldError { "Error from backend" } end Button(type: "submit") { "Save" } end RUBY end render Docs::VisualCodeExample.new(title: "Checkbox", context: self) do <<~RUBY Form(class: "w-2/3 space-y-6") do FormField do Checkbox(required: true) label( class: "text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70" ) { " Accept terms and conditions " } FormFieldError() end Button(type: "submit") { "Save" } end RUBY end render Docs::VisualCodeExample.new(title: "Select", context: self) do <<~RUBY Form(class: "w-2/3 space-y-6") do FormField do FormFieldLabel { "Select" } Select do SelectInput(required: true) SelectTrigger do SelectValue(placeholder: "Select a fruit") end SelectContent() do SelectGroup do SelectLabel { "Fruits" } SelectItem(value: "apple") { "Apple" } SelectItem(value: "orange") { "Orange" } SelectItem(value: "banana") { "Banana" } SelectItem(value: "watermelon") { "Watermelon" } end end end FormFieldError() end Button(type: "submit") { "Save" } end RUBY end render Docs::VisualCodeExample.new(title: "Combobox", context: self) do <<~RUBY Form(class: "w-2/3 space-y-6") do FormField do FormFieldLabel { "Combobox" } Combobox do ComboboxTrigger placeholder: "Pick value" ComboboxPopover do ComboboxSearchInput(placeholder: "Pick value or type anything") ComboboxList do ComboboxEmptyState { "No result" } ComboboxListGroup label: "Fruits" do ComboboxItem do ComboboxRadio(name: "food", value: "apple", required: true) span { "Apple" } end ComboboxItem do ComboboxRadio(name: "food", value: "banana", required: true) span { "Banana" } end end ComboboxListGroup label: "Vegetable" do ComboboxItem do ComboboxRadio(name: "food", value: "brocoli", required: true) span { "Broccoli" } end ComboboxItem do ComboboxRadio(name: "food", value: "carrot", required: true) span { "Carrot" } end end ComboboxListGroup label: "Others" do ComboboxItem do ComboboxRadio(name: "food", value: "chocolate", required: true) span { "Chocolate" } end ComboboxItem do ComboboxRadio(name: "food", value: "milk", required: true) span { "Milk" } end end end end end FormFieldError() end Button(type: "submit") { "Save" } end RUBY end Heading(level: 2) { "Rails Integration" } Text do plain "RubyUI Form components are plain HTML — they work with any form submission strategy. " plain "The recommended approach for Rails apps is to use " InlineLink(href: "https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_with", target: "_blank") { "form_with" } plain " to generate the " code(class: "font-mono text-sm") { "action" } plain " URL and CSRF token, then pass explicit " code(class: "font-mono text-sm") { "name" } plain " / " code(class: "font-mono text-sm") { "id" } plain " attributes to each RubyUI input so the browser serialises them correctly. " plain "Server-side errors can be surfaced by rendering " code(class: "font-mono text-sm") { "FormFieldError" } plain " with content from " code(class: "font-mono text-sm") { "model.errors.full_messages_for(:attr)" } plain "." end Heading(level: 3) { "Minimal Rails form" } Codeblock(<<~RUBY, syntax: :ruby) # In your Phlex view, call form_with via helpers: # form_with(url: users_path, method: :post) passes action + CSRF automatically. # # You can also set action and the CSRF token manually: Form(action: helpers.users_path, method: "post", class: "w-2/3 space-y-6") do input(type: "hidden", name: "authenticity_token", value: helpers.form_authenticity_token) FormField do FormFieldLabel(for: "user_email") { "Email" } Input( type: "email", id: "user_email", name: "user[email]", placeholder: "you@example.com", required: true ) FormFieldError() end Button(type: "submit") { "Continue" } end RUBY Heading(level: 3) { "Devise-style login form" } Codeblock(<<~RUBY, syntax: :ruby) # Full sign-in form mirroring Devise session[email] / session[password] params. # Pass backend errors (e.g. "Invalid email or password") into FormFieldError. Form(action: helpers.user_session_path, method: "post", class: "space-y-6") do input(type: "hidden", name: "authenticity_token", value: helpers.form_authenticity_token) FormField do FormFieldLabel(for: "session_email") { "Email" } Input( type: "email", id: "session_email", name: "session[email]", placeholder: "you@example.com", autocomplete: "email", required: true ) FormFieldError { @error_message } end FormField do FormFieldLabel(for: "session_password") { "Password" } Input( type: "password", id: "session_password", name: "session[password]", autocomplete: "current-password", required: true, minlength: "8" ) FormFieldError() end FormField do div(class: "flex items-center gap-2") do Checkbox(id: "session_remember_me", name: "session[remember_me]", value: "1") FormFieldLabel(for: "session_remember_me") { "Remember me" } end end Button(type: "submit", class: "w-full") { "Sign in" } end RUBY render Components::ComponentSetup::Tabs.new(component_name: component) render Docs::ComponentsTable.new(component_files(component)) end end |