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

232 lines
7.1 KiB
Elm
Raw Normal View History

2020-03-31 22:43:32 +03:00
module Examples.Select exposing (Msg, State, example)
2018-04-17 01:39:57 +03:00
{-|
2020-03-31 22:43:32 +03:00
@docs Msg, State, example
2018-04-17 01:39:57 +03:00
-}
2021-11-09 01:55:56 +03:00
import Accessibility.Styled.Key as Key
import Category exposing (Category(..))
import Css
2021-11-04 20:28:05 +03:00
import Debug.Control as Control exposing (Control)
import Debug.Control.Extra as ControlExtra
2020-03-31 23:20:03 +03:00
import Example exposing (Example)
2018-07-11 03:50:39 +03:00
import Html.Styled
2021-11-04 21:56:59 +03:00
import Html.Styled.Attributes exposing (css)
import KeyboardSupport exposing (Direction(..), Key(..))
import Nri.Ui.Colors.V1 as Colors
2019-11-16 01:33:59 +03:00
import Nri.Ui.Heading.V2 as Heading
2021-11-04 21:06:31 +03:00
import Nri.Ui.Select.V8 as Select exposing (Choice)
2018-04-17 01:39:57 +03:00
{-| -}
2020-03-31 23:20:03 +03:00
example : Example State Msg
2020-03-31 22:43:32 +03:00
example =
2020-09-09 21:43:10 +03:00
{ name = "Select"
2021-11-04 19:33:24 +03:00
, version = 8
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
, categories = [ Inputs ]
, keyboardSupport = []
2021-11-09 01:55:56 +03:00
, preview =
[ Select.view "Label" [ Select.custom [ Key.tabbable False ] ]
, Select.view "Hidden label"
[ Select.hiddenLabel
, Select.defaultDisplayText "Hidden label"
, Select.custom [ Key.tabbable False ]
]
]
2020-03-31 22:43:32 +03:00
, view =
\state ->
2021-11-04 21:56:59 +03:00
let
label =
Control.currentValue state.label
( attributesCode, attributes ) =
List.unzip (Control.currentValue state.attributes)
in
2021-11-04 21:36:37 +03:00
[ Control.view UpdateLabel state.label
|> Html.Styled.fromUnstyled
, Control.view UpdateAttributes state.attributes
2021-11-04 20:28:05 +03:00
|> Html.Styled.fromUnstyled
, Html.Styled.div
[ css [ Css.displayFlex, Css.alignItems Css.flexStart ]
2021-11-04 21:56:59 +03:00
]
[ Html.Styled.code
[ css
[ Css.display Css.block
, Css.margin2 (Css.px 20) Css.zero
, Css.whiteSpace Css.preWrap
, Css.maxWidth (Css.px 500)
]
]
[ Html.Styled.text <|
"Select.view \""
++ label
++ "\""
++ "\n [ "
++ String.join "\n , " attributesCode
++ "\n ] "
]
, Select.view label attributes
|> Html.Styled.map ConsoleLog
2021-11-04 21:56:59 +03:00
]
]
}
2021-11-04 20:28:05 +03:00
{-| -}
type alias State =
2021-11-04 21:36:37 +03:00
{ label : Control String
, attributes : Control Settings
2021-11-04 20:28:05 +03:00
}
2018-04-17 01:39:57 +03:00
{-| -}
init : State
2018-04-17 01:39:57 +03:00
init =
2021-11-04 21:36:37 +03:00
{ label = Control.string "Tortilla Selector"
, attributes = initControls
2021-11-04 20:28:05 +03:00
}
type alias Settings =
2021-11-04 21:56:59 +03:00
List ( String, Select.Attribute String )
initControls : Control Settings
2021-11-04 20:28:05 +03:00
initControls =
ControlExtra.list
2021-11-04 21:06:31 +03:00
|> ControlExtra.listItem "choices"
2021-11-04 21:56:59 +03:00
(Control.map
(\( code, choices ) ->
( "Select.choices identity" ++ code
, Select.choices identity choices
)
)
initChoices
)
2021-11-04 22:37:03 +03:00
|> ControlExtra.optionalListItem "hiddenLabel"
(Control.value ( "Select.hiddenLabel", Select.hiddenLabel ))
|> ControlExtra.optionalListItem "defaultDisplayText"
2021-11-04 21:56:59 +03:00
(Control.map
(\str ->
( "Select.defaultDisplayText \"" ++ str ++ "\""
, Select.defaultDisplayText str
)
)
(Control.string "Select a tasty tortilla based treat!")
)
|> ControlExtra.optionalListItem "containerCss"
(Control.choice
[ ( "flex-basis: 300px"
, Control.value
( "Select.containerCss [ Css.flexBasis (Css.px 300) ]"
, Select.containerCss [ Css.flexBasis (Css.px 300) ]
)
)
, ( "background-color: lichen"
, Control.value
( "Select.containerCss [ Css.backgroundColor Colors.lichen ]"
, Select.containerCss [ Css.backgroundColor Colors.lichen ]
)
)
]
)
|> ControlExtra.optionalListItem "noMargin"
(Control.map
(\bool ->
( "Select.noMargin " ++ Debug.toString bool
, Select.noMargin bool
)
)
(Control.bool True)
)
|> ControlExtra.optionalListItem "errorIf"
2021-11-04 21:56:59 +03:00
(Control.map
(\bool ->
( "Select.errorIf " ++ Debug.toString bool
, Select.errorIf bool
)
)
(Control.bool True)
)
2021-11-04 20:28:05 +03:00
|> ControlExtra.optionalListItem "errorMessage"
2021-11-04 21:56:59 +03:00
(Control.map
(\str ->
( "Select.errorMessage (Just \"" ++ str ++ "\")"
, Select.errorMessage (Just str)
)
)
(Control.string "The right item must be selected.")
)
2021-11-12 01:07:02 +03:00
|> ControlExtra.optionalListItem "guidance"
(Control.map
(\str ->
( "Select.guidance \"" ++ str ++ "\""
, Select.guidance str
)
)
<|
Control.string "The right item must be selected."
)
2021-11-04 21:56:59 +03:00
initChoices : Control ( String, List (Choice String) )
2021-11-04 21:06:31 +03:00
initChoices =
2021-11-04 21:32:46 +03:00
Control.choice
[ ( "Short choices"
2021-11-04 21:56:59 +03:00
, ( """
[ { label = "Tacos", value = "tacos" }
, { label = "Burritos", value = "burritos" }
, { label = "Enchiladas", value = "enchiladas" }
]"""
, [ { label = "Tacos", value = "tacos" }
, { label = "Burritos", value = "burritos" }
, { label = "Enchiladas", value = "enchiladas" }
]
)
2021-11-04 21:32:46 +03:00
|> Control.value
)
, ( "Overflowing text choices"
2021-11-04 21:56:59 +03:00
, ( """
[ { label = "Look at me, I design coastlines, I got an award for Norway. Where's the sense in that? My mistress' eyes are nothing like the sun. Coral be far more red than her lips red.", value = "design-coastlines" }
]"""
, [ { label = "Look at me, I design coastlines, I got an award for Norway. Where's the sense in that? My mistress' eyes are nothing like the sun. Coral be far more red than her lips red.", value = "design-coastlines" }
]
)
2021-11-04 21:32:46 +03:00
|> Control.value
)
]
2021-11-04 21:06:31 +03:00
{-| -}
type Msg
= ConsoleLog String
2021-11-04 21:36:37 +03:00
| UpdateLabel (Control String)
| UpdateAttributes (Control Settings)
2018-04-17 01:39:57 +03:00
{-| -}
update : Msg -> State -> ( State, Cmd Msg )
2018-04-17 01:39:57 +03:00
update msg state =
case msg of
ConsoleLog message ->
let
_ =
Debug.log "SelectExample" message
in
2021-11-04 20:28:05 +03:00
( state, Cmd.none )
2021-11-04 21:36:37 +03:00
UpdateLabel label ->
( { state | label = label }
, Cmd.none
)
2021-11-04 20:28:05 +03:00
UpdateAttributes attributes ->
( { state | attributes = attributes }
, Cmd.none
)