Skip to content

Commit

Permalink
formular,conscript: add make-radios-with-other
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdanp committed Mar 15, 2024
1 parent 5b51c82 commit ac63544
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 6 deletions.
88 changes: 84 additions & 4 deletions congame-core/components/formular.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

(require (for-syntax racket/base
racket/syntax
syntax/parse)
syntax/parse/pre)
forms
koyo/haml
(prefix-in m: marionette)
racket/list
racket/match
racket/port
threading
(prefix-in bot: (submod "bot.rkt" actions))
(prefix-in study: "study.rkt")
web-server/http)
Expand Down Expand Up @@ -38,7 +39,8 @@
input-time
textarea
make-checkboxes
make-radios)
make-radios
make-radios-with-other)

(define (kwd->symbol kwd)
(string->symbol (keyword->string kwd)))
Expand Down Expand Up @@ -157,7 +159,11 @@
[{~or (kwd:keyword _)
(kwd:keyword _ _)}
#'(let ([entry (hash-ref tbl 'kwd)])
(rw (car entry) ((cdr entry) 'widget)))]
(let ([widget ((cdr entry) 'widget)])
(if widget
(rw (car entry) widget)
(((cdr entry) 'widget/ns)
(widget-namespace (car entry) rw)))))]

[(e ...)
#`(#,@(map loop (syntax-e #'(e ...))))]
Expand All @@ -167,7 +173,11 @@
(syntax->datum #'(dynamic-field-id ...)))
#:with kwd (datum->syntax #'e (string->keyword (symbol->string (syntax-e #'e))))
#'(let ([entry (hash-ref tbl 'kwd)])
(rw (car entry) ((cdr entry) 'widget)))]
(let ([widget ((cdr entry) 'widget)])
(if widget
(rw (car entry) widget)
(((cdr entry) 'widget/ns)
(widget-namespace (car entry) rw)))))]

[e #'e]))
#:with defaults
Expand Down Expand Up @@ -602,6 +612,76 @@
(render-proc options make-radio)
,@((widget-errors) name value errors))))]))

(define ((make-radios-with-other options
#:required? [required? #t]
#:other-label [other-label "Other:"]
#:radio-validators [radio-validators null]
#:other-validators [other-validators null]
#:radio-attributes [radio-attributes null]
#:other-attributes [other-attributes '((placeholder "Other..."))]) meth)
(match meth
['validator
(form* ([radio-value (apply ensure binding/symbol radio-validators)]
[other-value (apply ensure binding/text other-validators)])
(if other-value
(ok other-value)
(if radio-value
(ok radio-value)
(if required?
(err '((radio-value . "You must pick a value or write something in the other field.")))
(ok #f)))))]
['widget #f]
['widget/ns
(lambda (rw)
(define (widget-radio-value name value _errors)
(let ([value (get-binding-value value)])
`(div
,@(for/list ([opt (in-list options)])
(match-define (cons option label)
opt)
`(div
(label
(input
([name ,name]
[type "radio"]
[value ,(symbol->string option)]
,@(if (eq? (string->symbol value) option)
(cons '(checked "") radio-attributes)
radio-attributes)))
,label))))))

(define (widget-other-value name value _errors)
`(label
,other-label
(script
#<<SCRIPT
function $$congame$$formular$$unradio() {
this.parentNode.parentNode.parentNode.querySelectorAll('[type=radio]').forEach(el => (el.checked = false));
}
SCRIPT
)
(input
([name ,name]
[type "text"]
[value ,(get-binding-value value)]
[onchange "$$congame$$formular$$unradio.apply(this)"]
[onfocus "$$congame$$formular$$unradio.apply(this)"]
,@other-attributes))))

(haml
(.group
(rw "radio-value" widget-radio-value)
,@(rw "radio-value" (widget-errors))
(rw "other-value" widget-other-value)
,@(rw "other-value" (widget-errors)))))]))

(define (get-binding-value bind)
(or
(and~> bind
(binding:form-value)
(bytes->string/utf-8))
""))


;; help ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Expand Down
18 changes: 17 additions & 1 deletion congame-example-study/conscript-easy-forms.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@
(define twice-free-form
(if free-form (* 2 free-form) "no value provided"))

(define radios-with-other
(get 'radios-with-other #f))

@md{
# Results so far

1. Result from `Multiple Checkboxes`: @checkboxes
2. Twice the result from `Free-Form Forms`: @(~a twice-free-form)
3. Radios with other: @(~a radios-with-other)

@button{Go back to choosing Forms}})

Expand Down Expand Up @@ -211,6 +215,14 @@
}
@submit-button}}})

(defstep (free-form-radios-with-other-choice)
@md{# Radios with "other" choice

@form{@binding[#:radios-with-other
(make-radios-with-other '((a . "A")
(b . "B")))]
@submit-button}})

(defstep (vertical-whitespace)
@md{
# More Vertical Whitespace
Expand Down Expand Up @@ -247,6 +259,10 @@

[labeled-submit-button --> choose-page]

[free-form-forms1 --> free-form-forms2 --> display-results --> choose-page]
[free-form-forms1
--> free-form-forms2
--> free-form-radios-with-other-choice
--> display-results
--> choose-page]

[vertical-whitespace --> choose-page])
4 changes: 3 additions & 1 deletion conscript/form.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
[congame:submit-button submit-button]
[congame:submit-button/label submit-button/label]
[congame:textarea textarea]
[congame:make-checkboxes make-checkboxes])
[congame:make-checkboxes make-checkboxes]
[congame:make-radios make-radios]
[congame:make-radios-with-other make-radios-with-other])
form
radios
select
Expand Down

0 comments on commit ac63544

Please sign in to comment.