ella / src / Ella / Forms / Widgets / RadioButtonList.hs

{-# LANGUAGE DisambiguateRecordFields, MultiParamTypeClasses, TypeSynonymInstances #-}
module Ella.Forms.Widgets.RadioButtonList where

import Control.Monad (liftM)
import Ella.Forms.Base
import Ella.GenUtils (nullToNothing)
import qualified Text.XHtml as X
import qualified Ella.Forms.Widgets.RadioButton as RB
import Text.XHtml ( (<<)
                  , (+++)
                  , (!)
                  )


data RadioButtonList = RadioButtonList {
      selectedValue :: String
    , name :: String
    , identifier :: String
    , values :: [String]
    , captions :: [X.Html]
}

instance X.HTML RadioButtonList where
    toHtml rbl = X.toHtml $
                 do
                   (val, caption, idx) <- zip3 (values rbl) (captions rbl) [0..]
                   return (X.label << (RB.RadioButton { value = val
                                                      , name = name rbl
                                                      , identifier = if (not . null . identifier) rbl
                                                                     then identifier rbl ++ "_" ++ show idx
                                                                     else ""
                                                      , checked = val == selectedValue rbl
                                                      }
                                       +++ " " +++ caption)
                           +++ X.br)

instance HasId RadioButtonList where
    setId theid t = t { identifier = theid }
    getId t = identifier t

instance HasVal RadioButtonList String where
    setVal val t = t { selectedValue = val }
    getVal t = selectedValue t
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.