2020-03-31 22:43:32 +03:00
|
|
|
module Examples.SortableTable exposing (Msg, State, example)
|
2019-07-11 22:46:22 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
|
2020-03-31 22:43:32 +03:00
|
|
|
@docs Msg, State, example
|
2019-07-11 22:46:22 +03:00
|
|
|
|
|
|
|
-}
|
|
|
|
|
2020-03-24 03:33:42 +03:00
|
|
|
import Category exposing (Category(..))
|
2022-03-30 21:49:11 +03:00
|
|
|
import Css exposing (..)
|
2020-03-31 23:20:03 +03:00
|
|
|
import Example exposing (Example)
|
2022-03-30 21:49:11 +03:00
|
|
|
import Html.Styled as Html exposing (..)
|
|
|
|
import Html.Styled.Attributes exposing (css)
|
2022-03-31 00:21:48 +03:00
|
|
|
import Nri.Ui.Colors.V1 as Colors
|
2019-11-15 20:12:06 +03:00
|
|
|
import Nri.Ui.Heading.V2 as Heading
|
2020-07-31 23:18:48 +03:00
|
|
|
import Nri.Ui.SortableTable.V2 as SortableTable
|
2022-03-31 00:21:48 +03:00
|
|
|
import Nri.Ui.Svg.V1 as Svg
|
2022-03-30 21:49:11 +03:00
|
|
|
import Nri.Ui.Table.V5 as Table
|
2022-03-31 00:21:48 +03:00
|
|
|
import Nri.Ui.UiIcon.V1 as UiIcon
|
2019-07-11 22:46:22 +03:00
|
|
|
|
|
|
|
|
|
|
|
type Column
|
|
|
|
= FirstName
|
|
|
|
| LastName
|
|
|
|
| Coins
|
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
type Msg
|
2022-03-15 21:06:13 +03:00
|
|
|
= SetSortState (SortableTable.State Column)
|
2019-07-11 22:46:22 +03:00
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
type alias State =
|
|
|
|
{ sortState : SortableTable.State Column }
|
|
|
|
|
|
|
|
|
2022-03-30 21:49:11 +03:00
|
|
|
type Direction
|
|
|
|
= Up
|
|
|
|
| Down
|
|
|
|
|
|
|
|
|
|
|
|
sortArrow : Direction -> Html msg
|
|
|
|
sortArrow direction =
|
2022-03-31 00:21:48 +03:00
|
|
|
(case direction of
|
|
|
|
Up ->
|
|
|
|
UiIcon.sortArrow
|
|
|
|
|
|
|
|
Down ->
|
|
|
|
UiIcon.sortArrowDown
|
|
|
|
)
|
|
|
|
|> Svg.withColor Colors.gray75
|
|
|
|
|> Svg.withWidth (Css.px 12)
|
|
|
|
|> Svg.withHeight (Css.px 12)
|
|
|
|
|> Svg.toHtml
|
2022-03-30 21:49:11 +03:00
|
|
|
|
|
|
|
|
2019-07-11 22:46:22 +03:00
|
|
|
{-| -}
|
2020-03-31 23:20:03 +03:00
|
|
|
example : Example State Msg
|
2020-03-31 22:43:32 +03:00
|
|
|
example =
|
2020-09-09 21:43:10 +03:00
|
|
|
{ name = "SortableTable"
|
|
|
|
, version = 2
|
2021-11-12 01:56:04 +03:00
|
|
|
, categories = [ Layout ]
|
2020-06-20 00:45:32 +03:00
|
|
|
, keyboardSupport = []
|
2020-03-31 22:43:32 +03:00
|
|
|
, state = init
|
|
|
|
, update = update
|
2020-03-31 22:48:26 +03:00
|
|
|
, subscriptions = \_ -> Sub.none
|
2022-03-30 21:49:11 +03:00
|
|
|
, preview =
|
|
|
|
let
|
2022-03-31 00:21:48 +03:00
|
|
|
header name =
|
|
|
|
div
|
2022-03-30 21:49:11 +03:00
|
|
|
[ css
|
|
|
|
[ Css.displayFlex
|
2022-03-31 00:21:48 +03:00
|
|
|
, Css.justifyContent Css.spaceBetween
|
2022-03-30 21:49:11 +03:00
|
|
|
, Css.alignItems Css.center
|
|
|
|
]
|
|
|
|
]
|
2022-03-31 00:21:48 +03:00
|
|
|
[ text name
|
|
|
|
, div
|
|
|
|
[ css
|
|
|
|
[ Css.displayFlex
|
|
|
|
, Css.flexDirection Css.column
|
|
|
|
, Css.marginTop (Css.px -4)
|
|
|
|
]
|
|
|
|
]
|
|
|
|
[ sortArrow Up
|
|
|
|
, sortArrow Down
|
|
|
|
]
|
2022-03-30 21:49:11 +03:00
|
|
|
]
|
|
|
|
in
|
|
|
|
[ Table.view
|
|
|
|
[ Table.custom
|
2022-03-31 00:21:48 +03:00
|
|
|
{ header = header "X"
|
2022-03-30 21:49:11 +03:00
|
|
|
, view = .a >> Html.text
|
|
|
|
, width = px 50
|
|
|
|
, cellStyles = always []
|
|
|
|
}
|
|
|
|
, Table.custom
|
2022-03-31 00:21:48 +03:00
|
|
|
{ header = header "Y"
|
2022-03-30 21:49:11 +03:00
|
|
|
, view = .b >> Html.text
|
|
|
|
, width = px 50
|
|
|
|
, cellStyles = always []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
[ { a = "Row 1 X"
|
|
|
|
, b = "Row 1 Y"
|
|
|
|
}
|
|
|
|
, { a = "Row 2 X"
|
|
|
|
, b = "Row 2 Y"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
]
|
2020-03-31 22:43:32 +03:00
|
|
|
, view =
|
2022-03-29 20:19:32 +03:00
|
|
|
\ellieLinkConfig { sortState } ->
|
2020-03-31 22:43:32 +03:00
|
|
|
let
|
|
|
|
config =
|
|
|
|
{ updateMsg = SetSortState
|
|
|
|
, columns =
|
|
|
|
[ SortableTable.string
|
|
|
|
{ id = FirstName
|
|
|
|
, header = "First name"
|
|
|
|
, value = .firstName
|
|
|
|
, width = 125
|
2020-07-31 23:18:48 +03:00
|
|
|
, cellStyles = \_ -> []
|
2020-03-31 22:43:32 +03:00
|
|
|
}
|
|
|
|
, SortableTable.string
|
|
|
|
{ id = LastName
|
|
|
|
, header = "Last name"
|
|
|
|
, value = .lastName
|
|
|
|
, width = 125
|
2020-07-31 23:18:48 +03:00
|
|
|
, cellStyles = \_ -> []
|
2020-03-31 22:43:32 +03:00
|
|
|
}
|
|
|
|
, SortableTable.custom
|
|
|
|
{ id = Coins
|
|
|
|
, header = Html.text "Coins"
|
|
|
|
, view = .coins >> String.fromInt >> Html.text
|
|
|
|
, sorter = SortableTable.simpleSort .coins
|
|
|
|
, width = 125
|
2020-07-31 23:18:48 +03:00
|
|
|
, cellStyles = \_ -> []
|
2020-03-31 22:43:32 +03:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
data =
|
|
|
|
[ { firstName = "First1", lastName = "Last1", coins = 1 }
|
|
|
|
, { firstName = "First2", lastName = "Last2", coins = 2 }
|
|
|
|
, { firstName = "First3", lastName = "Last3", coins = 3 }
|
|
|
|
, { firstName = "First4", lastName = "Last4", coins = 4 }
|
|
|
|
, { firstName = "First5", lastName = "Last5", coins = 5 }
|
2019-07-11 22:46:22 +03:00
|
|
|
]
|
2020-03-31 22:43:32 +03:00
|
|
|
in
|
|
|
|
[ Heading.h3 [] [ Html.text "With sortable headers" ]
|
|
|
|
, SortableTable.view config sortState data
|
|
|
|
, Heading.h3 [] [ Html.text "Loading" ]
|
|
|
|
, SortableTable.viewLoading config sortState
|
|
|
|
]
|
2019-07-11 22:46:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
init : State
|
|
|
|
init =
|
|
|
|
{ sortState = SortableTable.init FirstName }
|
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
update : Msg -> State -> ( State, Cmd Msg )
|
|
|
|
update msg state =
|
|
|
|
case msg of
|
|
|
|
SetSortState sortState ->
|
|
|
|
( { state | sortState = sortState }, Cmd.none )
|