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

125 lines
2.4 KiB
Elm
Raw Normal View History

2018-09-26 17:02:10 +03:00
module Examples.SegmentedControl exposing
( Msg
, State
, example
, init
, update
)
2018-03-24 05:05:34 +03:00
{-|
@docs Msg
@docs State
@docs example
@docs init
@docs update
2018-03-24 05:05:34 +03:00
-}
import Accessibility.Styled
import Html.Styled as Html exposing (Html)
import Html.Styled.Attributes as Attr
import Html.Styled.Events as Events
2018-03-24 05:05:34 +03:00
import ModuleExample exposing (Category(..), ModuleExample)
import Nri.Ui.SegmentedControl.V6 exposing (Width(..))
2018-03-24 05:05:34 +03:00
{-| -}
type Msg
= Select Id
| SetFillContainer Bool
2018-03-24 05:05:34 +03:00
{-| -}
type alias State =
Nri.Ui.SegmentedControl.V6.Config Id Msg
2018-03-24 05:05:34 +03:00
{-| -}
example : (Msg -> msg) -> State -> ModuleExample msg
example parentMessage state =
{ filename = "Nri/Ui/SegmentedControl/V6.elm"
2018-03-24 05:05:34 +03:00
, category = Behaviors
, content =
List.map (Html.map parentMessage >> Html.toUnstyled)
[ fillContainerCheckbox state.width
, Nri.Ui.SegmentedControl.V6.view state
]
2018-03-24 05:05:34 +03:00
}
{-| -}
init : State
init =
{ onClick = Select
, options =
[ { icon = Nothing
, id = "a"
, label = "Option A"
, value = "a"
}
, { icon = Nothing
, id = "b"
, label = "Option B"
, value = "b"
}
2018-03-24 05:05:34 +03:00
]
, selected = "a"
, width = FitContent
2018-03-24 05:05:34 +03:00
}
fillContainerCheckbox : Width -> Html Msg
fillContainerCheckbox currentOption =
let
id =
"SegmentedControl-fill-container-checkbox"
isChecked =
case currentOption of
FitContent ->
Just False
FillContainer ->
Just True
in
Html.div []
[ Accessibility.Styled.checkbox "Fill container"
isChecked
[ Attr.id id
, Events.onCheck SetFillContainer
]
, Html.label
[ Attr.for id
]
[ Html.text "Fill Container" ]
]
2018-03-24 05:05:34 +03:00
{-| -}
update : Msg -> State -> ( State, Cmd Msg )
update msg state =
case msg of
Select id ->
( { state | selected = id }, Cmd.none )
SetFillContainer fillContainer ->
( { state
| width =
if fillContainer then
FillContainer
2018-09-26 17:02:10 +03:00
else
FitContent
}
, Cmd.none
)
2018-03-24 05:05:34 +03:00
-- INTERNAL
type alias Id =
String