Fix broken on-focus tooltip behavior

This commit is contained in:
Tessa Kelly 2020-09-08 14:35:36 -07:00
parent 688b3190ce
commit 093718d9ed
5 changed files with 28 additions and 42 deletions

View File

@ -153,8 +153,7 @@ type alias Option value msg =
{-| {-|
- `onSelect` : the message to produce when an option is selected by the user - `focusAndSelect` : the message to produce when an option is selected by the user
- `onFocus` : the message to focus an element by id string
- `options`: the list of options available - `options`: the list of options available
- `selected`: the value of the currently-selected option - `selected`: the value of the currently-selected option
- `positioning`: how to position and size the segmented control - `positioning`: how to position and size the segmented control
@ -162,8 +161,7 @@ type alias Option value msg =
-} -}
view : view :
{ onSelect : a -> msg { focusAndSelect : { select : a, focus : Maybe String } -> msg
, onFocus : String -> msg
, options : List (Option a msg) , options : List (Option a msg)
, selected : a , selected : a
, positioning : Positioning , positioning : Positioning
@ -185,8 +183,7 @@ view config =
{ tabList, tabPanels } = { tabList, tabPanels } =
TabsInternal.views TabsInternal.views
{ onSelect = config.onSelect { focusAndSelect = config.focusAndSelect
, onFocus = config.onFocus
, selected = config.selected , selected = config.selected
, tabs = List.map toInternalTab config.options , tabs = List.map toInternalTab config.options
, tabListStyles = , tabListStyles =

View File

@ -112,8 +112,7 @@ view :
{ title : Maybe String { title : Maybe String
, alignment : Alignment , alignment : Alignment
, customSpacing : Maybe Float , customSpacing : Maybe Float
, onSelect : id -> msg , focusAndSelect : { select : id, focus : Maybe String } -> msg
, onFocus : String -> msg
, selected : id , selected : id
, tabs : List (Tab id msg) , tabs : List (Tab id msg)
} }
@ -133,8 +132,7 @@ view config =
{ tabList, tabPanels } = { tabList, tabPanels } =
TabsInternal.views TabsInternal.views
{ onSelect = config.onSelect { focusAndSelect = config.focusAndSelect
, onFocus = config.onFocus
, selected = config.selected , selected = config.selected
, tabs = List.map toInternalTab config.tabs , tabs = List.map toInternalTab config.tabs
, tabStyles = tabStyles config.customSpacing , tabStyles = tabStyles config.customSpacing

View File

@ -23,8 +23,7 @@ import Nri.Ui.Util exposing (dashify)
{-| -} {-| -}
type alias Config id msg = type alias Config id msg =
{ onSelect : id -> msg { focusAndSelect : { select : id, focus : Maybe String } -> msg
, onFocus : String -> msg
, selected : id , selected : id
, tabs : List (Tab id msg) , tabs : List (Tab id msg)
, tabListStyles : List Css.Style , tabListStyles : List Css.Style
@ -85,15 +84,15 @@ viewTab_ config index tab =
else else
AttributesExtra.none AttributesExtra.none
, Attributes.href href , EventExtras.onClickPreventDefaultForLinkWithHref
, EventExtras.onClickPreventDefaultForLinkWithHref (config.onSelect tab.id) (config.focusAndSelect { select = tab.id, focus = Nothing })
] ]
) )
Nothing -> Nothing ->
-- This is for a non-SPA view -- This is for a non-SPA view
( Html.button ( Html.button
, [ Events.onClick (config.onSelect tab.id) , [ Events.onClick (config.focusAndSelect { select = tab.id, focus = Nothing })
] ]
) )
@ -107,7 +106,6 @@ viewTab_ config index tab =
, Widget.selected isSelected , Widget.selected isSelected
, Role.tab , Role.tab
, Attributes.id (tabToId tab.idString) , Attributes.id (tabToId tab.idString)
, Events.onFocus (config.onSelect tab.id)
, Events.on "keyup" <| , Events.on "keyup" <|
Json.Decode.andThen (keyEvents config tab) Events.keyCode Json.Decode.andThen (keyEvents config tab) Events.keyCode
] ]
@ -132,7 +130,7 @@ viewTab_ config index tab =
keyEvents : Config id msg -> Tab id msg -> Int -> Json.Decode.Decoder msg keyEvents : Config id msg -> Tab id msg -> Int -> Json.Decode.Decoder msg
keyEvents { onFocus, tabs } thisTab keyCode = keyEvents { focusAndSelect, tabs } thisTab keyCode =
let let
findAdjacentTab tab acc = findAdjacentTab tab acc =
case acc of case acc of
@ -140,7 +138,7 @@ keyEvents { onFocus, tabs } thisTab keyCode =
acc acc
( True, Nothing ) -> ( True, Nothing ) ->
( True, Just (tabToId tab.idString) ) ( True, Just { select = tab.id, focus = Just (tabToId tab.idString) } )
( False, Nothing ) -> ( False, Nothing ) ->
( tab.id == thisTab.id, Nothing ) ( tab.id == thisTab.id, Nothing )
@ -158,7 +156,7 @@ keyEvents { onFocus, tabs } thisTab keyCode =
-- Right -- Right
case nextTab of case nextTab of
Just next -> Just next ->
Json.Decode.succeed (onFocus next) Json.Decode.succeed (focusAndSelect next)
Nothing -> Nothing ->
Json.Decode.fail "No next tab" Json.Decode.fail "No next tab"
@ -167,7 +165,7 @@ keyEvents { onFocus, tabs } thisTab keyCode =
-- Left -- Left
case previousTab of case previousTab of
Just previous -> Just previous ->
Json.Decode.succeed (onFocus previous) Json.Decode.succeed (focusAndSelect previous)
Nothing -> Nothing ->
Json.Decode.fail "No previous tab" Json.Decode.fail "No previous tab"

View File

@ -49,8 +49,7 @@ example =
, Html.p [ css [ Css.marginTop (Css.px 1) ] ] , Html.p [ css [ Css.marginTop (Css.px 1) ] ]
[ Html.text "Use in cases where it would also be reasonable to use Tabs." ] [ Html.text "Use in cases where it would also be reasonable to use Tabs." ]
, SegmentedControl.view , SegmentedControl.view
{ onSelect = SelectPage { focusAndSelect = FocusAndSelectPage
, onFocus = Focus
, selected = state.page , selected = state.page
, positioning = options.positioning , positioning = options.positioning
, toUrl = Nothing , toUrl = Nothing
@ -221,14 +220,13 @@ optionsControl =
(List.map (\i -> ( String.fromInt i, Control.value i )) (List.range 2 8)) (List.map (\i -> ( String.fromInt i, Control.value i )) (List.range 2 8))
) )
|> Control.field "long content" (Control.bool False) |> Control.field "long content" (Control.bool False)
|> Control.field "tooltips" (Control.bool False) |> Control.field "tooltips" (Control.bool True)
{-| -} {-| -}
type Msg type Msg
= Focus String = FocusAndSelectPage { select : Page, focus : Maybe String }
| Focused (Result Dom.Error ()) | Focused (Result Dom.Error ())
| SelectPage Page
| PageTooltip Page Bool | PageTooltip Page Bool
| SelectRadio Int | SelectRadio Int
| ChangeOptions (Control Options) | ChangeOptions (Control Options)
@ -238,9 +236,10 @@ type Msg
update : Msg -> State -> ( State, Cmd Msg ) update : Msg -> State -> ( State, Cmd Msg )
update msg state = update msg state =
case msg of case msg of
Focus id -> FocusAndSelectPage { select, focus } ->
( state ( { state | page = select }
, Task.attempt Focused (Dom.focus id) , Maybe.map (Task.attempt Focused << Dom.focus) focus
|> Maybe.withDefault Cmd.none
) )
Focused _ -> Focused _ ->
@ -248,11 +247,6 @@ update msg state =
, Cmd.none , Cmd.none
) )
SelectPage page ->
( { state | page = page }
, Cmd.none
)
PageTooltip page isOpen -> PageTooltip page isOpen ->
( { state ( { state
| pageTooltip = | pageTooltip =

View File

@ -81,8 +81,7 @@ type Id
type Msg type Msg
= SelectTab Id = FocusAndSelectTab { select : Id, focus : Maybe String }
| Focus String
| Focused (Result Dom.Error ()) | Focused (Result Dom.Error ())
| SetSettings (Control Settings) | SetSettings (Control Settings)
| ToggleTooltip Id Bool | ToggleTooltip Id Bool
@ -91,11 +90,12 @@ type Msg
update : Msg -> State -> ( State, Cmd Msg ) update : Msg -> State -> ( State, Cmd Msg )
update msg model = update msg model =
case msg of case msg of
SelectTab id -> FocusAndSelectTab { select, focus } ->
( { model | selected = id }, Cmd.none ) ( { model | selected = select }
, focus
Focus id -> |> Maybe.map (Dom.focus >> Task.attempt Focused)
( model, Task.attempt Focused (Dom.focus id) ) |> Maybe.withDefault Cmd.none
)
Focused error -> Focused error ->
( model, Cmd.none ) ( model, Cmd.none )
@ -146,8 +146,7 @@ example =
{ title = settings.title { title = settings.title
, alignment = settings.alignment , alignment = settings.alignment
, customSpacing = settings.customSpacing , customSpacing = settings.customSpacing
, onSelect = SelectTab , focusAndSelect = FocusAndSelectTab
, onFocus = Focus
, selected = model.selected , selected = model.selected
, tabs = allTabs model.openTooltip , tabs = allTabs model.openTooltip
} }