Playbook Forms - powerhome/playbook GitHub Wiki

Getting Started

  • Form methods defined in form_builder folder
  • File naming convention:
    • user_defined_field.rb
    • module name UserDefinedField
    • method user_defined(name, **options, props: {})
  • Form methods accept several arguments, including a name symbol, options hash, and props hash which can be passed onto playbook input kit
  • **options collects all other key value pairs passed to method and places them in an options: {} hash
  • Can be useful to add puts options and or puts props to make sense of what arguments are making it into the form method (outputs in rails console)

Naming conventions and inheritance of the super method

  • form methods inherit a super method which returns an html input tag with attributes parsed from the options hash
  • the input tag is in turn passed as a block to the @template.pbrails() method which returns the appropriate rails kit or other erb template
  • This super method can only be used if your form method name follows rails input conventions
  • i.e. def check_box inherits the super method, def checkbox does not
  • Unfortunately can't seem to find a list of methods names which will inherit or a way to form inheritance for newly defined, unconventional methods. Seems to be a method for all standard form input fields: checkbox, radio, text_field, date_field, select, etc.
  • Luckily it is possible to simply parse your own html attributes and pass them along into an <input> string instead of relying solely on the super method to return in input variable
  • In some cases you may wish to pass those html attributes as props to the playbook input kit of your choosing and include logic which applies them to the input within the kit itself
  • The super method also resolve form scoping for you (i.e. scope[example_field])
    • If you need to manually resolve scoping you can do so with the @object_name instance variable inside your form method
    • Check out Date Picker as an example
  • In summary, use the super method if you can. It will make validation and option parsing much easier. If you cannot there are still work arounds but they require more time and effort to implement.

Integrating User Defined Methods into Form System

  • New user defined fields must be prepended in form_builder.rb
  • prepend(UserDefinedField) where UserDefinedField is the module your form method belongs to
  • Newly defined methods can be tested in doc examples
  • add <%= form.user_defined :name_argument, key1: "opt_value1", key2: "opt_value2", props: { prop_key: "prop_value" } %> to bottom of form kit
  • the arguments will be parsed like so name: name_argument, options: { key1: "opt_value1", key2: "opt_value2"}, props: { prop_key: "prop_value" } assuming you have made use of the inherited super method mentioned above
⚠️ **GitHub.com Fallback** ⚠️