GithubHelp home page GithubHelp logo

serhat-m / movenext Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 260 KB

Simple and straightforward solution to get the next logical record

License: MIT License

Shell 1.26% TypeScript 64.33% HTML 0.76% CSS 20.13% JavaScript 13.52%

movenext's Introduction

Simple and straightforward solution to get the next logical record.

Live Demo

https://serhat-m.github.io/moveNext

Install

npm i movenext

Usage

Function moveNext(opts)

import moveNext from "movenext"

opts Object

  • data { [key: string]: any }[] array of records
  • direction 'prev' | 'next' determines the direction
  • endBehaviour 'default' | 'jump' behaviour after the last logical entry
  • selector(entry) => any callback for selecting the id reference
    • entry { [key: string]: any } entry of data
  • selectedId undefined | ... current selected id

@returns new selected id

Example

This example navigates through the data Array, if the keyboard keys ArrowDown or ArrowUp are pressed. The selectedId variable saves the current state.

import moveNext from "movenext"

const data = [
  { id: 1, title: "Dataset 1" },
  { id: 2, title: "Dataset 2" },
  { id: 3, title: "Dataset 3" },
]

let selectedId = undefined

document.addEventListener("keydown", (event) => {
  const direction = event.key === "ArrowDown" ? "next" : event.key === "ArrowUp" ? "prev" : false

  if (direction) {
    selectedId = moveNext({
      data,
      direction,
      endBehaviour: "default",
      selector: (entry) => entry.id,
      selectedId,
    })
  }
})

Example React

import moveNext from "movenext"

const [data, setData] = useState([
  { id: 1, title: "Dataset 1" },
  { id: 2, title: "Dataset 2" },
  { id: 3, title: "Dataset 3" },
])

const [selectedId, setSelectedId] = useState(undefined)

const keyDownHandler = useCallback(
  (event) => {
    const direction = event.key === "ArrowDown" ? "next" : event.key === "ArrowUp" ? "prev" : false

    if (direction) {
      setSelectedId((prevSelectedId) => {
        return moveNext({
          data,
          direction,
          endBehaviour: "default",
          selector: (entry) => entry.id,
          selectedId: prevSelectedId,
        })
      })
    }
  },
  [data],
)

useEffect(() => {
  document.addEventListener("keydown", keyDownHandler)

  return () => {
    document.removeEventListener("keydown", keyDownHandler)
  }
}, [keyDownHandler])

TypeScript

The following types are available and can be used to define e. g. typed helper functions:

  • Direction = "prev" | "next"
  • EndBehaviour = "default" | "jump”
import type { Direction, EndBehaviour } from "movenext"

// Example 1
function updateData(direction: Direction, behaviour: EndBehaviour) {
  ...
}

// Example 2
const direction: Direction | false = event.key === "ArrowDown" ? "next" : event.key === "ArrowUp" ? "prev" : false

movenext's People

Contributors

serhat-m 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.