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
|
|
|
|
|
|
|
-}
|
|
|
|
|
2020-03-24 03:33:42 +03:00
|
|
|
import Category exposing (Category(..))
|
2019-02-12 00:31:55 +03:00
|
|
|
import Css
|
2018-06-20 18:06:11 +03:00
|
|
|
import Html.Styled as Html exposing (..)
|
2019-02-12 00:31:55 +03:00
|
|
|
import Html.Styled.Attributes exposing (css)
|
2020-03-24 03:33:42 +03:00
|
|
|
import ModuleExample exposing (ModuleExample)
|
2019-04-04 03:11:41 +03:00
|
|
|
import Nri.Ui.Checkbox.V5 as Checkbox
|
2018-06-20 18:06:11 +03:00
|
|
|
import Nri.Ui.Data.PremiumLevel as PremiumLevel exposing (PremiumLevel(..))
|
2019-07-18 00:31:56 +03:00
|
|
|
import Nri.Ui.PremiumCheckbox.V6 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 =
|
2019-05-03 19:56:43 +03:00
|
|
|
{ name = "Nri.Ui.Checkbox.V5"
|
2018-06-20 18:06:11 +03:00
|
|
|
, 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
|
2019-02-12 00:31:55 +03:00
|
|
|
, viewMultilineCheckboxes
|
2018-06-20 18:06:11 +03:00
|
|
|
, h3 [] [ text "Premium Checkboxes" ]
|
|
|
|
, viewPremiumCheckboxes state
|
|
|
|
]
|
2018-10-23 19:55:30 +03:00
|
|
|
|> List.map (Html.map parentMessage)
|
2018-06-20 18:06:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
viewInteractableCheckbox : Id -> State -> Html Msg
|
|
|
|
viewInteractableCheckbox id state =
|
|
|
|
Checkbox.viewWithLabel
|
|
|
|
{ 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
viewIndeterminateCheckbox : Id -> State -> Html Msg
|
|
|
|
viewIndeterminateCheckbox id state =
|
|
|
|
Checkbox.viewWithLabel
|
|
|
|
{ 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
viewLockedOnInsideCheckbox : Id -> State -> Html Msg
|
|
|
|
viewLockedOnInsideCheckbox id state =
|
|
|
|
Checkbox.viewWithLabel
|
|
|
|
{ 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
viewDisabledCheckbox : Id -> State -> Html Msg
|
|
|
|
viewDisabledCheckbox id state =
|
|
|
|
Checkbox.viewWithLabel
|
|
|
|
{ identifier = id
|
|
|
|
, label = "Disabled theme"
|
|
|
|
, setterMsg = ToggleCheck id
|
|
|
|
, selected = isSelected id state
|
2018-06-20 18:13:08 +03:00
|
|
|
, disabled = True
|
2018-06-20 20:52:02 +03:00
|
|
|
, theme = Checkbox.Square
|
2018-06-20 18:06:11 +03:00
|
|
|
}
|
|
|
|
|
2019-02-12 00:38:37 +03:00
|
|
|
|
2019-02-12 00:31:55 +03:00
|
|
|
viewMultilineCheckboxes : Html Msg
|
|
|
|
viewMultilineCheckboxes =
|
|
|
|
Html.section
|
|
|
|
[ css [ Css.width (Css.px 500) ] ]
|
|
|
|
[ Html.h3 [] [ Html.text "Multiline Text in Checkboxes" ]
|
|
|
|
, Checkbox.viewWithLabel
|
2019-11-15 19:44:11 +03:00
|
|
|
{ identifier = "fake-not-selected"
|
2019-02-12 00:31:55 +03:00
|
|
|
, label = "Ut nobis et vel. Nulla rerum sit eos accusamus placeat. Iure sunt earum voluptatibus autem ratione soluta sint.\n\nIste perferendis eum corporis ullam magnam incidunt eos."
|
2019-11-15 19:44:11 +03:00
|
|
|
, setterMsg = ToggleCheck "fake-not-selected"
|
2019-02-12 00:31:55 +03:00
|
|
|
, selected = Checkbox.NotSelected
|
|
|
|
, disabled = False
|
|
|
|
, theme = Checkbox.Square
|
|
|
|
}
|
|
|
|
, Checkbox.viewWithLabel
|
2019-11-15 19:44:11 +03:00
|
|
|
{ identifier = "fake-partially-selected"
|
2019-02-12 00:31:55 +03:00
|
|
|
, label = "Ut nobis et vel. Nulla rerum sit eos accusamus placeat. Iure sunt earum voluptatibus autem ratione soluta sint.\n\nIste perferendis eum corporis ullam magnam incidunt eos."
|
|
|
|
, setterMsg = ToggleCheck "fake"
|
|
|
|
, selected = Checkbox.PartiallySelected
|
|
|
|
, disabled = True
|
|
|
|
, theme = Checkbox.Square
|
|
|
|
}
|
|
|
|
, Checkbox.viewWithLabel
|
2019-11-15 19:44:11 +03:00
|
|
|
{ identifier = "fake-not-selected-locked"
|
2019-02-12 00:31:55 +03:00
|
|
|
, label = "Ut nobis et vel. Nulla rerum sit eos accusamus placeat. Iure sunt earum voluptatibus autem ratione soluta sint.\n\nIste perferendis eum corporis ullam magnam incidunt eos."
|
|
|
|
, setterMsg = ToggleCheck "fake"
|
|
|
|
, selected = Checkbox.NotSelected
|
|
|
|
, disabled = True
|
|
|
|
, theme = Checkbox.Locked
|
|
|
|
}
|
|
|
|
, Checkbox.viewWithLabel
|
2019-11-15 19:44:11 +03:00
|
|
|
{ identifier = "fake-not-selected-square"
|
2019-02-12 00:31:55 +03:00
|
|
|
, label = "Ut nobis et vel. Nulla rerum sit eos accusamus placeat. Iure sunt earum voluptatibus autem ratione soluta sint.\n\nIste perferendis eum corporis ullam magnam incidunt eos."
|
|
|
|
, setterMsg = ToggleCheck "fake"
|
|
|
|
, selected = Checkbox.NotSelected
|
|
|
|
, disabled = True
|
|
|
|
, theme = Checkbox.Square
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-06-20 18:06:11 +03:00
|
|
|
|
|
|
|
viewPremiumCheckboxes : State -> Html Msg
|
|
|
|
viewPremiumCheckboxes state =
|
|
|
|
let
|
2019-11-16 01:56:28 +03:00
|
|
|
safeId =
|
|
|
|
String.replace " " "-"
|
|
|
|
|
2018-09-05 23:40:18 +03:00
|
|
|
checkbox config =
|
2019-06-04 03:14:21 +03:00
|
|
|
PremiumCheckbox.view
|
2018-09-05 23:40:18 +03:00
|
|
|
{ label = config.label
|
2019-11-16 01:56:28 +03:00
|
|
|
, id = "premium-checkbox-" ++ safeId config.label
|
2018-06-20 18:06:11 +03:00
|
|
|
, selected =
|
2018-09-05 23:40:18 +03:00
|
|
|
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
|
2018-09-05 23:40:18 +03:00
|
|
|
, isLocked = config.isLocked
|
2019-07-18 00:31:56 +03:00
|
|
|
, isPremium = config.isPremium
|
2018-09-05 23:40:18 +03:00
|
|
|
, onChange = ToggleCheck config.label
|
2018-06-20 18:06:11 +03:00
|
|
|
, onLockedClick = NoOp
|
|
|
|
}
|
|
|
|
in
|
|
|
|
Html.div []
|
2019-06-04 03:14:21 +03:00
|
|
|
[ checkbox
|
|
|
|
{ label = "Identify Adjectives 2 (Premium)"
|
|
|
|
, disabled = False
|
|
|
|
, isLocked = False
|
2019-07-18 00:31:56 +03:00
|
|
|
, isPremium = True
|
2019-06-04 03:14:21 +03:00
|
|
|
}
|
|
|
|
, checkbox
|
2019-07-18 00:31:56 +03:00
|
|
|
{ label = "Identify Adjectives 2 (Free)"
|
2019-06-04 03:14:21 +03:00
|
|
|
, disabled = False
|
2019-07-18 00:31:56 +03:00
|
|
|
, isLocked = False
|
|
|
|
, isPremium = False
|
2019-06-04 03:14:21 +03:00
|
|
|
}
|
|
|
|
, checkbox
|
2019-07-18 00:31:56 +03:00
|
|
|
{ label = "Revising Wordy Phrases 2 (Premium, Disabled)"
|
2019-06-04 03:14:21 +03:00
|
|
|
, disabled = True
|
|
|
|
, isLocked = True
|
2019-07-18 00:31:56 +03:00
|
|
|
, isPremium = True
|
2019-06-04 03:14:21 +03:00
|
|
|
}
|
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
|