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
|
|
|
|
|
|
|
{-|
|
|
|
|
|
2018-03-30 19:03:16 +03:00
|
|
|
@docs Msg
|
|
|
|
@docs State
|
|
|
|
@docs example
|
|
|
|
@docs init
|
|
|
|
@docs update
|
2018-03-24 05:05:34 +03:00
|
|
|
|
|
|
|
-}
|
|
|
|
|
2018-09-10 18:19:15 +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)
|
2019-06-05 00:35:01 +03:00
|
|
|
import Nri.Ui.SegmentedControl.V7 as SegmentedControl
|
2018-03-24 05:05:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
type Msg
|
|
|
|
= Select Id
|
2018-09-10 18:19:15 +03:00
|
|
|
| SetFillContainer Bool
|
2018-03-24 05:05:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
type alias State =
|
2019-06-05 00:35:01 +03:00
|
|
|
SegmentedControl.Config Id Msg
|
2018-03-24 05:05:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
2018-03-29 13:58:41 +03:00
|
|
|
example : (Msg -> msg) -> State -> ModuleExample msg
|
|
|
|
example parentMessage state =
|
2019-06-05 00:35:01 +03:00
|
|
|
{ name = "Nri.Ui.SegmentedControl.V7"
|
2019-05-03 19:30:02 +03:00
|
|
|
, category = Widgets
|
2018-03-24 05:05:34 +03:00
|
|
|
, content =
|
2018-10-23 19:55:30 +03:00
|
|
|
List.map (Html.map parentMessage)
|
2018-09-10 18:19:15 +03:00
|
|
|
[ fillContainerCheckbox state.width
|
2019-06-05 00:35:01 +03:00
|
|
|
, SegmentedControl.view state
|
2018-09-10 18:19:15 +03:00
|
|
|
]
|
2018-03-24 05:05:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
init : State
|
|
|
|
init =
|
|
|
|
{ onClick = Select
|
|
|
|
, options =
|
2018-03-29 04:20:58 +03:00
|
|
|
[ { icon = Nothing
|
|
|
|
, label = "Option A"
|
|
|
|
, value = "a"
|
|
|
|
}
|
|
|
|
, { icon = Nothing
|
|
|
|
, label = "Option B"
|
|
|
|
, value = "b"
|
|
|
|
}
|
2018-03-24 05:05:34 +03:00
|
|
|
]
|
|
|
|
, selected = "a"
|
2019-06-05 00:35:01 +03:00
|
|
|
, width = SegmentedControl.FitContent
|
2018-03-24 05:05:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-05 00:35:01 +03:00
|
|
|
fillContainerCheckbox : SegmentedControl.Width -> Html Msg
|
2018-09-10 18:19:15 +03:00
|
|
|
fillContainerCheckbox currentOption =
|
|
|
|
let
|
|
|
|
id =
|
|
|
|
"SegmentedControl-fill-container-checkbox"
|
|
|
|
|
|
|
|
isChecked =
|
|
|
|
case currentOption of
|
2019-06-05 00:35:01 +03:00
|
|
|
SegmentedControl.FitContent ->
|
2018-09-10 18:19:15 +03:00
|
|
|
Just False
|
|
|
|
|
2019-06-05 00:35:01 +03:00
|
|
|
SegmentedControl.FillContainer ->
|
2018-09-10 18:19:15 +03:00
|
|
|
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 )
|
|
|
|
|
2018-09-10 18:19:15 +03:00
|
|
|
SetFillContainer fillContainer ->
|
|
|
|
( { state
|
|
|
|
| width =
|
|
|
|
if fillContainer then
|
2019-06-05 00:35:01 +03:00
|
|
|
SegmentedControl.FillContainer
|
2018-09-26 17:02:10 +03:00
|
|
|
|
2018-09-10 18:19:15 +03:00
|
|
|
else
|
2019-06-05 00:35:01 +03:00
|
|
|
SegmentedControl.FitContent
|
2018-09-10 18:19:15 +03:00
|
|
|
}
|
|
|
|
, Cmd.none
|
|
|
|
)
|
|
|
|
|
2018-03-24 05:05:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- INTERNAL
|
|
|
|
|
|
|
|
|
|
|
|
type alias Id =
|
|
|
|
String
|