Socket
Socket
Sign inDemoInstall

react-virtual-drag-list

Package Overview
Dependencies
1
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-virtual-drag-list

A virtual scrolling list component that can be sorted by dragging


Version published
Maintainers
1
Weekly downloads
2,871
increased by6.33%

Weekly downloads

Readme

Source

react-virtual-drag-list

npm npm Software License

A virtual scrolling list component that can be sorted by dragging

Live Demo

Simple Usage

npm i react-virtual-drag-list

Root component:

import VirtualList from 'react-virtual-drag-list';

function Virtual() {

  const [list, setList] = useState([{id: '1', text: 'a'}, {id: '2', text: 'b'}, ...]);

  const onTop = () => {
    // scrolled to the top of list
  }
  const onBottom = () => {
    // scrolled to the bottom of list
  }
  const onDrop = (params) => {
    // dnd complete
    setList(() => params.list);
  }

  // use style and className as jsx used
  return (
    <VirtualList
      className="virtual-list"
      style={{ height: '500px' }}
      dataKey="id"
      dataSource={ list }
      handle=".handle"
      header={ <div className="loading">top loading...</div> }
      footer={ <div className="loading">bottom loading...</div> }
      v-top={ onTop }
      v-bottom={ onBottom }
      v-drop={ onDrop }
    >
      {
        (record, index, dataKey) => {
          return (
            <div>
              <span class=".handle">{ index }</span>
              { record.text }
            </div>
          )
        }
      }
    </VirtualList>
  )
}

Props

Callback

EmitTypeRequired?DefaultDescription
v-topFunction-Scrolling to the top of the scroller
v-bottomFunction-Scrolling to the bottom of the scroller
v-dragFunction-Drag is started
v-dropFunction-Drag is complete
v-addFunction-Element is dropped into the list from another
v-removeFunction-Element is removed from the list into another

Common used

PropTypeRequired?DefaultDescription
dataKeyString-The unique identifier of each piece of data, in the form of 'a.b.c'
dataSourceArray[]The data that needs to be rendered
sizeNumber-Estimated height of each row. You can choose to pass it or not, it will be automatically calculated
keepsNumber30The number of lines rendered by the virtual scroll
handleFunction/String-Drag handle selector within list items
groupObject/String-string: 'name' or object: { name: 'group', put: true/false, pull: true/false/'clone', revertDrag: true/false }
scrollerDocument | HTMLElement-Virtual list scrolling element
directionvertical | horizontalverticalScroll direction
lockAxisx | y-Axis on which dragging will be locked
keepOffsetBooleanfalseWhen scrolling up to load data, keep the same offset as the previous scroll
debounceTimeNumber0debounce time on scroll
throttleTimeNumber0debounce time on scroll
headerJSX.Element-Top of list
footerJSX.Element-Bottom of list

Uncommonly used

PropTypeRequired?DefaultDescription
draggableString-Specifies which items inside the element should be draggable. If does not set a value, the default list element can be dragged
disabledBooleanfalseDisables the sortable if set to true
animationNumber150Drag-and-drop's animation delay
autoScrollBooleantrueAutomatic scrolling when moving to the edge of the container
scrollThresholdNumber55Threshold to trigger autoscroll
delayNumber0Time in milliseconds to define when the sorting should start
delayOnTouchOnlyBooleanfalseOnly delay on press if user is using touch
fallbackOnBodyBooleanfalseAppends the ghost element into the document's body
rootTagStringdivLabel type for root element
wrapTagStringdivLabel type for list wrap element
wrapStyleObject{}List wrapper element style
wrapClassString''List wrapper element class
itemTagStringdivLabel type for list item element
itemStyleObject{}List item element style
itemClassString''List item element class
ghostStyleObject{}The style of the mask element when dragging
ghostClassString''The class of the mask element when dragging
chosenClassString''The class of the selected element when dragging

Methods

Use the methods exposed in the component by setting ref

...

const virtualRef = useRef();

const scrollToBottom = () => {
  virtualRef.current.scrollToBottom();
}

return (
  <button onClick={ scrollToBottom }></button>
  <virtualList
    ref={ virtualRef }
    ...
  >
    {
      (record) => <div>{ record.text }</div>
    }
  </virtualList>
)
PropDescription
getSize(key)get the height of the specified item by key value
getOffset()get the current scroll top/left
getClientSize()Get wrapper element client viewport size (width or height)
getScrollSize()Get all scroll size (scrollHeight or scrollWidth)
scrollToTop()scroll to the top of the list
scrollToBottom()scroll to the bottom of the list
scrollToKey(key)scroll to the specified data-key
scrollToIndex(index)scroll to the specified index value
scrollToOffset(offset)scroll to the specified height

Keywords

FAQs

Last updated on 16 Mar 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc