feat: add icon support to button, alert, badge, and form input

Button:
- :icon-left, :icon-right props for buttons with leading/trailing icons
- :icon prop for icon-only buttons (square padding via .btn-icon class)
- Icon size scales with button size (sm→sm, md/lg→sm/md)

Alert:
- Auto-assigns variant-specific icons (circle-check, alert-triangle,
  alert-circle, info) per variant
- :icon-name prop to override default, false to suppress
- Layout restructured with .alert-icon + .alert-content wrapper

Badge:
- :icon-name prop adds a leading icon before text
- .badge-icon CSS scales icon to match badge font size

Form input:
- :icon-left and :icon-right props on form-input
- Wraps input in .form-input-wrap with absolutely-positioned icon spans
- Padding adjusts automatically via .form-input--icon-left/right

All three dev targets (hiccup, replicant, squint) updated with demos.
This commit is contained in:
Florian Schroedl
2026-03-05 19:33:55 +01:00
parent e3132a3cb4
commit 93edebf144
15 changed files with 438 additions and 122 deletions

View File

@@ -1,6 +1,7 @@
(ns ui.alert-test
(:require [clojure.test :refer [deftest is testing]]
[ui.alert :as alert]))
[ui.alert :as alert]
[ui.icon :as icon]))
(deftest alert-class-list-test
(testing "neutral (no variant)"
@@ -24,12 +25,29 @@
(is (= "alert alert-success" (get-in result [1 :class])))
(is (= "alert" (get-in result [1 :role])))))
(testing "alert with title includes title paragraph"
(let [result (alert/alert {:title "Title"} "Body")]
(is (some #(and (vector? %) (= "alert-title" (get-in % [1 :class])))
(rest (rest result))))))
(testing "variant alert includes default icon"
(let [result (alert/alert {:variant :success :title "Done!"} "Saved.")
icon-span (nth result 2)]
(is (= :span (first icon-span)))
(is (= "alert-icon" (get-in icon-span [1 :class])))))
(testing "alert without title has no title paragraph"
(let [result (alert/alert {} "Body")]
(is (not (some #(and (vector? %) (= "alert-title" (get-in % [1 :class])))
(rest (rest result))))))))
(testing "neutral alert has no icon"
(let [result (alert/alert {:title "Title"} "Body")]
(is (not (some #(and (vector? %) (= "alert-icon" (get-in % [1 :class])))
(rest (rest result)))))))
(testing "icon-name false suppresses icon"
(let [result (alert/alert {:variant :success :icon-name false} "Body")]
(is (not (some #(and (vector? %) (= "alert-icon" (get-in % [1 :class])))
(rest (rest result)))))))
(testing "icon-name overrides default"
(let [result (alert/alert {:variant :success :icon-name :star} "Body")
icon-span (nth result 2)]
(is (= "alert-icon" (get-in icon-span [1 :class])))))
(testing "alert content is wrapped in alert-content div"
(let [result (alert/alert {:variant :info :title "Title"} "Body")
content-div (last result)]
(is (= :div (first content-div)))
(is (= "alert-content" (get-in content-div [1 :class]))))))

View File

@@ -1,6 +1,7 @@
(ns ui.badge-test
(:require [clojure.test :refer [deftest is testing]]
[ui.badge :as badge]))
[ui.badge :as badge]
[ui.icon :as icon]))
(deftest badge-class-list-test
(testing "default variant (primary)"
@@ -29,3 +30,14 @@
(testing "extra class gets appended"
(let [result (badge/badge {:class "extra"} "X")]
(is (= "badge extra" (get-in result [1 :class]))))))
(deftest badge-icon-test
(testing "badge with icon-name renders icon before text"
(let [result (badge/badge {:icon-name :check} "Done")]
(is (= :span (first result)))
(is (= :svg (first (nth result 2)))) ;; icon is first child
(is (= "Done" (nth result 3))))) ;; text is second child
(testing "badge without icon-name has no icon"
(let [result (badge/badge {} "Plain")]
(is (= "Plain" (nth result 2))))))

View File

@@ -1,6 +1,7 @@
(ns ui.button-test
(:require [clojure.test :refer [deftest is testing]]
[ui.button :as button]))
[ui.button :as button]
[ui.icon :as icon]))
(deftest button-class-list-test
(testing "default variant and size"
@@ -97,3 +98,39 @@
(is (= :a (first result)))
(is (= "https://example.com" (get-in result [1 :href])))
(is (= "btn btn-link" (get-in result [1 :class]))))))
(deftest button-icon-class-list-test
(testing "icon-only adds btn-icon class"
(is (= ["btn" "btn-secondary" "btn-icon"] (button/button-class-list {:icon :plus})))
(is (= ["btn" "btn-primary" "btn-icon"] (button/button-class-list {:variant :primary :icon :plus}))))
(testing "icon-only with size"
(is (= ["btn" "btn-primary" "btn-sm" "btn-icon"]
(button/button-class-list {:variant :primary :size :sm :icon :plus})))))
(deftest button-icon-component-test
(testing "icon-only button renders icon child"
(let [result (button/button {:variant :primary :icon :plus})]
(is (= :button (first result)))
(is (= "btn btn-primary btn-icon" (get-in result [1 :class])))
;; third element should be the SVG icon
(is (= :svg (first (nth result 2))))))
(testing "icon-left renders icon before text"
(let [result (button/button {:variant :primary :icon-left :plus} "Add")]
(is (= :button (first result)))
(is (= :svg (first (nth result 2)))) ;; icon is first child
(is (= "Add" (nth result 3))))) ;; text is second child
(testing "icon-right renders icon after text"
(let [result (button/button {:variant :primary :icon-right :arrow-right} "Next")]
(is (= :button (first result)))
(is (= "Next" (nth result 2))) ;; text is first child
(is (= :svg (first (nth result 3)))))) ;; icon is second child
(testing "icon-left and icon-right together"
(let [result (button/button {:variant :primary :icon-left :plus :icon-right :arrow-right} "Add")]
(is (= :svg (first (nth result 2)))) ;; left icon
(is (= "Add" (nth result 3))) ;; text
(is (= :svg (first (nth result 4)))))) ;; right icon
)

View File

@@ -1,6 +1,7 @@
(ns ui.form-test
(:require [clojure.test :refer [deftest is testing]]
[ui.form :as form]))
[ui.form :as form]
[ui.icon :as icon]))
;; ── form-field ──────────────────────────────────────────────────────
@@ -102,6 +103,41 @@
(let [result (form/form-input {:error true})]
(is (= "form-input form-input--error" (get-in result [1 :class]))))))
(deftest form-input-icon-test
(testing "input with icon-left wraps in form-input-wrap"
(let [result (form/form-input {:icon-left :search :placeholder "Search..."})]
(is (= :div (first result)))
(is (= "form-input-wrap" (get-in result [1 :class])))
;; first child: left icon span
(let [icon-span (nth result 2)]
(is (= :span (first icon-span)))
(is (= "form-input-icon form-input-icon--left" (get-in icon-span [1 :class]))))
;; second child: the actual input
(let [input (nth result 3)]
(is (= :input (first input)))
(is (clojure.string/includes? (get-in input [1 :class]) "form-input--icon-left")))))
(testing "input with icon-right wraps in form-input-wrap"
(let [result (form/form-input {:icon-right :check})]
(is (= :div (first result)))
;; second child: the input
(let [input (nth result 2)]
(is (= :input (first input)))
(is (clojure.string/includes? (get-in input [1 :class]) "form-input--icon-right")))
;; third child: right icon span
(let [icon-span (nth result 3)]
(is (= :span (first icon-span)))
(is (= "form-input-icon form-input-icon--right" (get-in icon-span [1 :class]))))))
(testing "input with both icons"
(let [result (form/form-input {:icon-left :search :icon-right :x})]
(is (= :div (first result)))
(is (= 5 (count result))))) ;; div + attrs + left-icon + input + right-icon
(testing "input without icons returns bare input"
(let [result (form/form-input {:placeholder "Name"})]
(is (= :input (first result))))))
;; ── form-textarea ───────────────────────────────────────────────────
(deftest form-textarea-class-list-test