Giter Site home page Giter Site logo

form_buildable's Introduction

FormBuildable

FormBuildable

Declaratively build FormBuilder classes in Rails. Goes great with TailwindCSS!

Usage

module ApplicationHelper
  include FormBuildable

  define_form_builder :default do
    html_classes do
      button {
        "inline-flex items-center justify-center rounded border shadow-sm font-medium px-4 py-2 text-sm "\
        " border-transparent text-white bg-blue-600 md:text-base lg:text-lg hover:bg-blue-800 "
      }

      form { "space-y-4" }
      field { "flex flex-col gap-2" }

      check_box_input { "w-6 h-6 text-blue-600 bg-gray-100 border-gray-300 rounded" }
      check_box { "flex gap-2 items-center" }
      check_box_label { "text-md" }

      input { "rounded-lg border-slate-300 p-2 text-slate-900 " }

      error_container { "flex justify-start" }
      error { "text-sm text-red-700 font-bold whitespace-pre" }
    end
  end

  define_form_builder :inline do
    # some other classes...
  end
end

Now use in a view with default_form_with or default_form_for

<%= default_form_with model: @user, url: registrations_path do |form| -%>
  <%= form.email_field :email -%>
  <%= form.check_box :accepted_terms -%>
  <%= form.button -%>
<% end -%>

Will produce some HTML like:

<form class="space-y-4" action="/users/registrations" method="post">
  <div class="flex flex-col gap-2">
    <label for="email_email">Email</label>
    <input class="rounded-lg border-slate-300 p-2 text-slate-900" type="email" name="email[email]" id="email_email" />
  </div>
  <div class="flex gap-2 items-center">
    <input name="email[accepted_terms]" type="hidden" value="0" autocomplete="off" />
    <input class="w-6 h-6 text-blue-600 bg-gray-100 border-gray-300 rounded" type="checkbox" value="1" name="email[accepted_terms]" id="email_accepted_terms" />
    <label class="text-md" for="email_accepted_terms">Accepted terms</label>
  </div>
  <button name="button" type="submit" class="inline-flex items-center justify-center rounded border shadow-sm font-medium px-4 py-2 text-sm border-transparent text-white bg-blue-600 md:text-base lg:text-lg hover:bg-blue-800">
    Create User
  </button
</form>

And when there is an error, the model's errors are automatically added to the markup

<form class="space-y-4" action="/registrations" method="post">
  <div class="flex flex-col gap-2">
    <label for="email_address">Address</label><input class="rounded-lg border-slate-300 p-2 text-slate-900" type="email" value="" name="email[address]" id="email_address" />
    <div class="flex justify-start"><p class="text-sm text-red-700 font-bold whitespace-pre" aria-live="polite" id="email-address-error" data-error="email-address-error">Address is invalid</p></div>
  </div>
  <div class="flex gap-2 items-center">
    <input name="email[accepted_terms]" type="hidden" value="0" autocomplete="off" />
    <input class="w-6 h-6 text-blue-600 bg-gray-100 border-gray-300 rounded" type="checkbox" value="1" name="email[accepted_terms]" id="email_accepted_terms" /><label class="text-md" for="email_accepted_terms">Accepted terms</label>
  </div>
  <div class="flex justify-start"><p class="text-sm text-red-700 font-bold whitespace-pre" aria-live="polite" id="email-accepted_terms-error" data-error="email-accepted_terms-error">Accepted terms must be accepted</p></div>
  <button name="button" type="submit" class="inline-flex items-center justify-center rounded border shadow-sm font-medium px-4 py-2 text-sm border-transparent text-white bg-blue-600 md:text-base lg:text-lg hover:bg-blue-800">
    Create Email
  </button>
</form>

TODO

So much! Maybe?

  • Radio buttons, selects, and other fields
  • Customize HTML of wrapper methods
  • Customize error and label HTML placement with the inputs
  • Form hints, placeholders, and more I18n customizations
  • Document other new form builder methods like error_tag and field

Installation

Add this line to your application's Gemfile:

gem "form_buildable"

And then execute:

$ bundle

Or install it yourself as:

$ gem install form_buildable

Contributing

Contribution directions go here.

License

The gem is available as open source under the terms of the MIT License.

form_buildable's People

Contributors

mjc-gh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.