[racket] playing with formlets: vertical radio buttons

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Thu Dec 12 15:17:09 EST 2013

Here's a version that does that. You need to write your own formlet,
but I hope you don't find it too scary:

#lang racket
(require web-server/formlets)

(define list->2list
  (match-lambda
   [(list)
    (list)]
   [(list* fst snd more)
    (list* (list fst snd) (list->2list more))]))

(define (wrap a-formlet wrapper)
  (λ (i)
    (define-values (xef proc next-i) (a-formlet i))
    (values (wrapper xef) proc next-i)))

(define (vertical-radio-group opts)
  (wrap (radio-group opts)
        (λ (xef)
          `(table ,@(for/list ([f (in-list (list->2list xef))])
                      `(tr (td , at f)))))))

(define f
  (formlet
   (div "Select:" ,{(vertical-radio-group '("A" "B")) . => . rg}
        "Input:" ,{input-string . => . text})
   (list rg text)))

(module+ test
  f
  (formlet-display f))

On Wed, Dec 11, 2013 at 2:05 AM, Dmitry Pavlov <dpavlov at ipa.nw.ru> wrote:
> Jay,
>
> Thanks, your code is more preferable to me as it uses
> less of Racket's formlets library internals.
>
> How do I use your "xe" expression if I want to build
> a formlet that includes the vertical radio button formlet
> plus something else?
>
> For example, if I used the ordinary radio-group formlet,
> I would write
>
> (define my-formlet
>   (formlet
>     (div "Select:" ,{(radio-group '("A" "B")) . => . rg}
>          "Input:" ,{input-string . => . text})
>    (list rg text)))
>
> What do I put instead the (radio-group) in that case?
> Sorry for possibly dumb question, I just started to learn
> web programming in Racket.
>
>
> Regards,
>
> Dmitry
>
>
>
>
>
>
> On 12/10/2013 08:21 PM, Jay McCarthy wrote:
>>
>> Hi Dmitry,
>>
>> Your version looks like it would probably. I think that I would do is
>> process the result of the radio-group formlet though.
>>
>> Like this:
>>
>> #lang racket
>> (require web-server/formlets)
>>
>> (define f
>>    (formlet ,(=> (radio-group (list 1 2 3 4) #:display number->string)
>>                  rg)
>>             rg))
>> (define fxes (formlet-display f))
>> (define xe
>>    `(table ,@(for/list ([f (in-list fxes)])
>>                `(tr (td ,f)))))
>>
>> (module+ test
>>    f
>>    fxes
>>    xe)
>>
>>
>>
>> On Mon, Dec 9, 2013 at 12:02 PM, Dmitry Pavlov <dpavlov at ipa.nw.ru> wrote:
>>>
>>> Hello,
>>>
>>> I recently started to write in Racket a simple web page.
>>>
>>> The page has radio buttons.
>>>
>>> The problem with (radio-group) provided by web-server/formlets
>>> is that it always puts the <input>-s on the page in a single
>>> block. It is not possible to put something between them,
>>> or put themselves in a table etc.
>>>
>>> And using just (radio) for that purpose makes impossible
>>> forming the group itself: the "name" attributes are all
>>> different, and I see no way to override them in the formlet.
>>>
>>> Having the need to organize my radio buttons vertically,
>>> I came up with the following:
>>>
>>>
>>> (define (vertical-radio-group values #:default (default (void)))
>>>    (cross
>>>     (pure
>>>      (lambda (bs)
>>>        (bytes->string/utf-8 (binding:form-value (first bs)))))
>>>     (make-input*
>>>      (lambda (n)
>>>        `(table
>>>          ,@(for/list ((value values))
>>>              `(tr
>>>                (td
>>>                 (input
>>>                  ,(append
>>>                    (if (equal? value default) '((checked "1")) '())
>>>                    `((name ,n)
>>>                      (type "radio")
>>>                      (value ,value))))
>>>                 ,value))))))))
>>>
>>>
>>> I am by no means convinced that it is the right way to get
>>> what I want, or the most simple way. Do experienced Racketeers
>>> have any comments?
>>>
>>>
>>> Best regards,
>>>
>>> Dmitry
>>>
>>> ____________________
>>>   Racket Users list:
>>>   http://lists.racket-lang.org/users
>>
>>
>>
>>
>



-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://faculty.cs.byu.edu/~jay

"The glory of God is Intelligence" - D&C 93


Posted on the users mailing list.