0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-03 04:49:03 -05:00

Add improved form validation in color lightbox.

This commit is contained in:
Andrey Antukh 2015-12-24 20:30:05 +02:00
parent f8b369784f
commit a896bb60d6
8 changed files with 53 additions and 10 deletions

View file

@ -30,6 +30,8 @@
[v] [v]
(not (nil? (re-find #"^#[0-9A-Fa-f]{6}$" v)))) (not (nil? (re-find #"^#[0-9A-Fa-f]{6}$" v))))
(def required v/required)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Public Api ;; Public Api
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

View file

@ -21,7 +21,6 @@
(defn app-render (defn app-render
[own] [own]
(let [{:keys [location location-params] :as state} (rum/react state)] (let [{:keys [location location-params] :as state} (rum/react state)]
(println "app-render" location state)
(case location (case location
:auth/login (users/login) :auth/login (users/login)
:dashboard/projects (dashboard/projects-page) :dashboard/projects (dashboard/projects-page)

View file

@ -5,10 +5,12 @@
[cats.labs.lens :as l] [cats.labs.lens :as l]
[uxbox.state :as st] [uxbox.state :as st]
[uxbox.rstore :as rs] [uxbox.rstore :as rs]
[uxbox.schema :as sc]
[uxbox.data.dashboard :as dd] [uxbox.data.dashboard :as dd]
[uxbox.util.lens :as ul] [uxbox.util.lens :as ul]
[uxbox.ui.dashboard.builtins :as builtins]
[uxbox.ui.icons :as i] [uxbox.ui.icons :as i]
[uxbox.ui.form :as form]
[uxbox.ui.dashboard.builtins :as builtins]
[uxbox.ui.lightbox :as lightbox] [uxbox.ui.lightbox :as lightbox]
[uxbox.ui.colorpicker :refer (colorpicker)] [uxbox.ui.colorpicker :refer (colorpicker)]
[uxbox.ui.dom :as dom] [uxbox.ui.dom :as dom]
@ -68,7 +70,7 @@
(defn page-title-render (defn page-title-render
[own coll] [own coll]
(letfn [(on-title-edited [e] (letfn [(on-title-edited [e]
(let [content (.-innerText (.-target e)) (let [content (dom/event->inner-text e)
collid (:id coll)] collid (:id coll)]
(rs/emit! (dd/rename-color-collection collid content)))) (rs/emit! (dd/rename-color-collection collid content))))
(on-delete [e] (on-delete [e]
@ -188,17 +190,21 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; TODO: implement proper form validation ;; TODO: implement proper form validation
(def ^:static +color-form-schema+
{:hex [sc/required sc/color]})
(defn- color-lightbox-render (defn- color-lightbox-render
[own {:keys [coll color]}] [own {:keys [coll color]}]
(let [local (:rum/local own)] (let [local (:rum/local own)]
(letfn [(submit [e] (letfn [(submit [e]
(let [params {:id (:id coll) :from color (if-let [errors (sc/validate +color-form-schema+ @local)]
:to (:hex @local)}] (swap! local assoc :errors errors)
(rs/emit! (dd/replace-color params)) (let [params {:id (:id coll) :from color
(lightbox/close!))) :to (:hex @local)}]
(rs/emit! (dd/replace-color params))
(lightbox/close!))))
(on-change [e] (on-change [e]
(let [value (str/trim (.-value (.-target e)))] (let [value (str/trim (dom/event->value e))]
(swap! local assoc :hex value)))] (swap! local assoc :hex value)))]
(html (html
[:div.lightbox-body [:div.lightbox-body
@ -207,6 +213,7 @@
[:div.row-flex [:div.row-flex
[:input#color-hex.input-text [:input#color-hex.input-text
{:placeholder "#" {:placeholder "#"
:class (form/error-class local :hex)
:on-change on-change :on-change on-change
:value (or (:hex @local) color "") :value (or (:hex @local) color "")
:type "text"}]] :type "text"}]]

View file

@ -8,6 +8,10 @@
[e] [e]
(.preventDefault e)) (.preventDefault e))
(defn event->inner-text
[e]
(.-innerText (.-target e)))
(defn event->value (defn event->value
[e] [e]
(.-value (.-target e))) (.-value (.-target e)))

View file

@ -0,0 +1,22 @@
(ns uxbox.ui.form
(:require [sablono.core :refer-macros [html]]
[uxbox.schema :as sc]))
(defn validate!
[local schema]
(if-let [errors (sc/validate schema @local)]
(swap! local assoc :errors errors)
(swap! local assoc :errors nil)))
(defn input-error
[local name]
(when-let [errors (get-in @local [:errors name])]
[:div.errors
[:ul {}
(for [error errors]
[:li error])]]))
(defn error-class
[local name]
(when (get-in @local [:errors name])
"invalid"))

View file

@ -75,13 +75,11 @@
(defn -will-mount (defn -will-mount
[own] [own]
(println "shortcut-will-mount")
(let [sub (init-handler)] (let [sub (init-handler)]
(assoc own ::subscription sub))) (assoc own ::subscription sub)))
(defn -will-unmount (defn -will-unmount
[own] [own]
(println "shortcut-will-unmount")
(let [sub (::subscription own)] (let [sub (::subscription own)]
(sub) (sub)
(dissoc own ::subscription))) (dissoc own ::subscription)))

View file

@ -44,3 +44,4 @@
@import 'partials/lightbox'; @import 'partials/lightbox';
@import 'partials/color-palette'; @import 'partials/color-palette';
@import 'partials/colorpicker'; @import 'partials/colorpicker';
@import 'partials/forms';

View file

@ -0,0 +1,10 @@
// TODO: juan revisit
input,
select,
textarea {
&.invalid {
border-color: red;
color: red;
}
}