Btn can have an optional ID

This commit is contained in:
Marcus Kammer 2023-09-01 18:39:05 +02:00
parent 5d9e3398fd
commit c13c94f045
Signed by: marcuskammer
GPG key ID: C374817BE285268F
2 changed files with 27 additions and 8 deletions

View file

@ -88,9 +88,11 @@
(in-package :cl-sbt/btn)
(defmacro btn ((&key (type "button") (color "primary") (size "")) &body body)
(defmacro btn ((&key (id nil) (type "button") (color "primary") (size "")) &body body)
"This macro generates a Bootstrap button.
ID: (optional) The HTML id attribute for the button.
COLOR: The color of the button (like 'primary', 'secondary', 'success', etc.).
SIZE: (optional) The size of the button ('lg' for large, 'sm' for small).
@ -106,6 +108,7 @@ Example:
`(spinneret:with-html
(:button :type ,type
:class ,class-str
,@(when (stringp id) (list :id id))
,@body))))
(defmacro define-btn (color &optional (outline nil) (size nil))
@ -123,8 +126,8 @@ button of the specified type and size."
(outline-name (if (null outline) "" "outline-"))
(color-name (concatenate 'string outline-name color))
(macro-name (intern (string-upcase (concatenate 'string "BTN-" outline-name color size-name)))))
`(defmacro ,macro-name ((&key (type "button")) &body body)
`(btn (:type ,type :color ,,color-name :size ,,size-name) ,@body))))
`(defmacro ,macro-name ((&key (id nil) (type "button")) &body body)
`(btn (:id ,id :type ,type :color ,,color-name :size ,,size-name) ,@body))))
(defmacro define-btns (colors)
"This macro generates a suite of button-creating macros for each provided button type.

View file

@ -14,7 +14,8 @@
(deftest test-btn-macro
(testing "Testing btn macro"
(let ((result (spinneret:with-html-string (btn (:color "primary") "foo"))))
(ok (search "class=\"btn btn-primary\"" result)))))
(ok (search "class=\"btn btn-primary\"" result))
(ok (search "type=button" result)))))
(deftest test-btn-macro-with-type
(testing "Testing btn macro with type"
@ -22,18 +23,33 @@
(ok (search "class=\"btn btn-primary\"" result))
(ok (search "type=submit" result)))))
(deftest test-btn-macro-with-id
(testing "Testing btn macro with id"
(let ((result (spinneret:with-html-string (btn (:id "submit" :color "primary") "foo"))))
(ok (search "class=\"btn btn-primary\"" result))
(ok (search "id=submit" result)))))
(deftest test-btn-primary
(testing "Testing btn primary"
(let ((result (spinneret:with-html-string (btn-primary () "foo"))))
(ok (search "class=\"btn btn-primary\"" result)))))
(let ((result (spinneret:with-html-string (btn-primary nil "foo"))))
(ok (search "class=\"btn btn-primary\"" result))
(ok (search "type=button" result)))))
(deftest test-btn-outline-primary
(testing "Testing btn outline primary"
(testing "Testing btn outline primary without additional keywords"
(let ((result (spinneret:with-html-string (btn-outline-primary () "foo"))))
(ok (search "class=\"btn btn-outline-primary\"" result)))))
(ok (search "class=\"btn btn-outline-primary\"" result))
(ok (search "type=button" result)))))
(deftest test-btn-outline-primary-with-type-submit
(testing "Testing btn outline primary with type submit"
(let ((result (spinneret:with-html-string (btn-outline-primary (:type "submit") "foo"))))
(ok (search "class=\"btn btn-outline-primary\"" result))
(ok (search "type=submit" result)))))
(deftest test-btn-outline-primary-with-id
(testing "Testing btn outline primary with id example"
(let ((result (spinneret:with-html-string (btn-outline-primary (:id "example") "foo"))))
(ok (search "class=\"btn btn-outline-primary\"" result))
(ok (search "type=button" result))
(ok (search "id=example" result)))))