Skip to content

Commit

Permalink
Add withRadioField a more flexible radio option renderer
Browse files Browse the repository at this point in the history
This re-expresses radioField into the new more flexible
function.
Which gives an adhoc example on how to use it as well.

This function passes the radio input to a callback function
to let said function decide how it should be rendered.
These changes allow you to make a radio table for example,
for selecting some row.

bump version number, add @SInCE

add note on radioField

Update changelog
  • Loading branch information
jappeace committed Aug 17, 2022
1 parent 337a992 commit 25f83fb
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 25f83fb

Please sign in to comment.