diff --git a/yesod-form/ChangeLog.md b/yesod-form/ChangeLog.md index 79a704be..1251b945 100644 --- a/yesod-form/ChangeLog.md +++ b/yesod-form/ChangeLog.md @@ -1,3 +1,8 @@ +## 1.6.2 + +* Move `addClass` from private/undocumented in `Yesod.Form.Bootstrap3` to `Yesod.Form.Functions` [#1510](https://github.com/yesodweb/yesod/pull/1510) +* Add `Yesod.Form.Functions.removeClass` [#1510](https://github.com/yesodweb/yesod/pull/1510) + ## 1.6.1 * Explicitly define `(<>)` in the `Semigroup` instance for `Enctype` diff --git a/yesod-form/Yesod/Form/Bootstrap3.hs b/yesod-form/Yesod/Form/Bootstrap3.hs index 5d2a4ffb..e99f82ba 100644 --- a/yesod-form/Yesod/Form/Bootstrap3.hs +++ b/yesod-form/Yesod/Form/Bootstrap3.hs @@ -33,9 +33,6 @@ import Control.Monad (liftM) import Data.Text (Text) import Data.String (IsString(..)) import Yesod.Core - -import qualified Data.Text as T - import Yesod.Form.Types import Yesod.Form.Functions @@ -82,12 +79,6 @@ withSmallInput fs = fs { fsAttrs = newAttrs } where newAttrs = addClass "input-sm" (fsAttrs fs) -addClass :: Text -> [(Text, Text)] -> [(Text, Text)] -addClass klass [] = [("class", klass)] -addClass klass (("class", old):rest) = ("class", T.concat [old, " ", klass]) : rest -addClass klass (other :rest) = other : addClass klass rest - - -- | How many bootstrap grid columns should be taken (see -- 'BootstrapFormLayout'). -- diff --git a/yesod-form/Yesod/Form/Functions.hs b/yesod-form/Yesod/Form/Functions.hs index fe02e8db..89d6451c 100644 --- a/yesod-form/Yesod/Form/Functions.hs +++ b/yesod-form/Yesod/Form/Functions.hs @@ -51,10 +51,13 @@ module Yesod.Form.Functions , parseHelper , parseHelperGen , convertField + , addClass + , removeClass ) where import Yesod.Form.Types import Data.Text (Text, pack) +import qualified Data.Text as T import Control.Arrow (second) import Control.Monad.Trans.Class import Control.Monad.Trans.RWS (ask, get, put, runRWST, tell, evalRWST, local, mapRWST) @@ -615,3 +618,33 @@ convertField to from (Field fParse fView fEnctype) = let fParse' ts = fmap (fmap (fmap to)) . fParse ts fView' ti tn at ei = fView ti tn at (fmap from ei) in Field fParse' fView' fEnctype + +-- | Removes a CSS class from the 'fsAttrs' in a 'FieldSettings'. +-- +-- ==== __Examples__ +-- +-- >>> removeClass "form-control" [("class","form-control login-form"),("id","home-login")] +-- [("class"," login-form"),("id","home-login")] +-- +-- @since 1.6.2 +removeClass :: Text -- ^ The class to remove + -> [(Text, Text)] -- ^ List of existing 'fsAttrs' + -> [(Text, Text)] +removeClass _ [] = [] +removeClass klass (("class", old):rest) = ("class", T.replace klass " " old) : rest +removeClass klass (other :rest) = other : removeClass klass rest + +-- | Adds a CSS class to the 'fsAttrs' in a 'FieldSettings'. +-- +-- ==== __Examples__ +-- +-- >>> addClass "login-form" [("class", "form-control"), ("id", "home-login")] +-- [("class","form-control login-form"),("id","home-login")] +-- +-- @since 1.6.2 +addClass :: Text -- ^ The class to add + -> [(Text, Text)] -- ^ List of existing 'fsAttrs' + -> [(Text, Text)] +addClass klass [] = [("class", klass)] +addClass klass (("class", old):rest) = ("class", T.concat [old, " ", klass]) : rest +addClass klass (other :rest) = other : addClass klass rest diff --git a/yesod-form/yesod-form.cabal b/yesod-form/yesod-form.cabal index adfcd2a9..f3e2fa51 100644 --- a/yesod-form/yesod-form.cabal +++ b/yesod-form/yesod-form.cabal @@ -1,5 +1,5 @@ name: yesod-form -version: 1.6.1 +version: 1.6.2 license: MIT license-file: LICENSE author: Michael Snoyman