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

145 lines
3.8 KiB
Elm
Raw Normal View History

2020-12-10 20:56:52 +03:00
module Examples.Switch exposing (Msg, State, example)
{-|
@docs Msg, State, example
-}
import Accessibility.Styled.Key as Key
2020-12-10 20:56:52 +03:00
import Category
2022-04-19 05:00:21 +03:00
import CommonControls
2022-04-19 04:57:22 +03:00
import Debug.Control as Control exposing (Control)
import Debug.Control.Extra as ControlExtra
import Debug.Control.View as ControlView
2020-12-10 20:56:52 +03:00
import Example exposing (Example)
2022-04-19 04:38:46 +03:00
import KeyboardSupport exposing (Key(..))
2022-04-19 03:40:39 +03:00
import Nri.Ui.Switch.V2 as Switch
2020-12-10 20:56:52 +03:00
2022-04-19 04:57:22 +03:00
moduleName : String
moduleName =
"Switch"
2020-12-10 20:56:52 +03:00
2022-04-19 04:57:22 +03:00
version : Int
version =
2
2020-12-10 20:56:52 +03:00
example : Example State Msg
example =
2022-04-19 04:57:22 +03:00
{ name = moduleName
, version = version
, state = init
, update = update
2020-12-10 20:56:52 +03:00
, subscriptions = \_ -> Sub.none
, preview =
[ Switch.view { label = "Toggle Off", id = "preview-switch-a" }
[ Switch.selected False
, Switch.custom [ Key.tabbable False ]
]
, Switch.view { label = "Toggle On", id = "preview-switch-b" }
[ Switch.selected True
, Switch.custom [ Key.tabbable False ]
]
]
2020-12-10 20:56:52 +03:00
, view =
2022-04-19 04:57:22 +03:00
\ellieLinkConfig state ->
let
currentValue =
Control.currentValue state.settings
in
[ ControlView.view
{ ellieLinkConfig = ellieLinkConfig
, name = moduleName
, version = version
, update = UpdateSettings
, settings = state.settings
, mainType = "RootHtml.Html msg"
, extraImports = []
, toExampleCode =
\{ label, attributes } ->
[ { sectionName = "Example"
, code =
moduleName
++ ".view"
++ " \""
++ label
++ "\"\t"
++ ControlView.codeFromListWithHardcoded
[ "Switch.selected "
++ Debug.toString state.selected
++ "\n-- , Switch.onSwitch Switch -- <- you'll need to wire in a Msg for the Switch to work"
]
attributes
}
]
}
2022-04-19 04:57:22 +03:00
, Switch.view { label = currentValue.label, id = "view-switch-example" }
(Switch.selected state.selected
:: Switch.onSwitch Switch
:: List.map Tuple.second currentValue.attributes
)
2020-12-10 20:56:52 +03:00
]
, categories = [ Category.Inputs ]
2022-04-19 04:38:46 +03:00
, keyboardSupport =
[ { keys = [ Space ]
, result = "Toggle the Switch state"
}
]
2020-12-10 20:56:52 +03:00
}
2022-04-19 04:57:22 +03:00
{-| -}
type alias State =
{ selected : Bool
, settings : Control Settings
}
init : State
init =
{ selected = True
, settings = controlSettings
}
type alias Settings =
{ label : String
, attributes : List ( String, Switch.Attribute Msg )
}
controlSettings : Control Settings
controlSettings =
Control.record Settings
|> Control.field "label" (Control.string "Show pandas in results")
|> Control.field "attributes" initAttributes
initAttributes : Control (List ( String, Switch.Attribute msg ))
initAttributes =
ControlExtra.list
2022-04-19 05:00:21 +03:00
|> CommonControls.disabledListItem moduleName Switch.disabled
2022-04-19 04:57:22 +03:00
{-| -}
type Msg
= Switch Bool
| UpdateSettings (Control Settings)
update : Msg -> State -> ( State, Cmd Msg )
update msg state =
case msg of
Switch bool ->
( { state | selected = bool }
, Cmd.none
)
UpdateSettings settings ->
( { state | settings = settings }
, Cmd.none
)