Skip to content

Commit

Permalink
Revive autoform text input widgets.
Browse files Browse the repository at this point in the history
Looks like interrupted work, intending to use STORE objects to write
data back.  Let's how that pans out.
  • Loading branch information
SvenMichaelKlose committed Jan 21, 2025
1 parent 13610a1 commit 5fb454f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"i18n.lisp"
"autoform.lisp"
"widgets.lisp"
; "widgets-editable.lisp"
"widgets-editable.lisp"
"toplevel.lisp"))
,(+ *modules-path* "js-http-request/main.lisp")
,@(list+ (+ *modules-path* "http-funcall/")
Expand Down
76 changes: 48 additions & 28 deletions modules/lml/autoform.lisp
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
(var *autoform-widgets* nil)

(defmacro def-autoform-widget (args predicate &body body)
`(+! *autoform-widgets* (… {:predicate ,predicate
:maker #'(,args ,@body)})))
`(push {:predicate ,predicate
:maker #'(,args ,@body)}
*autoform-widgets*))

(defmacro def-editable-autoform-widget (args predicate &body body)
`(push {:predicate ,predicate
:maker #'(,args ,@body)}
*autoform-widgets*))

(macro autoform-fn (name (schema data &optional key) &rest body)
`(progn
(fn ,name (props)
(with (,schema props.schema
,data props.data
,key props.key
widgets props.widgets)
,@body))
(declare-lml-component ,name)))


(defclass (autoform-field lml-component) (init-props)
Expand All @@ -13,55 +29,59 @@
(?
(function? !.key)
(~> !.key !.data)
(@ (widget *autoform-widgets*)
(@ (widget !.widgets)
(when (~> widget.predicate !.schema)
(return (~> widget.maker
!.schema !.key !.data
(aref !.data !.key))))))))
!.data !.key !.schema
(when !.data
(aref !.data !.key)))))))))

(finalize-class autoform-field)
(declare-lml-component autoform-field)


(macro autoform-fn (name (schema data &optional key) &rest body)
`(progn
(fn ,name (props)
(with (,schema props.schema
,data props.data
,key props.key)
,@body))
(declare-lml-component ,name)))

(autoform-fn autoform-preview-object (schema data)
`(tr
,@(@ [`(td (autoform-field :key ,_
:schema ,(aref schema.properties _)
:data ,(aref data _)))]
,@(@ [`(td (autoform-field :key ,_
:schema ,(aref schema.properties _)
:data ,(aref data _)
:widgets ,widgets))]
props.fields)))

(autoform-fn autoform-array (schema data)
`(table :class "autoform-array"
,@(@ [`(autoform-preview :schema ,schema.items
:data ,_)]
,@(@ [`(autoform-preview :schema ,schema.items
:data ,_
:widgets ,widgets)]
data)))

(fn autoform-i18n (x)
(? (json-object? x)
x.en
x))

(autoform-fn autoform-property (schema data)
(!= (aref schema.properties props.key)
`(label :class "autoform-property"
(span ,(| !.title props.key))
(autoform-field :key ,props.key
:schema ,!
:data ,data
:widgets ,*autoform-widgets*))))
(span ,(| (autoform-i18n !.title) props.key))
(autoform-field :key ,props.key
:schema ,!
:data ,data
:widgets ,widgets))))

(autoform-fn autoform-object (schema data)
`(div :class "autoform-object"
,@(@ [`(autoform-property :schema ,schema :data ,data :key ,_)]
props.fields)))
,@(@ [`(autoform-property :key ,_
:schema ,schema
:data ,data
:widgets ,widgets)]
(keys schema.properties))))

(autoform-fn autoform (schema data)
"Dispatch to 'AUTOFORM-<basic JSON type>'."
`(,(? (in? (schema-type schema) "array" "object")
($ 'autoform- (upcase (schema-type schema)))
'autoform-field)
:schema ,schema
:data ,data))
:schema ,schema
:data ,data
:widgets ,(| widgets *autoform-widgets*)))
7 changes: 7 additions & 0 deletions modules/lml/schema.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@
(? (string? x)
x
x.type))

(fn set-schema-items (value what schema &rest fields)
(@ (i (| fields (keys schema.properties)) schema)
(= (ref (ref schema i) what) value)))

(fn make-schema-editable (schema &rest fields)
(*> #'set-schema-items t "is_editable" schema fields))
12 changes: 4 additions & 8 deletions modules/lml/widgets.lisp
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
(fn autoform-value (schema v)
(| v schema.default ""))
(| v
(awhen schema
!.default)
""))

(def-autoform-widget (store name schema v)
[identity t]
`(pre :class "autoform-field-generic"
,(autoform-value schema v)))

(fn set-schema-items (value what schema &rest fields)
(@ (i (| fields (keys schema.properties)) schema)
(= (ref (ref schema i) what) value)))

(fn make-schema-editable (schema &rest fields)
(*> #'set-schema-items t "is_editable" schema fields))

0 comments on commit 5fb454f

Please sign in to comment.