2020-07-30 21:12:30 +03:00
|
|
|
module Examples.RadioButton exposing
|
|
|
|
( example
|
|
|
|
, State, Msg
|
|
|
|
)
|
|
|
|
|
|
|
|
{-|
|
|
|
|
|
|
|
|
@docs example
|
|
|
|
@docs State, Msg
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
2021-11-09 23:40:10 +03:00
|
|
|
import Browser.Dom as Dom
|
2020-07-30 21:12:30 +03:00
|
|
|
import Category exposing (Category(..))
|
|
|
|
import Css exposing (..)
|
2020-07-30 21:28:18 +03:00
|
|
|
import Debug.Control as Control exposing (Control)
|
2020-07-30 21:12:30 +03:00
|
|
|
import Dict exposing (Dict)
|
|
|
|
import Example exposing (Example)
|
2020-07-30 21:28:18 +03:00
|
|
|
import Html.Styled as Html exposing (..)
|
2020-07-30 21:12:30 +03:00
|
|
|
import Html.Styled.Attributes exposing (css)
|
|
|
|
import KeyboardSupport exposing (Direction(..), Key(..))
|
2020-07-30 21:39:16 +03:00
|
|
|
import Nri.Ui.Button.V10 as Button
|
2020-07-30 21:28:18 +03:00
|
|
|
import Nri.Ui.Data.PremiumLevel as PremiumLevel exposing (PremiumLevel)
|
2020-07-30 21:12:30 +03:00
|
|
|
import Nri.Ui.Heading.V2 as Heading
|
2021-11-09 23:40:10 +03:00
|
|
|
import Nri.Ui.Modal.V11 as Modal
|
2021-11-16 22:05:59 +03:00
|
|
|
import Nri.Ui.RadioButton.V3 as RadioButton
|
2021-10-27 20:42:23 +03:00
|
|
|
import Nri.Ui.Text.V6 as Text
|
2021-11-09 23:40:10 +03:00
|
|
|
import Task
|
2020-07-30 21:12:30 +03:00
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
example : Example State Msg
|
|
|
|
example =
|
2020-09-09 21:43:10 +03:00
|
|
|
{ name = "RadioButton"
|
2021-11-16 22:05:59 +03:00
|
|
|
, version = 3
|
2020-07-30 21:12:30 +03:00
|
|
|
, state = init
|
|
|
|
, update = update
|
2020-07-30 21:39:16 +03:00
|
|
|
, subscriptions = subscriptions
|
2021-11-05 21:19:08 +03:00
|
|
|
, preview = []
|
2020-07-30 21:12:30 +03:00
|
|
|
, view = view
|
2021-09-04 18:47:56 +03:00
|
|
|
, categories = [ Inputs ]
|
2020-07-30 21:47:32 +03:00
|
|
|
, keyboardSupport =
|
2020-11-20 21:42:26 +03:00
|
|
|
[ { keys = [ Arrow Left ]
|
2020-11-07 02:57:10 +03:00
|
|
|
, result = "Move the focus & select the radio button to the left"
|
|
|
|
}
|
2020-11-20 21:42:26 +03:00
|
|
|
, { keys = [ Arrow Right ]
|
2020-11-07 02:57:10 +03:00
|
|
|
, result = "Move the focus & select the radio button to the right"
|
|
|
|
}
|
2020-11-20 21:42:26 +03:00
|
|
|
, { keys = [ Space ]
|
2020-11-07 02:57:10 +03:00
|
|
|
, result = "Select the current radio button"
|
|
|
|
}
|
|
|
|
]
|
2020-07-30 21:12:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
view : State -> List (Html Msg)
|
|
|
|
view model =
|
|
|
|
[ Heading.h3 [] [ Html.text "RadioButton" ]
|
2020-07-30 21:28:18 +03:00
|
|
|
, Heading.h4 [] [ Html.text "view" ]
|
|
|
|
, viewVanilla model
|
|
|
|
, Heading.h4 [] [ Html.text "premium" ]
|
2021-11-09 23:40:10 +03:00
|
|
|
, Modal.view
|
2020-08-12 17:15:23 +03:00
|
|
|
{ title = "Go Premium!"
|
2020-07-30 21:39:16 +03:00
|
|
|
, wrapMsg = ModalMsg
|
2021-11-09 23:40:10 +03:00
|
|
|
, content = [ Text.mediumBody [ Text.plaintext "Often, we'll launch a modal showing the benefits of premium when a locked radio button is clicked." ] ]
|
|
|
|
, footer =
|
|
|
|
[ Button.button "Okay"
|
|
|
|
[ Button.modal
|
|
|
|
, Button.onClick CloseModal
|
|
|
|
, Button.id "close-premium-modal"
|
|
|
|
]
|
|
|
|
]
|
|
|
|
, focusTrap =
|
|
|
|
{ focus = Focus
|
|
|
|
, firstId = Modal.closeButtonId
|
|
|
|
, lastId = "close-premium-modal"
|
|
|
|
}
|
2020-07-30 21:39:16 +03:00
|
|
|
}
|
2021-11-09 23:40:10 +03:00
|
|
|
[ Modal.closeButton ]
|
2020-07-30 21:39:16 +03:00
|
|
|
model.modal
|
2020-07-30 21:12:30 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-07-30 21:28:18 +03:00
|
|
|
viewVanilla : State -> Html Msg
|
|
|
|
viewVanilla state =
|
2021-09-04 18:49:10 +03:00
|
|
|
div []
|
2020-07-30 21:28:18 +03:00
|
|
|
[ RadioButton.view
|
2021-11-19 02:18:50 +03:00
|
|
|
(selectionToString Dogs)
|
2021-11-19 00:23:43 +03:00
|
|
|
[ RadioButton.enabled
|
2021-11-19 02:14:44 +03:00
|
|
|
, RadioButton.value Dogs
|
|
|
|
, RadioButton.name "pets"
|
2021-11-19 01:01:48 +03:00
|
|
|
, RadioButton.selectedValue state.selectedValue
|
2021-11-19 01:48:49 +03:00
|
|
|
, RadioButton.onSelect Select
|
2021-11-19 02:14:44 +03:00
|
|
|
, RadioButton.valueToString selectionToString
|
2021-11-17 20:01:25 +03:00
|
|
|
]
|
2020-07-30 21:28:18 +03:00
|
|
|
, RadioButton.view
|
2021-11-19 02:18:50 +03:00
|
|
|
(selectionToString Cats)
|
2021-11-19 00:23:43 +03:00
|
|
|
[ RadioButton.enabled
|
2021-11-19 02:14:44 +03:00
|
|
|
, RadioButton.value Cats
|
|
|
|
, RadioButton.name "pets"
|
2021-11-19 01:01:48 +03:00
|
|
|
, RadioButton.selectedValue state.selectedValue
|
2021-11-19 01:48:49 +03:00
|
|
|
, RadioButton.onSelect Select
|
2021-11-19 02:14:44 +03:00
|
|
|
, RadioButton.valueToString selectionToString
|
2021-11-19 01:48:49 +03:00
|
|
|
]
|
|
|
|
, RadioButton.view
|
2021-11-19 02:18:50 +03:00
|
|
|
(selectionToString Robots)
|
2021-11-19 02:44:51 +03:00
|
|
|
[ RadioButton.premium
|
|
|
|
{ teacherPremiumLevel = PremiumLevel.Premium
|
|
|
|
, contentPremiumLevel = PremiumLevel.PremiumWithWriting
|
|
|
|
}
|
2021-11-19 02:14:44 +03:00
|
|
|
, RadioButton.value Robots
|
|
|
|
, RadioButton.name "pets"
|
2021-11-19 01:48:49 +03:00
|
|
|
, RadioButton.selectedValue state.selectedValue
|
|
|
|
, RadioButton.onSelect Select
|
2021-11-19 02:14:44 +03:00
|
|
|
, RadioButton.valueToString selectionToString
|
2021-11-19 03:07:21 +03:00
|
|
|
, RadioButton.showPennant <| OpenModal ""
|
2021-11-19 00:23:43 +03:00
|
|
|
]
|
2020-07-30 21:28:18 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-11-19 02:14:44 +03:00
|
|
|
type Selection
|
|
|
|
= Dogs
|
|
|
|
| Cats
|
|
|
|
| Robots
|
|
|
|
|
|
|
|
|
|
|
|
selectionToString : Selection -> String
|
|
|
|
selectionToString selection =
|
|
|
|
case selection of
|
|
|
|
Dogs ->
|
|
|
|
"Dogs"
|
|
|
|
|
|
|
|
Cats ->
|
|
|
|
"Cats"
|
|
|
|
|
|
|
|
Robots ->
|
|
|
|
"Robots"
|
|
|
|
|
|
|
|
|
2020-07-30 21:28:18 +03:00
|
|
|
{-| -}
|
|
|
|
type alias State =
|
2021-11-19 02:14:44 +03:00
|
|
|
{ selectedValue : Maybe Selection
|
2020-07-30 21:39:16 +03:00
|
|
|
, modal : Modal.Model
|
2020-07-30 21:28:18 +03:00
|
|
|
, premiumControl : Control PremiumConfig
|
|
|
|
}
|
2020-07-30 21:12:30 +03:00
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
init : State
|
|
|
|
init =
|
2020-07-30 21:28:18 +03:00
|
|
|
{ selectedValue = Nothing
|
2020-07-30 21:39:16 +03:00
|
|
|
, modal = Modal.init
|
2020-07-30 21:28:18 +03:00
|
|
|
, premiumControl = initPremiumControls
|
|
|
|
}
|
2020-07-30 21:12:30 +03:00
|
|
|
|
|
|
|
|
2020-07-30 21:28:18 +03:00
|
|
|
type alias PremiumConfig =
|
|
|
|
{ teacherPremiumLevel : PremiumLevel
|
|
|
|
, showPennant : Bool
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
initPremiumControls : Control PremiumConfig
|
|
|
|
initPremiumControls =
|
|
|
|
Control.record PremiumConfig
|
|
|
|
|> Control.field "teacherPremiumLevel"
|
|
|
|
(Control.choice
|
|
|
|
[ ( "Free", Control.value PremiumLevel.Free )
|
|
|
|
, ( "Premium", Control.value PremiumLevel.PremiumWithWriting )
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Control.field "showPennant" (Control.bool False)
|
|
|
|
|
|
|
|
|
|
|
|
type Msg
|
2021-11-09 23:40:10 +03:00
|
|
|
= OpenModal String
|
|
|
|
| ModalMsg Modal.Msg
|
|
|
|
| CloseModal
|
2021-11-19 02:14:44 +03:00
|
|
|
| Select Selection
|
2020-07-30 21:28:18 +03:00
|
|
|
| SetPremiumControl (Control PremiumConfig)
|
2021-11-09 23:40:10 +03:00
|
|
|
| Focus String
|
|
|
|
| Focused (Result Dom.Error ())
|
2020-07-30 21:12:30 +03:00
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
update : Msg -> State -> ( State, Cmd Msg )
|
|
|
|
update msg model =
|
2020-07-30 21:28:18 +03:00
|
|
|
case msg of
|
2021-11-09 23:40:10 +03:00
|
|
|
OpenModal returnFocusTo ->
|
|
|
|
let
|
|
|
|
( modal, cmd ) =
|
|
|
|
Modal.open
|
|
|
|
{ startFocusOn = Modal.closeButtonId
|
|
|
|
, returnFocusTo = returnFocusTo
|
|
|
|
}
|
|
|
|
in
|
|
|
|
( { model | modal = modal }, Cmd.map ModalMsg cmd )
|
|
|
|
|
2020-07-30 21:39:16 +03:00
|
|
|
ModalMsg modalMsg ->
|
|
|
|
let
|
|
|
|
( modal, cmd ) =
|
|
|
|
Modal.update { dismissOnEscAndOverlayClick = True }
|
|
|
|
modalMsg
|
|
|
|
model.modal
|
|
|
|
in
|
|
|
|
( { model | modal = modal }, Cmd.map ModalMsg cmd )
|
2020-07-30 21:28:18 +03:00
|
|
|
|
2021-11-09 23:40:10 +03:00
|
|
|
CloseModal ->
|
|
|
|
let
|
|
|
|
( modal, cmd ) =
|
|
|
|
Modal.close model.modal
|
|
|
|
in
|
|
|
|
( { model | modal = modal }, Cmd.map ModalMsg cmd )
|
|
|
|
|
2020-07-30 21:28:18 +03:00
|
|
|
Select value ->
|
|
|
|
( { model | selectedValue = Just value }, Cmd.none )
|
|
|
|
|
|
|
|
SetPremiumControl premiumControl ->
|
|
|
|
( { model | premiumControl = premiumControl }, Cmd.none )
|
|
|
|
|
2021-11-09 23:40:10 +03:00
|
|
|
Focus focus ->
|
|
|
|
( model, Task.attempt Focused (Dom.focus focus) )
|
|
|
|
|
|
|
|
Focused _ ->
|
2020-07-30 21:28:18 +03:00
|
|
|
( model, Cmd.none )
|
2020-07-30 21:39:16 +03:00
|
|
|
|
|
|
|
|
|
|
|
subscriptions : State -> Sub Msg
|
|
|
|
subscriptions { modal } =
|
|
|
|
Sub.map ModalMsg (Modal.subscriptions modal)
|