Coder Social home page Coder Social logo

jeonbyeongmin / use-table-drag-select Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 1.0 49.16 MB

A React hook for selecting table cells by dragging

Home Page: https://jeonbyeongmin.github.io/use-table-drag-select/

License: MIT License

JavaScript 12.10% TypeScript 87.90%
react hooks table drag-selection

use-table-drag-select's Introduction

use-table-drag-select

2

React hook to select table by drag

  • Super simple
  • useful in implementing a timetable

Installation

npm i use-table-drag-select
yarn add use-table-drag-select

Usage

All you have to do is add a ref


  1. With initial table values
export function Table() {
  const [ref, value] = useTableDragSelect([
    [false, false, false, false, false, false, false],
    [false, false, false, false, false, false, false],
    [false, false, false, false, false, false, false],
    [false, false, false, false, false, false, false],
    [false, false, false, false, false, false, false],
    [false, false, false, false, false, false, false],
  ]);

  return (
    <table ref={ref} className="timetable">
      <thead>
        <tr>
          <th></th>
          <th>Mon</th>
          <th>Tue</th>
          <th>Wed</th>
          <th>Thu</th>
          <th>Fri</th>
          <th>Sat</th>
          <th>Sun</th>
        </tr>
      </thead>
      <tbody>
        {value.map((row, rowIndex) => (
          <tr key={rowIndex}>
            <th>{rowIndex + 1}</th>
            {row.map((_, columnIndex) => (
              <td key={columnIndex} />
            ))}
          </tr>
        ))}
      </tbody>
    </table>
  );
});

You can use a iterable value on render()


  1. With complete table DOM
export function Table() {
  const [ref, value] = useTableDragSelect();

  return (
    <table ref={ref} className="timetable">
      <thead>
        <tr>
          <th></th>
          <th>Mon</th>
          <th>Tue</th>
          <th>Wed</th>
          <th>Thu</th>
          <th>Fri</th>
          <th>Sat</th>
          <th>Sun</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th>1</th>
          <td />
          <td />
          <td />
          <td />
          <td />
          <td />
          <td />
        </tr>
        <tr>
          <th>2</th>
          <td />
          <td />
          <td />
          <td />
          <td />
          <td />
          <td />
        </tr>
        ...
      </tbody>
    </table>
  );
});

You should not put anything in useTableDragSelect


Demo

A minimal demo page can be found in demo directory.

Online demo is also available.

use-table-drag-select's People

Contributors

jeonbyeongmin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

use-table-drag-select's Issues

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.