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
|
|
|
|
|
|
|
-}
|
|
|
|
|
2020-03-24 03:33:42 +03:00
|
|
|
import Category exposing (Category(..))
|
2020-01-27 19:16:22 +03:00
|
|
|
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
|
2019-11-16 01:33:59 +03:00
|
|
|
import Html.Styled.Attributes
|
2020-06-20 00:45:32 +03:00
|
|
|
import KeyboardSupport exposing (Direction(..), Key(..))
|
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 ]
|
2020-06-20 00:45:32 +03:00
|
|
|
, keyboardSupport = []
|
2020-03-31 22:43:32 +03:00
|
|
|
, view =
|
|
|
|
\state ->
|
2021-11-04 20:28:05 +03:00
|
|
|
let
|
2021-11-04 20:44:47 +03:00
|
|
|
attributes : Settings
|
2021-11-04 20:28:05 +03:00
|
|
|
attributes =
|
|
|
|
Control.currentValue state.attributes
|
|
|
|
in
|
|
|
|
[ Control.view UpdateAttributes state.attributes
|
|
|
|
|> Html.Styled.fromUnstyled
|
2021-11-04 21:23:26 +03:00
|
|
|
, Select.view "Tortilla Selector" attributes
|
2020-03-31 22:43:32 +03:00
|
|
|
|> Html.Styled.map ConsoleLog
|
|
|
|
, Html.Styled.div
|
|
|
|
[ Html.Styled.Attributes.css [ Css.maxWidth (Css.px 400) ] ]
|
2021-11-04 20:03:03 +03:00
|
|
|
[ Select.view "Selector with Overflowed Text"
|
2021-11-04 20:52:25 +03:00
|
|
|
[ Select.defaultDisplayText "Look at me, I design coastlines, I got an award for Norway. Where's the sense in that?"
|
|
|
|
]
|
2020-03-31 22:43:32 +03:00
|
|
|
|> Html.Styled.map ConsoleLog
|
|
|
|
]
|
2020-01-27 19:16:22 +03:00
|
|
|
]
|
2019-05-17 02:56:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-04 20:28:05 +03:00
|
|
|
{-| -}
|
|
|
|
type alias State =
|
2021-11-04 20:44:47 +03:00
|
|
|
{ attributes : Control Settings
|
2021-11-04 20:28:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-17 01:39:57 +03:00
|
|
|
{-| -}
|
2020-01-16 22:47:42 +03:00
|
|
|
init : State
|
2018-04-17 01:39:57 +03:00
|
|
|
init =
|
2021-11-04 20:28:05 +03:00
|
|
|
{ attributes = initControls
|
|
|
|
}
|
2020-01-16 22:47:42 +03:00
|
|
|
|
|
|
|
|
2021-11-04 20:44:47 +03:00
|
|
|
type alias Settings =
|
|
|
|
List (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:23:26 +03:00
|
|
|
(Control.map (Select.choices identity) initChoices)
|
2021-11-04 20:52:25 +03:00
|
|
|
|> ControlExtra.optionalListItem "defaultDisplayText"
|
|
|
|
(Control.map Select.defaultDisplayText <|
|
|
|
|
Control.string "Select a tasty tortilla based treat!"
|
|
|
|
)
|
2021-11-04 20:28:05 +03:00
|
|
|
|> ControlExtra.optionalListItem "errorIf"
|
|
|
|
(Control.map Select.errorIf <| Control.bool True)
|
|
|
|
|> ControlExtra.optionalListItem "errorMessage"
|
2021-11-04 20:52:25 +03:00
|
|
|
(Control.map (Just >> Select.errorMessage) <|
|
|
|
|
Control.string "The right item must be selected."
|
|
|
|
)
|
2020-01-16 22:47:42 +03:00
|
|
|
|
|
|
|
|
2021-11-04 21:06:31 +03:00
|
|
|
initChoices : Control (List (Choice String))
|
|
|
|
initChoices =
|
|
|
|
ControlExtra.list
|
|
|
|
|> initChoice "value-1" "Tacos"
|
|
|
|
|> initChoice "value-2" "Burritos"
|
|
|
|
|> initChoice "value-3" "Enchiladas"
|
|
|
|
|
|
|
|
|
|
|
|
initChoice : String -> String -> Control (List (Choice String)) -> Control (List (Choice String))
|
|
|
|
initChoice value default =
|
|
|
|
let
|
|
|
|
toChoice label =
|
|
|
|
{ label = label, value = value }
|
|
|
|
in
|
|
|
|
ControlExtra.listItem value (Control.map toChoice (Control.string default))
|
|
|
|
|
|
|
|
|
2020-01-16 22:47:42 +03:00
|
|
|
{-| -}
|
|
|
|
type Msg
|
|
|
|
= ConsoleLog String
|
2021-11-04 20:44:47 +03:00
|
|
|
| UpdateAttributes (Control Settings)
|
2019-05-17 02:56:06 +03:00
|
|
|
|
|
|
|
|
2018-04-17 01:39:57 +03:00
|
|
|
{-| -}
|
2020-01-16 22:47:42 +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 )
|
|
|
|
|
|
|
|
UpdateAttributes attributes ->
|
|
|
|
( { state | attributes = attributes }
|
|
|
|
, Cmd.none
|
|
|
|
)
|