如何使用消化函数创建一个具有以编程方式生成的复选框列表的表单,该表单将返回一个列表。例如:
[x] Milk
[ ] Cereals
[x] Ground meat
会返回["Milk", "Ground meat"]
。
我期待类型会是这样的:
form :: (Functor m, Monad m) => [String] -> HappstackForm m Html BlazeFormHtml [String]
答案 0 :(得分:14)
没有标准的方法可以这样做,但digestive-functors
使用Applicative
界面是高度可组合的,因此您可以轻松创建所需内容。
您可以定义checkBox
,返回Maybe String
,即元素的名称(如果已选中)。
checkBox :: (Functor m, Monad m)
=> String -> HappstackForm m Html BlazeFormHtml (Maybe String)
checkBox str = fmap maybeStr (inputCheckBox False) <++ label str
where
maybeStr True = Just str
maybeStr False = Nothing
然后,您可以循环遍历字符串列表,为列表中的每个元素创建一个这样的复选框:
listForm' :: (Functor m, Monad m)
=> [String]
-> HappstackForm m Html BlazeFormHtml [Maybe String]
listForm' = foldr (\x xs -> fmap (:) x <*> xs) (pure []) . map checkBox
catMaybes :: [Maybe a] -> [a]
可帮助您进一步降低结果:
listForm :: (Functor m, Monad m)
=> [String]
-> HappstackForm m Html BlazeFormHtml [String]
listForm = fmap catMaybes . listForm'
最后,我们可以实例化实际形式:
food :: [String]
food = ["Milk", "Cereals", "Ground meat"]
foodForm :: (Functor m, Monad m)
=> HappstackForm m Html BlazeFormHtml [String]
foodForm = listForm food