mirror of
https://github.com/penpot/penpot.git
synced 2025-02-12 10:09:03 -05:00
✨ Add schema validation for tabs
This commit is contained in:
parent
3df9c88bb7
commit
89562d0231
2 changed files with 28 additions and 28 deletions
|
@ -20,8 +20,8 @@
|
||||||
:git/url "https://github.com/funcool/beicon.git"}
|
:git/url "https://github.com/funcool/beicon.git"}
|
||||||
|
|
||||||
funcool/rumext
|
funcool/rumext
|
||||||
{:git/tag "v2.12"
|
{:git/tag "v2.13"
|
||||||
:git/sha "ab819f5"
|
:git/sha "dc8e1e5"
|
||||||
:git/url "https://github.com/funcool/rumext.git"}
|
:git/url "https://github.com/funcool/rumext.git"}
|
||||||
|
|
||||||
instaparse/instaparse {:mvn/version "1.5.0"}
|
instaparse/instaparse {:mvn/version "1.5.0"}
|
||||||
|
|
|
@ -78,26 +78,6 @@
|
||||||
(when (= button-position "end")
|
(when (= button-position "end")
|
||||||
action-button)]))
|
action-button)]))
|
||||||
|
|
||||||
(defn- valid-tabs?
|
|
||||||
[tabs]
|
|
||||||
(every? (fn [tab]
|
|
||||||
(let [icon (obj/get tab "icon")
|
|
||||||
label (obj/get tab "label")
|
|
||||||
aria-label (obj/get tab "aria-label")]
|
|
||||||
(and (or (not icon) (contains? icon-list icon))
|
|
||||||
(not (and icon (nil? label) (nil? aria-label)))
|
|
||||||
(not (and aria-label (or (nil? icon) label))))))
|
|
||||||
(seq tabs)))
|
|
||||||
|
|
||||||
(def ^:private positions
|
|
||||||
#{"start" "end"})
|
|
||||||
|
|
||||||
(defn- valid-button-position?
|
|
||||||
[position button]
|
|
||||||
(or (nil? position)
|
|
||||||
(and (contains? positions position)
|
|
||||||
(some? button))))
|
|
||||||
|
|
||||||
(defn- get-tab
|
(defn- get-tab
|
||||||
[tabs id]
|
[tabs id]
|
||||||
(or (array/find #(= id (obj/get % "id")) tabs)
|
(or (array/find #(= id (obj/get % "id")) tabs)
|
||||||
|
@ -108,13 +88,33 @@
|
||||||
(let [tab (get-tab tabs default)]
|
(let [tab (get-tab tabs default)]
|
||||||
(obj/get tab "id")))
|
(obj/get tab "id")))
|
||||||
|
|
||||||
(mf/defc tab-switcher*
|
(def ^:private schema:tab
|
||||||
{::mf/props :obj}
|
[:and
|
||||||
[{:keys [class tabs on-change-tab default-selected action-button-position action-button] :rest props}]
|
[:map {:title "tab"}
|
||||||
;; TODO: Use a schema to assert the tabs prop -> https://tree.taiga.io/project/penpot/task/8521
|
[:icon {:optional true}
|
||||||
(assert (valid-tabs? tabs) "unexpected props for tab-switcher")
|
[:and :string [:fn #(contains? icon-list %)]]]
|
||||||
(assert (valid-button-position? action-button-position action-button) "invalid action-button-position")
|
[:label {:optional true} :string]
|
||||||
|
[:aria-label {:optional true} :string]
|
||||||
|
[:content some?]]
|
||||||
|
[:fn {:error/message "invalid data: missing required props"}
|
||||||
|
(fn [tab]
|
||||||
|
(or (and (contains? tab :icon)
|
||||||
|
(or (contains? tab :label)
|
||||||
|
(contains? tab :aria-label)))
|
||||||
|
(contains? tab :label)))]])
|
||||||
|
|
||||||
|
(def ^:private schema:tab-switcher
|
||||||
|
[:map
|
||||||
|
[:class {:optional true} :string]
|
||||||
|
[:action-button-position {:optional true}
|
||||||
|
[:enum "start" "end"]]
|
||||||
|
[:default-selected {:optional true} :string]
|
||||||
|
[:tabs [:vector {:min 1} schema:tab]]])
|
||||||
|
|
||||||
|
(mf/defc tab-switcher*
|
||||||
|
{::mf/props :obj
|
||||||
|
::mf/schema schema:tab-switcher}
|
||||||
|
[{:keys [class tabs on-change-tab default-selected action-button-position action-button] :rest props}]
|
||||||
(let [selected* (mf/use-state #(get-selected-tab-id tabs default-selected))
|
(let [selected* (mf/use-state #(get-selected-tab-id tabs default-selected))
|
||||||
selected (deref selected*)
|
selected (deref selected*)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue