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
|
2019-06-05 02:01:20 +03:00
|
|
|
import Debug.Control as Control exposing (Control)
|
2018-09-10 18:19:15 +03:00
|
|
|
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-10-24 21:29:27 +03:00
|
|
|
import Nri.Ui.Colors.V1 as Colors
|
2019-10-24 21:08:28 +03:00
|
|
|
import Nri.Ui.SegmentedControl.V8 as SegmentedControl
|
|
|
|
import Nri.Ui.Svg.V1 as Svg exposing (Svg)
|
|
|
|
import Nri.Ui.UiIcon.V1 as UiIcon
|
2018-03-24 05:05:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
type Msg
|
2019-06-05 02:01:20 +03:00
|
|
|
= Select ExampleOption
|
|
|
|
| ChangeOptions (Control Options)
|
|
|
|
|
|
|
|
|
|
|
|
type ExampleOption
|
|
|
|
= A
|
|
|
|
| B
|
|
|
|
| C
|
2018-03-24 05:05:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
type alias State =
|
2019-06-05 02:01:20 +03:00
|
|
|
{ selected : ExampleOption
|
|
|
|
, optionsControl : Control Options
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type alias Options =
|
|
|
|
{ width : SegmentedControl.Width
|
2019-10-24 21:08:28 +03:00
|
|
|
, icon : Maybe Svg
|
2019-06-05 02:01:20 +03:00
|
|
|
, useSpa : Bool
|
|
|
|
}
|
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-09-11 20:37:19 +03:00
|
|
|
let
|
|
|
|
options =
|
|
|
|
Control.currentValue state.optionsControl
|
|
|
|
in
|
2019-10-24 21:08:28 +03:00
|
|
|
{ name = "Nri.Ui.SegmentedControl.V8"
|
2019-05-03 19:30:02 +03:00
|
|
|
, category = Widgets
|
2018-03-24 05:05:34 +03:00
|
|
|
, content =
|
2019-06-05 02:01:20 +03:00
|
|
|
[ Control.view ChangeOptions state.optionsControl
|
|
|
|
|> Html.fromUnstyled
|
|
|
|
, let
|
|
|
|
viewFn =
|
|
|
|
if options.useSpa then
|
|
|
|
SegmentedControl.viewSpa Debug.toString
|
|
|
|
|
|
|
|
else
|
|
|
|
SegmentedControl.view
|
|
|
|
in
|
|
|
|
viewFn
|
|
|
|
{ onClick = Select
|
|
|
|
, options =
|
|
|
|
[ A, B, C ]
|
|
|
|
|> List.map
|
|
|
|
(\i ->
|
|
|
|
{ icon = options.icon
|
|
|
|
, label = "Option " ++ Debug.toString i
|
|
|
|
, value = i
|
|
|
|
}
|
|
|
|
)
|
|
|
|
, selected = state.selected
|
|
|
|
, width = options.width
|
2019-06-05 03:47:27 +03:00
|
|
|
, content = Html.text ("[Content for " ++ Debug.toString state.selected ++ "]")
|
2019-06-05 02:01:20 +03:00
|
|
|
}
|
2019-09-11 20:37:19 +03:00
|
|
|
, Html.h3 [] [ Html.text "Toggle only view" ]
|
|
|
|
, Html.p [] [ Html.text "Used when you only need the ui element and not a page control." ]
|
|
|
|
, SegmentedControl.viewToggle
|
|
|
|
{ onClick = Select
|
|
|
|
, options =
|
|
|
|
[ A, B, C ]
|
|
|
|
|> List.map
|
|
|
|
(\i ->
|
|
|
|
{ icon = options.icon
|
2019-09-17 02:08:08 +03:00
|
|
|
, label = "Choice " ++ Debug.toString i
|
2019-09-11 20:37:19 +03:00
|
|
|
, value = i
|
|
|
|
}
|
|
|
|
)
|
|
|
|
, selected = state.selected
|
|
|
|
, width = options.width
|
|
|
|
}
|
2019-06-05 02:01:20 +03:00
|
|
|
]
|
|
|
|
|> List.map (Html.map parentMessage)
|
2018-03-24 05:05:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
2019-10-24 21:08:28 +03:00
|
|
|
init : State
|
|
|
|
init =
|
2019-06-05 02:01:20 +03:00
|
|
|
{ selected = A
|
|
|
|
, optionsControl =
|
|
|
|
Control.record Options
|
|
|
|
|> Control.field "width"
|
|
|
|
(Control.choice
|
2019-07-30 22:55:55 +03:00
|
|
|
[ ( "FitContent", Control.value SegmentedControl.FitContent )
|
|
|
|
, ( "FillContainer", Control.value SegmentedControl.FillContainer )
|
|
|
|
]
|
2019-06-05 02:01:20 +03:00
|
|
|
)
|
2019-10-24 21:29:27 +03:00
|
|
|
|> Control.field "icon" (Control.maybe False redFlagControl)
|
2019-06-05 02:01:20 +03:00
|
|
|
|> Control.field "which view function"
|
|
|
|
(Control.choice
|
2019-07-30 22:55:55 +03:00
|
|
|
[ ( "view", Control.value False )
|
|
|
|
, ( "viewSpa", Control.value True )
|
|
|
|
]
|
2019-06-05 02:01:20 +03:00
|
|
|
)
|
2018-03-24 05:05:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-24 21:29:27 +03:00
|
|
|
redFlagControl : Control Svg
|
|
|
|
redFlagControl =
|
|
|
|
Control.value (Svg.withColor Colors.red UiIcon.flag)
|
|
|
|
|
|
|
|
|
2018-03-24 05:05:34 +03:00
|
|
|
{-| -}
|
|
|
|
update : Msg -> State -> ( State, Cmd Msg )
|
|
|
|
update msg state =
|
|
|
|
case msg of
|
|
|
|
Select id ->
|
2019-06-05 02:01:20 +03:00
|
|
|
( { state | selected = id }
|
2018-09-10 18:19:15 +03:00
|
|
|
, Cmd.none
|
|
|
|
)
|
|
|
|
|
2019-06-05 02:01:20 +03:00
|
|
|
ChangeOptions newOptions ->
|
|
|
|
( { state | optionsControl = newOptions }
|
|
|
|
, Cmd.none
|
|
|
|
)
|