Code Monkey home page Code Monkey logo

drag-selection's Introduction

基于react的框选组件。

Usage

npm install drag-selection
  .container {
    height: 430px;
    overflow-y: auto;
    background: #f5f5f5;
    display: grid;
    gap: 10px;
    justify-content: center;
    grid-template-columns: repeat(auto-fill, 100px);
    grid-template-rows: repeat(auto-fill, 100px);
    .node {
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 16px;
      font-weight: bold;
      background: #e5e5e5;
    }
    .highlight {
      background: #a5a5a5;
      color: #fff;
    }
  }
        const nodeIds = range(1,31);
        return (<DragSelection 
                 onSelectionMove={({ left, top, width, height }) => {
                    if (width > 10 && height > 10) {
                    // 要有一定的移动量才计算框选,不然很容易误点
                    nodeIds.forEach(nodeId => {
                        const element = document.getElementById(nodeId);
                        if (element) {
                        const rect = element.getBoundingClientRect();
                        if (
                            left <= rect.left + rect.width &&
                            left + width >= rect.left &&
                            top <= rect.top + rect.height &&
                            top + height >= rect.top
                        ) {
                            element.classList.add(styles.highlight)
                        }else{
                            element.classList.remove(styles.highlight)
                        }
                        }
                    });
                    }
              }}>
               <div className={styles.container}>
                  {nodeIds.map(nodeId=><div id={`${nodeId}`} className={styles.node}>{nodeId+1}</div>)}
               </div>
              </DragSelection>)

RUN EFFECT

image

LICENSE

MIT

drag-selection's People

Contributors

lth707 avatar

Watchers

James Cloos avatar  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.