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

130 lines
3.1 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 Debug.Control as Control exposing (Control)
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.Icon.V5 as Icon
import Nri.Ui.SegmentedControl.V7 as SegmentedControl
2018-03-24 05:05:34 +03:00
{-| -}
type Msg
= Select ExampleOption
| ChangeOptions (Control Options)
type ExampleOption
= A
| B
| C
2018-03-24 05:05:34 +03:00
{-| -}
type alias State =
{ selected : ExampleOption
, optionsControl : Control Options
}
type alias Options =
{ width : SegmentedControl.Width
, icon : Maybe SegmentedControl.Icon
, useSpa : Bool
}
2018-03-24 05:05:34 +03:00
{-| -}
example : (Msg -> msg) -> State -> ModuleExample msg
example parentMessage state =
{ name = "Nri.Ui.SegmentedControl.V7"
2019-05-03 19:30:02 +03:00
, category = Widgets
2018-03-24 05:05:34 +03:00
, content =
[ Control.view ChangeOptions state.optionsControl
|> Html.fromUnstyled
, let
options =
Control.currentValue state.optionsControl
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
, content = Html.text ("[Content for " ++ Debug.toString state.selected ++ "]")
}
]
|> List.map (Html.map parentMessage)
2018-03-24 05:05:34 +03:00
}
{-| -}
init : { r | help : String } -> State
init assets =
{ 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 )
]
)
|> Control.field "icon"
(Control.maybe False (Control.value { alt = "Help", icon = Icon.helpSvg assets }))
|> Control.field "which view function"
(Control.choice
2019-07-30 22:55:55 +03:00
[ ( "view", Control.value False )
, ( "viewSpa", Control.value True )
]
)
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
)
ChangeOptions newOptions ->
( { state | optionsControl = newOptions }
, Cmd.none
)