Skip to content

Commit

Permalink
Merge pull request #1775 from SupercedeTech/add-with-option-more-flex…
Browse files Browse the repository at this point in the history
…ible-radio-inputs

Add withRadioField a more flexible radio option renderer
  • Loading branch information
snoyberg committed Sep 18, 2022
2 parents 26a195b + 25f83fb commit 65adf9b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
4 changes: 4 additions & 0 deletions yesod-form/ChangeLog.md
@@ -1,5 +1,9 @@
# ChangeLog for yesod-form

## 1.7.2

* Added `withRadioField` and re-express `radioField` into that. [#1775](https://github.com/yesodweb/yesod/pull/1775)

## 1.7.1

* Added `colorField` for creating a html color field (`<input type="color">`) [#1748](https://github.com/yesodweb/yesod/pull/1748)
Expand Down
45 changes: 35 additions & 10 deletions yesod-form/Yesod/Form/Fields.hs
Expand Up @@ -49,6 +49,7 @@ module Yesod.Form.Fields
, selectFieldListGrouped
, radioField
, radioFieldList
, withRadioField
, checkboxesField
, checkboxesFieldList
, multiSelectField
Expand Down Expand Up @@ -530,26 +531,50 @@ checkboxesField ioptlist = (multiSelectField ioptlist)
radioField :: (Eq a, RenderMessage site FormMessage)
=> HandlerFor site (OptionList a)
-> Field (HandlerFor site) a
radioField = selectFieldHelper
(\theId _name _attrs inside -> [whamlet|
$newline never
<div ##{theId}>^{inside}
|])
(\theId name isSel -> [whamlet|
radioField = withRadioField
(\theId optionWidget -> [whamlet|
$newline never
<label .radio for=#{theId}-none>
<div>
<input id=#{theId}-none type=radio name=#{name} value=none :isSel:checked>
^{optionWidget}
_{MsgSelectNone}
|])
(\theId name attrs value isSel text -> [whamlet|
(\theId value _isSel text optionWidget -> [whamlet|
$newline never
<label .radio for=#{theId}-#{value}>
<div>
<input id=#{theId}-#{value} type=radio name=#{name} value=#{value} :isSel:checked *{attrs}>
^{optionWidget}
\#{text}
|])
Nothing


-- | Allows the user to place the option radio widget somewhere in
-- the template.
-- For example: If you want a table of radio options to select.
-- 'radioField' is an example on how to use this function.
--
-- @since 1.7.2
withRadioField :: (Eq a, RenderMessage site FormMessage)
=> (Text -> WidgetFor site ()-> WidgetFor site ()) -- ^ nothing case for mopt
-> (Text -> Text -> Bool -> Text -> WidgetFor site () -> WidgetFor site ()) -- ^ cases for values
-> HandlerFor site (OptionList a)
-> Field (HandlerFor site) a
withRadioField nothingFun optFun =
selectFieldHelper outside onOpt inside Nothing
where
outside theId _name _attrs inside' = [whamlet|
$newline never
<div ##{theId}>^{inside'}
|]
onOpt theId name isSel = nothingFun theId $ [whamlet|
$newline never
<input id=#{theId}-none type=radio name=#{name} value=none :isSel:checked>
|]
inside theId name attrs value isSel display =
optFun theId value isSel display [whamlet|
<input id=#{theId}-#{(value)} type=radio name=#{name} value=#{(value)} :isSel:checked *{attrs}>
|]


-- | Creates a group of radio buttons to answer the question given in the message. Radio buttons are used to allow differentiating between an empty response (@Nothing@) and a no response (@Just False@). Consider using the simpler 'checkBoxField' if you don't need to make this distinction.
--
Expand Down
2 changes: 1 addition & 1 deletion yesod-form/yesod-form.cabal
@@ -1,6 +1,6 @@
cabal-version: >= 1.10
name: yesod-form
version: 1.7.1
version: 1.7.2
license: MIT
license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com>
Expand Down

0 comments on commit 65adf9b

Please sign in to comment.