Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upstream helper functions to Yesod.Form.Option. #1828

Merged
merged 5 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions yesod-form/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# ChangeLog for yesod-form


## 1.7.7

* Added `optionsFromList'` to create an OptionList from a List, using the PathPiece instance for the external value and
a custom function for the user-facing value. Also added `optionsEnum'` to create an OptionList from an enumeration
[#1828](https://github.com/yesodweb/yesod/pull/1828)

## 1.7.6

* Added `datetimeLocalField` for creating a html `<input type="datetime-local">` [#1817](https://github.com/yesodweb/yesod/pull/1817)
Expand Down
37 changes: 37 additions & 0 deletions yesod-form/Yesod/Form/Option.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{-# LANGUAGE FlexibleContexts #-}

module Yesod.Form.Option where

import Yesod.Core
import Yesod.Form.Fields

-- | Creates an 'OptionList' from a 'List', using the 'PathPiece' instance for
-- the external value and a custom function for the user-facing value.
-- NB: We choose PathPiece as the most suitable class for generating External
-- Values. The motivation is to avoid Show/Read instances which could leak
-- an internal representation to forms, query params, javascript etc.
optionsFromList' ::
MonadHandler m
=> RenderMessage (HandlerSite m) msg
=> PathPiece a
=> [a]
-> (a -> msg)
-> m (OptionList a)
optionsFromList' lst toDisplay = do
mr <- getMessageRender
pure $ mkOptionList $ flip map lst $ \v -> Option
{ optionDisplay = mr $ toDisplay v
, optionInternalValue = v
, optionExternalValue = toPathPiece v
}

-- | Creates an 'OptionList' from an 'Enum'
optionsEnum' ::
MonadHandler m
=> RenderMessage (HandlerSite m) msg
=> PathPiece a
=> Enum a
=> Bounded a
=> (a -> msg)
-> m (OptionList a)
optionsEnum' = optionsFromList' [minBound..maxBound]
3 changes: 2 additions & 1 deletion yesod-form/yesod-form.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: >= 1.10
name: yesod-form
version: 1.7.6
version: 1.7.7
license: MIT
license-file: LICENSE
author: Michael Snoyman <[email protected]>
Expand Down Expand Up @@ -46,6 +46,7 @@ library
build-depends: network-uri >= 2.6

exposed-modules: Yesod.Form
Yesod.Form.Option
Yesod.Form.Types
Yesod.Form.Functions
Yesod.Form.Bootstrap3
Expand Down