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

Checkboxes communicate "no selection" properly #57

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion flex-tasks/src/FlexTask/FormUtil.hs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function setDefaults(values){
else if(fieldType != null && fieldType.toLowerCase() === "checkbox"){
field.checked = input.includes(field.getAttribute("value"));
}
else{
else if(fieldType != null && fieldType.toLowerCase() !== "hidden"){
var inputElem = fields.length > 1 ? JSON.parse(input)[j] : input;
if(inputElem != "Missing" && inputElem != "None"){
field.value = inputElem;
Expand Down
8 changes: 4 additions & 4 deletions flex-tasks/src/FlexTask/Generic/Form.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ import Data.Text (Text, pack, unpack)
import Yesod

import FlexTask.Widgets
( horizontalRadioField
( checkboxField
, horizontalRadioField
, joinRenders
, renderForm
, verticalCheckboxesField
)
import FlexTask.YesodConfig (FlexForm, Handler, Rendered)

Expand Down Expand Up @@ -566,8 +566,8 @@ renderNextMultipleChoiceField pairsWith =
, True
, areq $
case align of
Vertical -> verticalCheckboxesField
Horizontal -> checkboxesField
Vertical -> checkboxField True
Horizontal -> checkboxField False
$ withOptions opts
)
_ -> error "Incorrect naming scheme for a multi choice!"
Expand Down
9 changes: 7 additions & 2 deletions flex-tasks/src/FlexTask/Generic/ParseInternal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ instance Parse SingleChoiceSelection where


instance Parse MultipleChoiceSelection where
parseInput = multipleChoiceAnswer <$> parseInput
parseInput = multipleChoiceAnswer <$> parseWithEmptyMarker



Expand All @@ -200,7 +200,12 @@ parseInstanceSingleChoice = toEnum . subtract 1 <$> parseInput

-- | Same as `parseInstanceSingleChoice`, but for parsing a List of the given type, i.e. a multiple choice version.
parseInstanceMultiChoice :: (Bounded a, Enum a, Eq a) => Parser [a]
parseInstanceMultiChoice = map (toEnum . subtract 1) <$> parseInput
parseInstanceMultiChoice = map (toEnum . subtract 1) <$> parseWithEmptyMarker



parseWithEmptyMarker :: Parser [Int]
parseWithEmptyMarker = filter (<0) <$> parseInput



Expand Down
24 changes: 16 additions & 8 deletions flex-tasks/src/FlexTask/Widgets.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,27 @@ $newline never



verticalCheckboxesField :: Eq a => Handler (OptionList a) -> Field Handler [a]
verticalCheckboxesField optList = (multiSelectField optList)
checkboxField :: Eq a => Bool -> Handler (OptionList a) -> Field Handler [a]
checkboxField isVertical optList = (multiSelectField optList)
{ fieldView =
\theId title attrs val _isReq -> do
os <- olOptions <$> handlerToWidget optList
let optSelected (Left _) _ = False
optSelected (Right values) opt = optionInternalValue opt `elem` values
let selected (Left _) _ = False
selected (Right values) opt = optionInternalValue opt `elem` values
checkboxWidget opt = [whamlet|
<label>
<input type=checkbox name=#{title} value=#{optionExternalValue opt} *{attrs} :selected val opt:checked>
#{optionDisplay opt}
|]
[whamlet|
<span ##{theId}>
<input type=hidden name=#{title} value=-1>
$forall opt <- os
<div>
<label>
<input type=checkbox name=#{title} value=#{optionExternalValue opt} *{attrs} :optSelected val opt:checked>
#{optionDisplay opt}
$with box <- checkboxWidget opt
$if isVertical
<div>
^{box}
$else
^{box}
|]
}
5 changes: 3 additions & 2 deletions flex-tasks/test/FlexTask/Generic/ParseSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,20 @@ spec = do
prop "single choice works" $ \i ->
useParser parseInput (escapedSingle $ show i) `shouldParse` singleChoiceAnswer i
prop "multiple choice works" $ \is ->
useParser parseInput (escapedList $ map show is) `shouldParse` multipleChoiceAnswer is
useParser parseInput (escapedList $ map show is) `shouldParse` multipleChoiceAnswer (removeEmpty is)

describe "choice selection parsers (for a test enum)" $ do
specify "single choice works" $
forAll (chooseInt (0,2)) $ \i ->
useParser parseInput (escapedSingle $ show $ i+1) `shouldParse` toEnum @TestEnum i
specify "multiple choice works" $
forAll (sublistOf [0..2]) $ \is ->
useParser parseInput (escapedList $ map (show . (+1)) is) `shouldParse` map (toEnum @TestEnum) is
useParser parseInput (escapedList $ map (show . (+1)) is) `shouldParse` map (toEnum @TestEnum) (removeEmpty is)
where
testParse = many1 digit
boolShow b = if b then "yes" else "no"
maybeShow = maybe "None"
removeEmpty = filter (<0)

testParsingMaybeStringList fromString = testParsingStringList (with fromString)
testParsingMaybe from = testParsingString (with from)
Expand Down
Loading