ValentinH / react-easy-sort

A React component to sort items in lists or grids
https://ValentinH.github.io/react-easy-sort
MIT License
160 stars 25 forks source link

Example of forwardRef usage #17

Closed mahdiyazdani closed 2 years ago

mahdiyazdani commented 3 years ago

Thanks for the great tool. Would you be able to share a brief example of how to pass a component as a child to the SortableKnob using React.forwardRef()?

ValentinH commented 3 years ago

Sure, here you go: https://codesandbox.io/s/react-easy-sort-custom-knob-demo-ij37h?file=/src/App.tsx

I'm also adding this to the Readme.

mahdiyazdani commented 3 years ago

Thank you for your reply. I was thinking that perhaps it would be possible not to have a wrapper div within the CustomKnob component, and pass on the ref directory to the DragIndicatorIcon instead.

const CustomKnob = React.forwardRef<HTMLDivElement, {}>((props, ref) => {
  const classes = useKnobStyles();
  return (
    <div ref={ref} className={classes.root}> { /* Referring to this line */ }
      <DragIndicatorIcon />
      <Typography variant="caption">drag me</Typography>
    </div>
  );
});
ValentinH commented 3 years ago

Yes this should be possible if the icon can hold a ref.