-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
143 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#lang racket/base | ||
|
||
(require (for-syntax racket/base | ||
racket/syntax | ||
syntax/parse) | ||
forms | ||
koyo/haml | ||
racket/match (prefix-in study: "study.rkt")) | ||
|
||
(provide | ||
:: | ||
formular | ||
checkbox | ||
radios) | ||
|
||
(define-syntax (:: _stx) | ||
(raise-syntax-error ':: "may only be used within a formular")) | ||
|
||
(define-syntax (formular stx) | ||
(syntax-parse stx | ||
#:literals (::) | ||
[(_ form action-e) | ||
#:with rw (format-id stx "rw") | ||
#:with tbl (format-id stx "tbl") | ||
#:with ((kwd fld) ...) | ||
(let loop ([stx #'form] | ||
[pairs null]) | ||
(syntax-parse stx | ||
#:literals (::) | ||
[(:: kwd:keyword fld) | ||
(cons #'(kwd fld) pairs)] | ||
|
||
[(_ e ...) | ||
(apply append (map (λ (stx) (loop stx null)) | ||
(syntax->list #'(e ...))))] | ||
|
||
[_ pairs])) | ||
#:with (field-id ...) | ||
(for/list ([idx (in-naturals 1)] | ||
[kwd (in-list (syntax-e #'(kwd ...)))]) | ||
(format-id kwd "input_~a" idx)) | ||
#:with patched-form | ||
(let loop ([stx #'form]) | ||
(syntax-parse stx | ||
#:literals (::) | ||
[(:: kwd:keyword _) | ||
#'(let ([entry (hash-ref tbl 'kwd)]) | ||
(rw (car entry) ((cdr entry) 'widget)))] | ||
|
||
[(fn e ...) | ||
#`(fn #,@(map loop (syntax-e #'(e ...))))] | ||
|
||
[e #'e])) | ||
#'(let ([action-fn action-e] | ||
[field-id fld] ...) | ||
(let ([tbl (make-hasheq | ||
(list (cons 'kwd (cons (symbol->string 'field-id) field-id)) ...))]) | ||
(let ([f (form* ([field-id (field-id 'validator)] ...) | ||
(cons | ||
(list 'kwd ...) | ||
(list field-id ...)))]) | ||
(study:form | ||
f | ||
(lambda (res) | ||
(define vals-by-kwd | ||
(for/hasheq ([k (in-list (car res))] | ||
[v (in-list (cdr res))]) | ||
(values k v))) | ||
(define sorted-kwds | ||
(sort (car res) keyword<?)) | ||
(define sorted-vals | ||
(for/list ([k (in-list sorted-kwds)]) | ||
(hash-ref vals-by-kwd k))) | ||
(keyword-apply action-fn sorted-kwds sorted-vals null)) | ||
(lambda (rw) | ||
patched-form)))))])) | ||
|
||
(define ((checkbox label) meth) | ||
(match meth | ||
['validator | ||
(ensure binding/boolean (required))] | ||
|
||
['widget | ||
(lambda (name value errors) | ||
(haml | ||
(.group | ||
(:label ((widget-checkbox) name value errors) label) | ||
,@((widget-errors) name value errors))))])) | ||
|
||
(define ((radios label options #:validators [validators null]) meth) | ||
(match meth | ||
['validator | ||
(apply ensure binding/text (required) validators)] | ||
|
||
['widget | ||
(lambda (name value errors) | ||
(haml | ||
(.group | ||
(:label.radio-group | ||
label | ||
((widget-radio-group options) name value errors)) | ||
,@((widget-errors) name value errors))))])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters