noredink-ui/styleguide-app/Examples/Table.elm

169 lines
4.8 KiB
Elm
Raw Normal View History

2020-03-31 22:43:32 +03:00
module Examples.Table exposing (Msg, State, example)
{-|
@docs Msg, State, example
-}
2022-05-25 02:44:20 +03:00
import Accessibility.Styled exposing (..)
import Category exposing (Category(..))
import Css exposing (..)
2022-05-25 02:34:40 +03:00
import Debug.Control as Control exposing (Control)
import Debug.Control.View as ControlView
2020-03-31 23:20:03 +03:00
import Example exposing (Example)
2020-06-23 03:46:20 +03:00
import Nri.Ui.Button.V10 as Button
2019-08-05 21:48:02 +03:00
import Nri.Ui.Colors.V1 as Colors
import Nri.Ui.Heading.V2 as Heading
2022-05-25 02:44:20 +03:00
import Nri.Ui.Table.V5 as Table exposing (Column)
{-| -}
2020-03-31 22:43:32 +03:00
type alias State =
2022-05-25 02:34:40 +03:00
Control Settings
2022-05-25 02:34:40 +03:00
moduleName : String
moduleName =
"Table"
version : Int
version =
5
{-| -}
2020-03-31 23:20:03 +03:00
example : Example State Msg
2020-03-31 22:43:32 +03:00
example =
2022-05-25 02:34:40 +03:00
{ name = moduleName
, version = version
, state = controlSettings
, update = update
2020-03-31 22:48:26 +03:00
, subscriptions = \_ -> Sub.none
2021-11-12 01:56:04 +03:00
, categories = [ Layout ]
, keyboardSupport = []
2021-11-06 00:01:10 +03:00
, preview =
[ Table.view
[ Table.string
{ header = "A"
, value = .a
, width = px 50
, cellStyles = always []
}
, Table.string
{ header = "B"
, value = .b
, width = px 50
, cellStyles = always []
}
]
[ { a = "Row 1 A"
, b = "Row 1 B"
}
, { a = "Row 2 A"
, b = "Row 2 B"
}
]
]
2020-03-31 22:43:32 +03:00
, view =
2022-05-25 02:34:40 +03:00
\ellieLinkConfig state ->
2020-03-31 22:43:32 +03:00
let
columns =
[ Table.string
{ header = "First Name"
, value = .firstName
, width = calc (pct 50) minus (px 250)
, cellStyles = always []
}
, Table.string
{ header = "Last Name"
, value = .lastName
, width = calc (pct 50) minus (px 250)
, cellStyles = always []
}
, Table.string
{ header = "# Submitted"
, value = .submitted >> String.fromInt
, width = px 125
, cellStyles =
\value ->
if value.submitted < 5 then
[ backgroundColor Colors.redLight
, textAlign center
]
2019-08-05 21:48:02 +03:00
2020-03-31 22:43:32 +03:00
else
[ textAlign center ]
}
, Table.custom
2022-05-25 02:44:20 +03:00
{ header = text "Actions"
2020-03-31 22:43:32 +03:00
, width = px 250
2022-05-25 02:34:40 +03:00
, view = \_ -> Button.button "Action" [ Button.small, Button.onClick (ConsoleLog "Clicked button!") ]
2020-03-31 22:43:32 +03:00
, cellStyles = always []
}
]
in
2022-05-25 02:34:40 +03:00
[ ControlView.view
{ ellieLinkConfig = ellieLinkConfig
, name = moduleName
, version = version
, update = UpdateControl
, settings = state
, mainType = "RootHtml.Html msg"
, extraImports = []
, toExampleCode = \settings -> [ { sectionName = moduleName ++ ".view", code = "TODO" } ]
}
2022-05-25 02:44:20 +03:00
, Heading.h2 [ Heading.style Heading.Subhead ] [ text "With header" ]
2020-03-31 22:43:32 +03:00
, Table.view columns data
2022-05-25 02:44:20 +03:00
, Heading.h2 [ Heading.style Heading.Subhead ] [ text "Without header" ]
2020-03-31 22:43:32 +03:00
, Table.viewWithoutHeader columns data
2022-05-25 02:44:20 +03:00
, Heading.h2 [ Heading.style Heading.Subhead ] [ text "Loading" ]
2020-03-31 22:43:32 +03:00
, Table.viewLoading columns
2022-05-25 02:44:20 +03:00
, Heading.h2 [ Heading.style Heading.Subhead ] [ text "Loading without header" ]
2020-03-31 22:43:32 +03:00
, Table.viewLoadingWithoutHeader columns
]
}
2022-05-25 02:34:40 +03:00
{-| -}
type Msg
= UpdateControl (Control Settings)
| ConsoleLog String
update : Msg -> State -> ( State, Cmd msg )
update msg state =
case msg of
UpdateControl control ->
( control, Cmd.none )
ConsoleLog message ->
( Debug.log "Menu Example" message |> always state, Cmd.none )
type alias Settings =
{}
controlSettings : Control Settings
controlSettings =
Control.record Settings
2022-05-25 02:44:20 +03:00
type alias Data =
{ firstName : String
, lastName : String
, submitted : Int
}
data : List Data
data =
[ { firstName = "First1", lastName = "Last1", submitted = 10 }
, { firstName = "First2", lastName = "Last2", submitted = 0 }
, { firstName = "First3", lastName = "Last3", submitted = 3 }
, { firstName = "First4", lastName = "Last4", submitted = 15 }
, { firstName = "First5", lastName = "Last5", submitted = 8 }
]