2.7.4 • Published 6 months ago

react-virtual-drag-list v2.7.4

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

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 onDrop = (event) => {
    // dnd complete
    setList(() => event.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> }
      onDrop={ onDrop }
    >
      {
        (record, index, dataKey) => {
          return (
            <div>
              <span className=".handle">{ index }</span>
              { record.text }
            </div>
          )
        }
      }
    </VirtualList>
  )
}

Props

Callback

EmitTypeDefaultDescription
onTopFunction-Scrolling to the top of the scroller
onBottomFunction-Scrolling to the bottom of the scroller
onDragFunction-Drag is started
onDropFunction-Drag is complete

Required props | Prop | Type | Default | Description | | ------------ | -------- | ----------- | --------------------------------------------------------------------- | | dataKey | String | - | The unique identifier of each piece of data, in the form of 'a.b.c' | | dataSource | Array | [] | The data that needs to be rendered |

Common used

PropTypeDefaultDescription
headerJSX.Element-Top of list
footerJSX.Element-Bottom of list
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
handleString-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
debounceTimeNumber0debounce time on scroll
throttleTimeNumber0debounce time on scroll
tableModeBooleanfalsedisplay with table and tbody

Uncommonly used

PropTypeDefaultDescription
draggableString.virutal-dnd-list-itemSpecifies which items inside the element should be draggable
sortableBooleantrueWhether the current list can be sorted by dragging
lockAxisx \| y-Axis on which dragging will be locked
keepOffsetBooleanfalseWhen scrolling up to load data, keep the same offset as the previous scroll
disabledBooleanfalseDisables the sortable if set to true
animationNumber150Drag-and-drop's animation delay
autoScrollBooleantrueAutomatic scrolling when moving to the edge of the container
scrollSpeedObject{ x: 10, y: 10 }Vertical&Horizontal scrolling speed (px)
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
itemClassStringvirutal-dnd-list-itemList item element class
ghostStyleObject{}The style of the mask element when dragging
ghostClassString''The class of the mask element when dragging
chosenClassString''Class name for the chosen item
placeholderClassString''Class name for the drop placeholder

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
2.7.4

6 months ago

2.7.3

7 months ago

2.7.0

1 year ago

2.7.1

1 year ago

2.6.2

1 year ago

2.6.1

1 year ago

2.6.0

1 year ago

2.5.0

2 years ago

2.5.2

2 years ago

2.5.1

2 years ago

2.4.6

3 years ago

2.4.1

3 years ago

2.4.0

3 years ago

2.4.3

3 years ago

2.4.2

3 years ago

2.4.5

3 years ago

2.4.4

3 years ago

2.3.0

3 years ago

2.2.0

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago