marekrozmus / react-swipeable-list

Swipeable list component for React supporting several behaviours (e.g. iOS)
https://marekrozmus.github.io/react-swipeable-list/
MIT License
114 stars 20 forks source link

[Feature Request] allow to pass react fragments or insert react nodes between list items #49

Open VityaSchel opened 1 year ago

VityaSchel commented 1 year ago

Currently it's impossible to use react-swipeable-list with fragments because it breaks and actions does not work.

marekrozmus commented 1 year ago

@VityaSchel Please provide any code snippet you would like to use.

it-nalon commented 1 year ago

I had the same issue, I have an UI where every swipable item has a title.

Something like that:

Screenshot 2023-11-06 alle 18 04 51

I tried wrapping the item in a <div /> but it broke the swipe functionality, after looking at the source code I created the <SwipableListItemProxy /> component:

import { Children, ReactNode, cloneElement } from 'react'

export type SwipableListItemProxyProps = {
  header?: ReactNode
  footer?: ReactNode
  children: ReactNode
} & Record<string, any>

export function SwipableListItemProxy({
  children,
  header,
  footer,
  ...props
}: SwipableListItemProxyProps) {
  return (
    <>
      {header}
      {Children.map(children, (child, index) =>
        cloneElement(child as any, {
          ...props,
        })
      )}
      {footer}
    </>
  )
}

Now I can use it like that:

<SwipeableList type={ListType.IOS}>
  <SwipableListItemProxy header={<div>My Header</div>}>
    <SwipeableListItem>...</SwipeableListItem>
  </SwipableListItemProxy>
</SwipeableList>

It's far from perfect, I don't like the component name nor the props and there's no error management, but it's easily customizable to suit other needs.

gmfacundo commented 5 months ago

Hey, I was wondering if is there any workaround for this issue?

I'm struggling to implement this library along with dnd-kit/sortable. When I tried to wrap the list items between fragments I encountered this error.

it-nalon commented 5 months ago

Hey, I was wondering if is there any workaround for this issue?

I'm struggling to implement this library along with dnd-kit/sortable. When I tried to wrap the list items between fragments I encountered this error.

Did you try with my solution?

gmfacundo commented 5 months ago

Hey @it-nalon. Yes I tried to use your code as a template but I couldn't make it work for my case.

I have to mention that I'm still learning and it was the first time that I tried to extend a library by my own.