noredink-ui/styleguide-app/Examples/Checkbox.elm

182 lines
4.9 KiB
Elm
Raw Normal View History

2018-06-20 18:06:11 +03:00
module Examples.Checkbox exposing (Msg, State, example, init, update)
{-|
2018-09-26 17:02:10 +03:00
@docs Msg, State, example, init, update
2018-06-20 18:06:11 +03:00
-}
import Assets exposing (assets)
import Debug.Control as Control exposing (Control)
import Html.Styled as Html exposing (..)
import ModuleExample as ModuleExample exposing (Category(..), ModuleExample)
import Nri.Ui.Checkbox.V3 as Checkbox
import Nri.Ui.Data.PremiumLevel as PremiumLevel exposing (PremiumLevel(..))
2018-09-08 01:24:02 +03:00
import Nri.Ui.PremiumCheckbox.V2 as PremiumCheckbox
2018-06-20 18:06:11 +03:00
import Set exposing (Set)
{-| -}
type Msg
= ToggleCheck Id Bool
| NoOp
{-| -}
type alias State =
{ isChecked : Set String
}
{-| -}
example : (Msg -> msg) -> State -> ModuleExample msg
example parentMessage state =
{ filename = "Nri/Checkbox.elm"
, category = Inputs
, content =
[ viewInteractableCheckbox "styleguide-checkbox-interactable" state
, viewIndeterminateCheckbox "styleguide-checkbox-indeterminate" state
, viewLockedOnInsideCheckbox "styleguide-locked-on-inside-checkbox" state
, viewDisabledCheckbox "styleguide-checkbox-disabled" state
, h3 [] [ text "Premium Checkboxes" ]
, viewPremiumCheckboxes state
]
|> List.map (Html.toUnstyled << Html.map parentMessage)
}
{-| -}
init : State
init =
{ isChecked = Set.empty
}
{-| -}
update : Msg -> State -> ( State, Cmd Msg )
update msg state =
case msg of
ToggleCheck id checked ->
let
isChecked =
if checked then
Set.insert id state.isChecked
2018-09-26 17:02:10 +03:00
2018-06-20 18:06:11 +03:00
else
Set.remove id state.isChecked
in
( { state | isChecked = isChecked }, Cmd.none )
NoOp ->
( state, Cmd.none )
-- INTERNAL
type alias PremiumExampleConfig =
{ disabled : Bool
, teacherPremiumLevel : PremiumLevel
, pennant : Maybe PremiumCheckbox.Pennant
2018-06-20 18:06:11 +03:00
}
viewInteractableCheckbox : Id -> State -> Html Msg
viewInteractableCheckbox id state =
Checkbox.viewWithLabel
assets
{ identifier = id
, label = "This is an interactable checkbox!"
, setterMsg = ToggleCheck id
, selected = isSelected id state
, disabled = False
2018-06-20 20:52:02 +03:00
, theme = Checkbox.Square
2018-06-20 18:06:11 +03:00
, noOpMsg = NoOp
}
viewIndeterminateCheckbox : Id -> State -> Html Msg
viewIndeterminateCheckbox id state =
Checkbox.viewWithLabel
assets
{ identifier = id
, label = "This Checkbox is set in an indeterminate state"
, setterMsg = ToggleCheck id
, selected = Checkbox.PartiallySelected
, disabled = True
2018-06-20 20:52:02 +03:00
, theme = Checkbox.Square
2018-06-20 18:06:11 +03:00
, noOpMsg = NoOp
}
viewLockedOnInsideCheckbox : Id -> State -> Html Msg
viewLockedOnInsideCheckbox id state =
Checkbox.viewWithLabel
assets
{ identifier = id
2018-06-26 19:36:43 +03:00
, label = "I'm a locked Checkbox"
2018-06-20 18:06:11 +03:00
, setterMsg = ToggleCheck id
, selected = Checkbox.NotSelected
, disabled = True
2018-06-26 19:36:43 +03:00
, theme = Checkbox.Locked
2018-06-20 18:06:11 +03:00
, noOpMsg = NoOp
}
viewDisabledCheckbox : Id -> State -> Html Msg
viewDisabledCheckbox id state =
Checkbox.viewWithLabel
assets
{ identifier = id
, label = "Disabled theme"
, setterMsg = ToggleCheck id
, selected = isSelected id state
, disabled = True
2018-06-20 20:52:02 +03:00
, theme = Checkbox.Square
2018-06-20 18:06:11 +03:00
, noOpMsg = NoOp
}
viewPremiumCheckboxes : State -> Html Msg
viewPremiumCheckboxes state =
let
checkbox config =
2018-06-20 18:06:11 +03:00
PremiumCheckbox.premium
assets
{ label = config.label
, id = "premium-checkbox-" ++ config.label
2018-06-20 18:06:11 +03:00
, selected =
if Set.member config.label state.isChecked then
2018-06-20 18:06:11 +03:00
Checkbox.Selected
2018-09-26 17:02:10 +03:00
2018-06-20 18:06:11 +03:00
else
Checkbox.NotSelected
, disabled = config.disabled
, isLocked = config.isLocked
, pennant = config.pennant
, onChange = ToggleCheck config.label
2018-06-20 18:06:11 +03:00
, onLockedClick = NoOp
, noOpMsg = NoOp
}
in
Html.div []
[ checkbox { label = "Identify Adjectives 1 (Free)", disabled = False, isLocked = False, pennant = Nothing }
, checkbox { label = "Identify Adjectives 2 (Premium)", disabled = False, isLocked = False, pennant = Just PremiumCheckbox.Premium }
, checkbox { label = "Revising Wordy Phrases 1 (Writing)", disabled = False, isLocked = True, pennant = Just PremiumCheckbox.PremiumWithWriting }
, checkbox { label = "Revising Wordy Phrases 2 (Writing) (Disabled)", disabled = True, isLocked = True, pennant = Just PremiumCheckbox.PremiumWithWriting }
2018-06-20 18:06:11 +03:00
]
type alias Id =
String
isSelected : Id -> State -> Checkbox.IsSelected
isSelected id state =
if Set.member id state.isChecked then
Checkbox.Selected
2018-09-26 17:02:10 +03:00
2018-06-20 18:06:11 +03:00
else
Checkbox.NotSelected