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

210 lines
5.7 KiB
Elm
Raw Normal View History

2018-09-26 17:02:10 +03:00
module Examples.SegmentedControl exposing
2020-03-31 22:43:32 +03:00
( Msg, State
2018-09-26 17:02:10 +03:00
, example
)
2018-03-24 05:05:34 +03:00
{-|
2020-03-31 22:43:32 +03:00
@docs Msg, State
@docs example
2018-03-24 05:05:34 +03:00
-}
2020-08-05 03:52:30 +03:00
import Accessibility.Styled as Html exposing (Html)
2020-06-19 23:41:28 +03:00
import AtomicDesignType exposing (AtomicDesignType(..))
import Category exposing (Category(..))
2020-08-05 04:08:16 +03:00
import Css
import Debug.Control as Control exposing (Control)
2020-03-31 23:20:03 +03:00
import Example exposing (Example)
2020-08-05 04:08:16 +03:00
import Html.Styled.Attributes as Attributes exposing (css)
import Html.Styled.Events as Events
import KeyboardSupport exposing (Direction(..), Key(..))
import Nri.Ui.Colors.V1 as Colors
2020-08-05 03:37:56 +03:00
import Nri.Ui.SegmentedControl.V10 as SegmentedControl
2019-10-24 23:23:52 +03:00
import Nri.Ui.Svg.V1 as Svg exposing (Svg)
2019-10-24 21:08:28 +03:00
import Nri.Ui.UiIcon.V1 as UiIcon
2018-03-24 05:05:34 +03:00
{-| -}
2020-03-31 23:20:03 +03:00
example : Example State Msg
2020-03-31 22:43:32 +03:00
example =
2020-08-05 03:37:56 +03:00
{ name = "Nri.Ui.SegmentedControl.V10"
2020-03-31 22:43:32 +03:00
, state = init
, update = update
2020-03-31 22:48:26 +03:00
, subscriptions = \_ -> Sub.none
2020-03-31 22:43:32 +03:00
, view =
\state ->
let
options =
Control.currentValue state.optionsControl
in
[ Control.view ChangeOptions state.optionsControl
|> Html.fromUnstyled
2020-08-05 04:08:16 +03:00
, Html.h3 [ css [ Css.marginBottom Css.zero ] ]
[ Html.code [] [ Html.text "view" ] ]
, Html.p [ css [ Css.marginTop (Css.px 1) ] ]
[ Html.text "Use when you need a page control. This view is effectively a fancy Tab/Tabpanel pairing." ]
2020-08-05 03:52:30 +03:00
, SegmentedControl.view
2020-08-05 05:30:24 +03:00
{ onClick = SelectPage
, selected = state.page
2020-03-31 22:43:32 +03:00
, width = options.width
2020-08-05 05:06:28 +03:00
, toUrl = Nothing
2020-08-05 05:30:24 +03:00
, options = List.take options.count (buildOptions options.icon)
2020-03-31 22:43:32 +03:00
}
2020-08-05 04:08:16 +03:00
, Html.h3 [ css [ Css.marginBottom Css.zero ] ]
[ Html.code [] [ Html.text "viewSelect" ] ]
, Html.p [ css [ Css.marginTop (Css.px 1) ] ]
[ Html.text "Use when you only need the ui element. This view is effectively a fancy Radio button." ]
2020-05-22 21:31:13 +03:00
, SegmentedControl.viewSelect
2020-05-20 21:58:21 +03:00
{ onClick = MaybeSelect
2020-08-05 07:28:01 +03:00
, options = List.take options.count (buildSelectOptions options.icon)
, selected = state.optionallySelected
, width = options.width
}
2020-03-31 22:43:32 +03:00
]
, categories = [ Widgets, Layout ]
2020-06-20 00:16:10 +03:00
, atomicDesignType = Molecule
, keyboardSupport =
[ { keys = [ Enter ], result = "Select the focused control" }
, { keys = [ Space ], result = "Select the focused control" }
, { keys = [ Tab ], result = "Focus the next focusable element" }
, { keys = [ Tab, Shift ], result = "Focus the previous focusable element" }
]
2018-03-24 05:05:34 +03:00
}
2020-08-05 05:30:24 +03:00
type Page
= Flag
| Sprout
| Star
| Sapling
| Attention
| Tree
| Premium
| Activity
buildOptions : Bool -> List (SegmentedControl.Option Page Msg)
buildOptions keepIcon =
let
2020-08-05 07:28:01 +03:00
buildOption value icon =
2020-08-05 05:30:24 +03:00
{ icon = ifIcon icon
, label = Debug.toString value
, value = value
, attributes = []
, content = Html.text <| "Content for " ++ Debug.toString value
}
ifIcon icon =
if keepIcon then
Just icon
else
Nothing
in
2020-08-05 07:28:01 +03:00
[ buildOption Flag UiIcon.flag
, buildOption Sprout UiIcon.sprout
, buildOption Star UiIcon.star
, buildOption Sapling UiIcon.sapling
, buildOption Attention <| Svg.withColor Colors.greenDark UiIcon.attention
, buildOption Tree UiIcon.tree
, buildOption Premium UiIcon.premiumLock
, buildOption Activity <| Svg.withColor Colors.purple UiIcon.activity
2020-07-29 21:10:49 +03:00
]
2020-08-05 07:28:01 +03:00
buildSelectOptions : Bool -> List (SegmentedControl.SelectOption Int msg)
buildSelectOptions keepIcon =
2020-08-05 05:06:28 +03:00
let
2020-08-05 07:28:01 +03:00
buildOption value icon =
{ icon = ifIcon icon
, label = "Source " ++ Debug.toString value
, value = value
2020-08-05 04:17:24 +03:00
, attributes = []
}
2020-08-05 07:28:01 +03:00
ifIcon icon =
if keepIcon then
Just icon
else
Nothing
in
2020-08-05 07:28:01 +03:00
List.indexedMap buildOption
[ UiIcon.leaderboard
, UiIcon.person
, UiIcon.performance
, UiIcon.gift
, UiIcon.document
, UiIcon.key
, UiIcon.badge
, UiIcon.hat
]
{-| -}
type alias State =
2020-08-05 05:30:24 +03:00
{ page : Page
, optionallySelected : Maybe Int
, optionsControl : Control Options
}
2018-03-24 05:05:34 +03:00
{-| -}
2019-10-24 21:08:28 +03:00
init : State
init =
2020-08-05 05:30:24 +03:00
{ page = Flag
, optionallySelected = Nothing
, optionsControl = optionsControl
}
type alias Options =
{ width : SegmentedControl.Width
, icon : Bool
2020-07-29 21:10:49 +03:00
, count : Int
2018-03-24 05:05:34 +03:00
}
optionsControl : Control Options
optionsControl =
Control.record Options
|> Control.field "width"
(Control.choice
[ ( "FitContent", Control.value SegmentedControl.FitContent )
, ( "FillContainer", Control.value SegmentedControl.FillContainer )
]
)
|> Control.field "icon" (Control.bool False)
2020-07-29 21:10:49 +03:00
|> Control.field "count"
(Control.choice
(List.map (\i -> ( String.fromInt i, Control.value i )) (List.range 2 8))
)
{-| -}
type Msg
2020-08-05 05:30:24 +03:00
= SelectPage Page
| MaybeSelect Int
| ChangeOptions (Control Options)
2018-03-24 05:05:34 +03:00
{-| -}
update : Msg -> State -> ( State, Cmd Msg )
update msg state =
case msg of
2020-08-05 05:30:24 +03:00
SelectPage page ->
( { state | page = page }
2020-05-20 21:58:21 +03:00
, Cmd.none
)
2020-05-20 22:23:13 +03:00
2020-05-20 21:58:21 +03:00
MaybeSelect id ->
( { state | optionallySelected = Just id }
, Cmd.none
)
ChangeOptions newOptions ->
( { state | optionsControl = newOptions }
, Cmd.none
)