diff --git a/src/joy/form-helper.janet b/src/joy/form-helper.janet index 283f4eb..f1bbbf9 100644 --- a/src/joy/form-helper.janet +++ b/src/joy/form-helper.janet @@ -2,110 +2,118 @@ (import ./helper :prefix "") (import ./csrf :prefix "") -(defn- field [kind val key & attrs] - [:input (merge {:type kind :name (string key) :value (get val key)} (table ;attrs))]) +(defn- field [kind name & attrs] + [:input (merge {:type kind :name (string name)} (struct ;attrs))]) -(def hidden-field - `(hidden-field val key & attrs) +(defn hidden-field + `(hidden-field name & attrs) Generates an html element where - val is a dictionary and key is the value html attribute of a key - in the val dictionary. If key is nil, an error will be thrown. + name is a keyword denoting the name html attribute. Ex. - (hidden-field {:a "a" :b "b"} :a :class "a-class" :style "a-style") - (hidden-field {:a "a" :b "b"} :b)` - (partial field "hidden")) + (hidden-field :myhiddenfield :value "hiddenvalue" :class "a-class" :style "a-style") + (hidden-field :api-token :value "secret-token") + (hidden-field :valueless-hidden-field)` + [name & attrs] + (field "hidden" name ;attrs)) -(def text-field - `(text-field val key & attrs) +(defn text-field + `(text-field name & attrs) Generates an html element where - val is a dictionary and key is the value html attribute of a key - in the val dictionary. If key is nil, an error will be thrown. + name is a keyword denoting the name html attribute. Ex. - (text-field {:a "a" :b "b"} :a :class "a-class" :style "a-style") - (text-field {:a "a" :b "b"} :b)` - (partial field "text")) + (text-field :username :placeholder "Enter Username" :class "a-class" :style "a-style") + (text-field :some-prefilled-text :value "I am prefilled!") + (text-field :text-field)` + [name & attrs] + (field "text" name ;attrs)) -(def email-field - `(email-field val key & attrs) +(defn email-field + `(email-field name & attrs) Generates an html element where - val is a dictionary and key is the value html attribute of a key - in the val dictionary. If key is nil, an error will be thrown. + name is a keyword denoting the name html attribute. Ex. - (email-field {:a "a" :b "b"} :a :class "a-class" :style "a-style") - (email-field {:a "a" :b "b"} :b)` - (partial field "email")) + (email-field :email-address :placeholder "Email" :value "me@example.com") + (email-field :email :class "a-class" :style "a-style") + (email-field :email)` + [name & attrs] + (field "email" name ;attrs)) -(def password-field - `(password-field val key & attrs) +(defn password-field + `(password-field name & attrs) Generates an html element where - val is a dictionary and key is the value html attribute of a key - in the val dictionary. If key is nil, an error will be thrown. + name is a keyword denoting the name html attribute. Ex. - (password-field {:a "a" :b "b"} :a :class "a-class" :style "a-style") - (password-field {:a "a" :b "b"} :b)` - (partial field "password")) + (password-field :pass-field :placeholder "Password" :class "a-class" :style "a-style") + (password-field :pswd :class "a-class" :style "a-style") + (password-field :pswd)` + [name & attrs] + (field "password" name ;attrs)) -(def file-field - `(file-field val key & attrs) +(defn file-field + `(file-field name & attrs) Generates an html element where - val is a dictionary and key is the value html attribute of a key - in the val dictionary. If key is nil, an error will be thrown. + name is a keyword denoting the name html attribute. Ex. - (file-field {:a "a" :b "b"} :a :class "a-class" :style "a-style") - (file-field {:a "a" :b "b"} :b)` - (partial field "file")) + (file-field :file-field :accept "image/*,.pdf") + (file-field :file-field :class "a-class" :style "a-style") + (file-field :file-field)` + [name & attrs] + (field "file" name ;attrs)) (defn checkbox-field - `(checkbox-field val key & attrs) + `(checkbox-field name checked? & attrs) Generates two inputs, one hidden and one checkbox - where val is a dictionary and key is the value html attribute of a key - in that val dictionary. The first checkbox input is hidden + where name is a keyword denoting the name html attribute, + and checked? is a boolean denoting whether the checkbox is + checked by default. Ex. - (checkbox-field {:enabled true} :enabled :class "a-class" :style "a-style") + (checkbox-field :neovim? true :true "you're cool" :false "reconsider") + (checkbox-field :something false :class "a-class" :style "a-style") => - - ` - [val key & attrs] - (let [checked (if (or (true? (get val key)) - (one? (get val key))) - {:checked ""} - {}) + + + + + ` + [name checked? & attrs] + (let [checked (if checked? {:checked ""} {}) attrs (struct ;attrs)] - - [[:input {:type "hidden" :name key :value (get attrs :false 0)}] - [:input (merge {:type "checkbox" :name key :value (get attrs :true 1)} + [(hidden-field name :value (get attrs :false 0)) + [:input (merge {:type "checkbox" :name (string name) :value (get attrs :true 1)} checked attrs)]])) (defn form-for - `Generates a