Add children slot to element
This commit is contained in:
parent
6ddf42f6a4
commit
3eea6a4e39
2 changed files with 28 additions and 26 deletions
|
@ -2,6 +2,7 @@
|
|||
(:use #:cl)
|
||||
(:export #:element-kind
|
||||
#:element-props
|
||||
#:element-children
|
||||
#:create-element
|
||||
#:expand))
|
||||
(in-package #:hsx/element)
|
||||
|
@ -12,20 +13,25 @@
|
|||
:initarg :kind)
|
||||
(props
|
||||
:reader element-props
|
||||
:initarg :props)))
|
||||
:initarg :props)
|
||||
(children
|
||||
:reader element-children
|
||||
:initarg :children)))
|
||||
|
||||
(defun create-element (kind props &rest children)
|
||||
(make-instance 'element
|
||||
:kind kind
|
||||
:props (append props
|
||||
(and children
|
||||
(list :children (flatten children))))))
|
||||
:props props
|
||||
:children (flatten children)))
|
||||
|
||||
(defmethod expand ((elm element))
|
||||
(with-accessors ((kind element-kind)
|
||||
(props element-props)) elm
|
||||
(props element-props)
|
||||
(children element-children)) elm
|
||||
(if (functionp kind)
|
||||
(apply kind props)
|
||||
(apply kind (append props
|
||||
(and children
|
||||
(list :children children))))
|
||||
elm)))
|
||||
|
||||
;;;; utils
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
(in-suite create-element)
|
||||
|
||||
(test create-html-element
|
||||
(test create-builtin-element
|
||||
(let* ((inner (create-element "span"
|
||||
'(:class "red")
|
||||
"World!"))
|
||||
|
@ -16,14 +16,12 @@
|
|||
nil
|
||||
"Hello,"
|
||||
inner)))
|
||||
(with-accessors ((kind element-kind)
|
||||
(props element-props)) inner
|
||||
(is (string= kind "span"))
|
||||
(is (equal props `(:class "red" :children ("World!")))))
|
||||
(with-accessors ((kind element-kind)
|
||||
(props element-props)) outer
|
||||
(is (string= kind "p"))
|
||||
(is (equal props `(:children ("Hello," ,inner)))))))
|
||||
(is (string= (element-kind inner) "span"))
|
||||
(is (equal (element-props inner) `(:class "red")))
|
||||
(is (equal (element-children inner) (list "World!")))
|
||||
(is (string= (element-kind outer) "p"))
|
||||
(is (null (element-props outer)))
|
||||
(is (equal (element-children outer) (list "Hello," inner)))))
|
||||
|
||||
(test flatten-element-children
|
||||
(let* ((elm (create-element "p"
|
||||
|
@ -31,9 +29,8 @@
|
|||
"a"
|
||||
nil
|
||||
(list "b" (list nil "c"))
|
||||
(cons "d" "e")))
|
||||
(children (getf (element-props elm) :children)))
|
||||
(is (equal children (list "a" "b" "c" "d" "e")))))
|
||||
(cons "d" "e"))))
|
||||
(is (equal (element-children elm) (list "a" "b" "c" "d" "e")))))
|
||||
|
||||
(test create-component-element
|
||||
(labels ((comp (&key variant children)
|
||||
|
@ -47,11 +44,10 @@
|
|||
(outer (create-element #'comp
|
||||
'(:variant "red")
|
||||
inner)))
|
||||
(with-accessors ((kind element-kind)
|
||||
(props element-props)) outer
|
||||
(is (eql kind #'comp))
|
||||
(is (equal props `(:variant "red" :children (,inner)))))
|
||||
(with-accessors ((kind element-kind)
|
||||
(props element-props)) (expand outer)
|
||||
(is (string= kind "p"))
|
||||
(is (equal props `(:class "red" :children ("Hello," ,inner))))))))
|
||||
(is (eql (element-kind outer) #'comp))
|
||||
(is (equal (element-props outer) `(:variant "red")))
|
||||
(is (equal (element-children outer) (list inner)))
|
||||
(let ((expanded-elm (expand outer)))
|
||||
(is (string= (element-kind expanded-elm) "p"))
|
||||
(is (equal (element-props expanded-elm) `(:class "red")))
|
||||
(is (equal (element-children expanded-elm) (list "Hello," inner)))))))
|
||||
|
|
Loading…
Reference in a new issue