diff --git a/src/component/button.lisp b/src/component/button.lisp index 94e105e..2a5c6f4 100644 --- a/src/component/button.lisp +++ b/src/component/button.lisp @@ -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. diff --git a/tests/component/button.lisp b/tests/component/button.lisp index 0a8e9dd..fbe5339 100644 --- a/tests/component/button.lisp +++ b/tests/component/button.lisp @@ -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)))))