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

102 lines
2.8 KiB
Elm
Raw Normal View History

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-06-19 23:41:28 +03:00
import AtomicDesignType exposing (AtomicDesignType(..))
import Category exposing (Category(..))
2020-03-31 23:20:03 +03:00
import Example exposing (Example)
2019-07-11 23:18:22 +03:00
import Html.Styled as Html
import Nri.Ui.Heading.V2 as Heading
2019-07-11 22:46:22 +03:00
import Nri.Ui.SortableTable.V1 as SortableTable
type Column
= FirstName
| LastName
| Coins
{-| -}
type Msg
= NoOp
| SetSortState (SortableTable.State Column)
{-| -}
type alias State =
{ sortState : SortableTable.State Column }
{-| -}
2020-03-31 23:20:03 +03:00
example : Example State Msg
2020-03-31 22:43:32 +03:00
example =
2019-07-11 23:18:22 +03:00
{ name = "Nri.Ui.SortableTable.V1"
2020-06-19 23:41:28 +03:00
, categories = [ Tables, Layout ]
, atomicDesignType = AtomicDesignType.Molecule
2020-03-31 22:43:32 +03:00
, state = init
, update = update
2020-03-31 22:48:26 +03:00
, subscriptions = \_ -> Sub.none
2020-03-31 22:43:32 +03:00
, view =
\{ sortState } ->
let
config =
{ updateMsg = SetSortState
, columns =
[ SortableTable.string
{ id = FirstName
, header = "First name"
, value = .firstName
, width = 125
}
, SortableTable.string
{ id = LastName
, header = "Last name"
, value = .lastName
, width = 125
}
, SortableTable.custom
{ id = Coins
, header = Html.text "Coins"
, view = .coins >> String.fromInt >> Html.text
, sorter = SortableTable.simpleSort .coins
, width = 125
}
]
}
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
NoOp ->
( state, Cmd.none )
SetSortState sortState ->
( { state | sortState = sortState }, Cmd.none )