Giter Club home page Giter Club logo

elm-reorderable's Introduction

elm-reorderable Build Status

Reorder entries while maintaining a key/value correspondence.

Having a list of entries that need to accept input, need to render efficiently, and can be arbitrarily reordered is fairly common. Keyed nodes are a great tool for implementing that, but there's a catch: every item needs to have some sort of unique identifier.

In reality, you don't always want every single item to have an identifier. You could of course add that to your model, just for the sake of making it possible to use Keyed nodes, but that's not always practical, and arguably it's simply not the best solution.

Reorderable attempts to take some of that pain away by keeping track of a sequential identifier for everything you add to it; while also making sure that the identifier survives things like dropping items, swapping items at places or arbitrarily moving an item through the structure.

module Main exposing (main)

import Browser
import Html exposing (Html, text)
import Html.Attributes as Attr
import Html.Events as Events
import Html.Keyed as Keyed
import Reorderable exposing (Reorderable)
import Tuple

--- updated to Elm 0.19

type alias Model =
    Reorderable String


type Msg
    = Add
    | Remove Int
    | MoveUp Int
    | MoveDown Int
    | Input Int String


initialModel : Model
initialModel =
    Reorderable.singleton ""


update : Msg -> Model -> Model
update msg model =
    case msg of
        Add ->
            Reorderable.push "" model

        Remove idx ->
            Reorderable.drop idx model

        MoveUp idx ->
            Reorderable.moveUp idx model

        MoveDown idx ->
            Reorderable.moveDown idx model

        Input idx input ->
            Reorderable.set idx input model


view : Model -> Html Msg
view items =
    Html.div []
        [ Keyed.node "div" [] <|
            List.indexedMap viewItem (Reorderable.toKeyedList items)
        , Html.button
            [ Events.onClick Add ]
            [ text "Add another entry" ]
        ]


viewItem : Int -> ( String, String ) -> ( String, Html Msg )
viewItem idx ( key, value ) =
    Html.div [ Attr.class "side-by-side" ]
        [ Html.textarea [ Events.onInput <| Input idx ] []
        , Html.ul [ Attr.class "actions" ]
            [ Html.li [ Events.onClick <| Remove idx ] [ text "remove" ]
            , Html.li [ Events.onClick <| MoveUp idx ] [ text "move up" ]
            , Html.li [ Events.onClick <| MoveDown idx ] [ text "move down" ]
            ]
        ]
        |> Tuple.pair key


main =
    Browser.sandbox
        { init = initialModel
        , update = update
        , view = view
        }

Made with ❤️

elm-reorderable's People

Contributors

twistingtwists avatar zwilias avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.