Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chooser empty content #73

Merged
merged 3 commits into from
Mar 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions source/Ui/Chooser.elm
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module Ui.Chooser exposing
( Model, Item, Msg, init, onChange, update, view, render, subscriptions
, close, toggleItem, getFirstSelected, updateData, selectFirst, setValue
, placeholder, closeOnSelect, deselectable, searchable, multiple, items
, renderWhenClosed )
, placeholder, emptyContent, closeOnSelect, deselectable, searchable
, multiple, items, renderWhenClosed
)

{-| This is a component for selecting a single / multiple items
form a list of choices, with lots of options.
Expand All @@ -14,7 +15,7 @@ form a list of choices, with lots of options.
@docs onChange

# DSL
@docs placeholder, closeOnSelect, deselectable, searchable, multiple, items
@docs placeholder, emptyContent, closeOnSelect, deselectable, searchable, multiple, items
@docs renderWhenClosed

# View
Expand Down Expand Up @@ -91,6 +92,7 @@ type alias Model =
, renderWhenClosed : Bool
, selected : Set String
, placeholder : String
, emptyContent : Html.Html Msg
, closeOnSelect : Bool
, deselectable : Bool
, intended : String
Expand Down Expand Up @@ -135,11 +137,12 @@ init _ =
, closeOnSelect = False
, deselectable = False
, selected = Set.empty
, placeholder = ""
, emptyContent = text "No items to display!"
, searchable = False
, multiple = False
, disabled = False
, readonly = False
, placeholder = ""
, uid = Uid.uid ()
, intended = ""
, value = ""
Expand Down Expand Up @@ -168,6 +171,12 @@ placeholder : String -> Model -> Model
placeholder value model =
{ model | placeholder = value }

{-| Sets the text for when there are no items to display.
-}
emptyContent : Html.Html Msg -> Model -> Model
emptyContent value model =
{ model | emptyContent = value }


{-| Sets whether or not to close the dropdown when selecting an item.
-}
Expand Down Expand Up @@ -306,10 +315,14 @@ view model =
render : Model -> Html.Html Msg
render model =
let
children =
children =
if model.dropdown.open || (not model.dropdown.open && model.renderWhenClosed) then
(List.map (Html.Lazy.lazy2 renderItem model) (items_ model))
else
if List.length (items_ model) > 0 then
(List.map (Html.Lazy.lazy2 renderItem model) (items_ model))
else
[ node "ui-chooser-empty-content" [] [ model.emptyContent ]
]
else
[]

val =
Expand Down
19 changes: 9 additions & 10 deletions source/Ui/Styles/Chooser.elm
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import Ui.Styles exposing (Style)

import Regex

{-| Styles for a checkbox using the default theme.
{-| Styles for a chooser using the default theme.
-}
defaultStyle : Style
defaultStyle =
Ui.Styles.attributes (style Theme.default)
Ui.Styles.attributes <| style Theme.default


{-| Returns the style node for a checkbox using the given theme.
{-| Returns the style node for a chooser using the given theme.
-}
style : Theme -> Node
style theme =
Expand Down Expand Up @@ -110,14 +110,13 @@ style theme =
[ maxHeight (px 250)
, padding (px 5)
, display flex
]

, selector "ui-scrolled-panel-wrapper:empty:before"
[ contentString "No items to display!"
, fontStyle italic
, padding (px 12)
, display block
, opacity 0.5
]
, selector "ui-chooser-empty-content"
[ fontStyle italic
, padding (px 12)
, display block
, opacity 0.5
]

, selector "ui-chooser-item"
Expand Down
36 changes: 22 additions & 14 deletions spec/Ui/ChooserSpec.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ import Ui.Chooser

import Steps exposing (..)

items : List Ui.Chooser.Item
items =
[ { id = "0", label = "Superman", value = "superman" }
, { id = "1", label = "Batman", value = "batman" }
, { id = "2", label = "Hello", value = "hello" }
]

view : Ui.Chooser.Model -> Html.Html Ui.Chooser.Msg
view model =
Ui.Container.row []
Expand All @@ -26,18 +19,33 @@ view model =
specs : Node
specs =
describe "Ui.Chooser"
[
[ it "Placeholder should be 'Select something...'"
[ assert.attributeEquals
{ text = "Select something..."
, selector = "input"
, attribute = "placeholder"
}
]
, it "emptyContent should be 'No items.'"
[ assert.containsText
{ text = "No items."
, selector = "ui-chooser-empty-content"
}
]
]


init : Ui.Chooser.Model
init = Ui.Chooser.init ()
|> Ui.Chooser.placeholder "Select something..."
|> Ui.Chooser.emptyContent (Html.text "No items.")
|> Ui.Chooser.deselectable True
|> Ui.Chooser.multiple True

main =
runWithProgram
{ subscriptions = Ui.Chooser.subscriptions
, update = Ui.Chooser.update
, init = \() ->
Ui.Chooser.init ()
|> Ui.Chooser.placeholder "Select something..."
|> Ui.Chooser.items items
|> Ui.Chooser.deselectable True
|> Ui.Chooser.multiple True
, init = \() -> init
, view = view
} specs