DEV Community

Tejendra Singh Rajawat
Tejendra Singh Rajawat

Posted on • Edited on

🎨 Mastering Drag-and-Drop in React-Select: A Visual Guide 🚀

Absolutely! Let's add some emojis and improve the visual appeal with a simple diagram.


🚀 Enhancing Drag-and-Drop in React-Select

Today, we'll embark on a journey to amplify the drag-and-drop experience in the react-select library. 🎉 To begin, let's gear up with the essential Higher Order Components (HOC) from both react-select and react-sortable-hoc.

import Select, { components } from "react-select";
import { SortableContainer, SortableElement } from "react-sortable-hoc";
Enter fullscreen mode Exit fullscreen mode

To facilitate the drag-and-drop events, we'll wield a moveable array and a customized MultiValue function. 🛠️

// Function for drag and drop
function arrayMove(array, from, to) {
  array = array.slice();
  array.splice(to < 0 ? array.length + to : to, 0, array.splice(from, 1)[0]);
  return array;
}

// Crafting a draggable MultiValue component
const SortableMultiValue = SortableElement((props) => {
  const onMouseDown = (e) => {
    e.preventDefault();
    e.stopPropagation();
  };
  const innerProps = { onMouseDown };
  return <components.MultiValue {...props} innerProps={innerProps} />;
});

// Assembling a sortable Select component
const SortableSelect = SortableContainer(Select);
Enter fullscreen mode Exit fullscreen mode

Now, let's infuse life into our drag-and-drop adventure by using the onChange and onSortEnd functions to dance with selection and reordering.

// Example with mandatory skills 🌟
const [selectedMand, setSelectedMand] = React.useState([...getMandatorySkillOption]);

const onChangeMand = (selectedOptions) => setSelectedMand(selectedOptions);
const onSortEndMan = ({ oldIndex, newIndex }) => {
  const newValue = arrayMove(selectedMand, oldIndex, newIndex);
  setSelectedMand(newValue);
  console.log("Values sorted:", newValue.map((i) => i.value));
};
Enter fullscreen mode Exit fullscreen mode

Lastly, let's bring all the props together in our stage, the SortableSelect component. It eagerly accepts all the props from react-select and adds a touch of magic with draggable MultiValues. ✨

<SortableSelect
  axis="xy"
  styles={customStyles}
  classNamePrefix={"create_position"}
  onSortEnd={onSortEndMan}
  distance={4}
  getHelperDimensions={({ node }) => node.getBoundingClientRect()}
  isMulti
  options={getMandatorySkillOption}
  value={selectedMand}
  onChange={onChangeMand}
  components={{
    MultiValue: SortableMultiValue,
  }}
  defaultValue={formData?.mandatorySkill?.map((i) => ({
    value: i.id,
    label: i.mandatorySkillName,
  }))}
  closeMenuOnSelect={false}
/>
Enter fullscreen mode Exit fullscreen mode

With this, we aim to not only enhance functionality but also add a dash of fun and excitement to the reading experience! 🚀📚

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay