Home

Awesome

A ClojureScript library that generates Om/React forms

You just go down the list of all the most important things on the internet and what you find is :
a Form is usually the broker for the stuff that matters

Luke Wroblewski from Conversions@Google 2014 - Part 2

Project Goals

Artifacts

Clojars Project

Project Maturity

om-inputs is used for real production projects that are used every day. It has been tested by QA Team. At this stage, the project will minimize the breaking changes as it could impact production code.

Show me what it looks like and how to use it !

You can try it online with the interactive tutorial

How does it work

Anatomy of a component

To build a component we need :

The component name

The name is used :

Description of the fields

The fields of a component are described with Schema.

Using Schema allows the :

Supported atomic Schema types

A value can be nil using s/maybe :

{:person/first-name (s/maybe s/Str)}

A key can be optional using s/s/optional-key :

 {(s/optional-key :person/size) s/Num}
Example
(def sch-person {:person/first-name s/Str
                 :person/name s/Str
                 (s/optional-key :person/birthdate) s/Inst
                 (s/optional-key :person/size) s/Int
                 (s/optional-key :person/gender) (s/enum "M" "Ms")})

The callback function

The callback function takes the cursor app state, the owner and the entity.

(fn [app owner entity])
Callback options
{(s/optional-key :action) {(s/optional-key :one-shot) s/Bool
                             (s/optional-key :no-reset) s/Bool
                             (s/optional-key :async) s/Bool
                             (s/optional-key :attrs) {s/Any s/Any}}}
Asynchronous action
{:action {:async true}}

An action can also be asynchronous. In this case the callback fn must have 4 args : (fn [app owner entity chan]) The fourth argument is a core.async channel in which the fn puts the succes or failure of the operation.

Build an Om input component

To build an Om input component, just call the function make-input-comp with the required parameters :

In this example we build the component :create-person with the Schema seen previously and the callback simply diplay the created map :

(def person-input-view (make-input-comp :create-person sch-person #(js/alert %3)))

Translation of the Schema into UI.

The form inputs

Each entry of a schema generate a field in the form.

Hence, the example schema will produce a form with these input fields :

Fields validation

There are two type of validations :

  1. Schema validation
  2. Verily validation
Schema Validation

Schema is able to check that a data structure is conform :

In case of a map :

This job is done by Schema/Coercion : When a value is not of the declared type, we have a chance to coerce it in the correct type.

The problem with an HTML form is that all data are strings.

When validations occur ?
Inline validation

Each field is validated when leaving the input.

If the field is required and left empty the field is maked invalid and a tooltip is displayed.

Submission validation

When clicking the action button, the form is validated according to the Schema :

Options

Options are a mean to override the default behavior of the library.

All options are given in a map.

Order of fields

The schema is a map that can't be ordered so the fields are displayed in a random order.

You can define the total ordering by giving a vector :

(def opts {:order [:person/first-name :person/name :person/gender :person/birthdate :person/size :person/married]})
Change the rendering (implementation may change)
Different way to display an enum

By default the enum is display as a dropdown list It is possible to choose different representations : Vertical Group of radio buttons :

(def opts {:person/gender {:type "radio-group"}})

Horizontal group of radio buttons :

(def opts {:person/gender {:type "radio-group-inline"}})

Segmented controls :

(def opts {:person/gender {:type "btn-group"}})

Stepper (-|+) :

(def opts {:items/number {:type "stepper"
                          :attrs {:min 0 :max 10 :step 1}}})
More Complex Validation rules

It is possible to add more complex validation rules than the one provided by Schema.

I chose Verily for the following reasons :

My goal is to be able to plug any other validation framework.

Add validations rules
(def opts {:validations [[:min-val 100 :person/size :person-size-min-length]
                         [:email :person/email :bad-email]]})
Initial value (not implemented yet)

It should be possible to have initial values for each field.

(def opts {:init {:person/married true}})

The initial data could be retrieved from the cursor app-state.

i18n

It is possible to provide the labels and error messages in multiple languages. Just put a map in the shared data :

(om/root
 app-view
 app-state
 {:target (. js/document (getElementById "person"))
  :shared {:i18n {"en" {:language {:action "Change language"
                                   :lang {:label "Language"
                                          :data {"en" "English"
                                                 "fr" "French"}}}
                        :create-person {:action "Create person"
                                        :person/name {:label "Name"}
                                        :person/birthdate {:label "Birthday"}
                                        :person/first-name {:label "Firstname"}
                                        :person/size {:label "Size"}
                                        :person/gender {:label "Gender"
                                                        :data {"M" "Mister"
                                                               "Ms" "Miss"}}}}
                  "fr" {:language {:action "Choix de la langue"
                                   :lang {:label "Langue"
                                          :data {"en" "Anglais"
                                                 "fr" "Français"}}}
                        :create-person {:action "Créer personne"
                                       :person/name {:label "Nom"}
                                       :person/first-name {:label "Prénom"}
                                       :person/birthdate {:label "Date de naissance"}
                                       :person/size {:label "Taille"}
                                       :person/gender {:label "Genre"
                                                       :data {"M" "Monsieur"
                                                              "Ms" "Madame"}}}}}}})

Copyright and license

Copyright © 2013-2015 Hiram Madelaine

Licensed under the EPL (see the file LICENSE.md).