Make selects expand to the full width of parent

There are places where we need the select to be as wide as possible.
Since it seems like a sensible default to have all the selects expand,
we take that approach.

If we find out that this is harder, we can release use the old version.
This commit is contained in:
Hardy Jones 2018-04-18 07:58:19 -07:00
parent 537425f94c
commit 1647b2f358
No known key found for this signature in database
GPG Key ID: 53FEB62789F62A82
3 changed files with 116 additions and 5 deletions

111
src/Nri/Ui/Select/V2.elm Normal file
View File

@ -0,0 +1,111 @@
module Nri.Ui.Select.V2
exposing
( Config
, customView
, styles
, view
)
{-| Build a select input.
# Configure
@docs Config
# Render
@docs view, customView
@docs styles
-}
import Css
import Css.Foreign
import Dict
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Json.Decode exposing (Decoder, andThen, succeed)
import Nri.Ui.Colors.V1
import Nri.Ui.Styles.V1
import Nri.Ui.Util
{-| Select-specific Choice.Config
-}
type alias Config a =
{ choices : List { label : String, value : a }
, current : a
}
{-| TODO: move this to View.Extra
-}
niceId : String -> a -> String
niceId prefix x =
prefix ++ "-" ++ Nri.Ui.Util.dashify (Nri.Ui.Util.removePunctuation (toString x))
{-| A normal select dropdown
-}
view : Config a -> Html a
view config =
customView [] config
{-| A select dropdown with custom attributes.
This should be phased out as the new Select style is implemented,
all pages should use the new, consistent style.
-}
customView : List (Html.Attribute a) -> Config a -> Html a
customView attributes config =
let
valueLookup =
-- TODO: probably worth using Lazy here, since choices won't change often
config.choices
|> List.map (\x -> ( niceId "nri-select" x.value, x.value ))
|> Dict.fromList
decodeValue string =
Dict.get string valueLookup
|> Maybe.map Json.Decode.succeed
|> Maybe.withDefault (Json.Decode.fail ("Nri.Select: could not decode the value: " ++ toString string ++ "\nexpected one of: " ++ toString (Dict.keys valueLookup)))
onSelectHandler =
on "change" (targetValue |> andThen decodeValue)
viewChoice choice =
Html.option
[ Html.Attributes.id (niceId "nri-select" choice.value)
, Html.Attributes.value (niceId "nri-select" choice.value)
, Html.Attributes.selected (choice.value == config.current)
]
[ Html.text choice.label ]
in
config.choices
|> List.map viewChoice
|> Html.select (styles.class [ Select ] :: onSelectHandler :: attributes)
{-| -}
type CssClasses
= Select
{-| -}
styles : Nri.Ui.Styles.V1.Styles Never CssClasses c
styles =
Nri.Ui.Styles.V1.styles "Nri-Ui-Select-V2-"
[ Css.Foreign.class Select
[ Css.backgroundColor Nri.Ui.Colors.V1.white
, Css.border3 (Css.px 1) Css.solid Nri.Ui.Colors.V1.gray75
, Css.borderRadius (Css.px 8)
, Css.color Nri.Ui.Colors.V1.gray20
, Css.cursor Css.pointer
, Css.fontSize (Css.px 15)
, Css.height (Css.px 45)
, Css.width (Css.pct 100)
]
]

View File

@ -21,7 +21,7 @@ module Examples.Select
import Html
import ModuleExample exposing (Category(..), ModuleExample)
import Nri.Ui.Select.V1
import Nri.Ui.Select.V2
{-| -}
@ -36,7 +36,7 @@ type Msg
{-| -}
type alias State value =
Nri.Ui.Select.V1.Config value
Nri.Ui.Select.V2.Config value
{-| -}
@ -45,7 +45,7 @@ example parentMessage state =
{ filename = "ui/src/Nri/Select.elm"
, category = Inputs
, content =
[ Html.map (parentMessage << ConsoleLog) (Nri.Ui.Select.V1.view state)
[ Html.map (parentMessage << ConsoleLog) (Nri.Ui.Select.V2.view state)
]
}

View File

@ -19,7 +19,7 @@ import Nri.Ui.AssetPath as AssetPath exposing (Asset(Asset))
import Nri.Ui.Dropdown.V1
import Nri.Ui.Icon.V2
import Nri.Ui.SegmentedControl.V5
import Nri.Ui.Select.V1
import Nri.Ui.Select.V2
import Nri.Ui.Text.V1 as Text
import Nri.Ui.TextArea.V1 as TextArea
import String.Extra
@ -159,7 +159,7 @@ styles =
, (Nri.Ui.Dropdown.V1.styles |> .css) ()
, (Nri.Ui.Icon.V2.styles |> .css) ()
, (Nri.Ui.SegmentedControl.V5.styles |> .css) ()
, (Nri.Ui.Select.V1.styles |> .css) ()
, (Nri.Ui.Select.V2.styles |> .css) ()
, (Text.styles |> .css) ()
, (TextArea.styles |> .css) assets
]