0.1.3 • Published 9 months ago

react-draggable-select v0.1.3

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

React-draggable-select

2024-10-089 28 52-ezgif com-video-to-gif-converter

Features

  • Select items by dragging or clicking
  • Supports mobile
  • Supports SSR
  • Customizable styling

Installation

npm install react-draggable-select

Usage

import { DragSelect, useSelection } from "react-draggable-select";

function App() {
  const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // Ensure to use an array with unique values.

  const { selectionContext, isSelected, setRef } = useSelection();

  return (
    <div>
      <DragSelect selectionContext={selectionContext} // Required: This prop is necessary to function correctly.
      />
      <div>
        {items.map((v, i) => (
          <div
            key={i}
            data-id={v} // Required: Must be a number; it will be included in the selection result array.
            ref={setRef} // Required: Pass this ref to all selectable elements to enable proper selection tracking.
            className={`${isSelected(v) ? "selected" : ""}`}
          >
            {v}
          </div>
        ))}
      </div>
    </div>
  );
}

Note: No default styles are provided for selected items. Please specify custom styles to enhance visibility.

API Reference

DragSelect Components Props

Prop NameTypeRequiredDescription
selectionContextobjectONecessary for the DragSelect component to function correctly.
draggedSectionClassNamestringxClass name for the dragged section.
onSelectionComplete(selected: number) => voidxCallback fired when selection is complete.

useSelection custom hook

return ValueTypeDescription
selectionContextobjectNecessary for the DragSelect component to function correctly.
isSelected(value: number) => booleanFunction to check if an item is selected.
selectedItemsnumber[]Array of currently selected items (from data-id values).
setRef(el: HTMLElement \| null) => voidFunction to set a ref for selectable item.

Styling

You can customize the styling of the dragged section using either plain CSS or a custom class name.

Example

You can override the default style using CSS.

.ds-dragged-section {
  background-color: #8989f180;
}

or pass a custom class name to draggedSectionClassName:

 <DragSelect draggedSectionClassName={customClassName} />
0.1.3

9 months ago

0.1.2

9 months ago

0.1.1

9 months ago

0.1.0

10 months ago