Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
VityaSchel opened this issue Aug 17, 2023 · 5 comments

Comments

@VityaSchel
Copy link

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

@marekrozmus
Copy link
Owner

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

@it-nalon
Copy link

it-nalon commented Nov 6, 2023

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
Copy link

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
Copy link

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
Copy link

gmfacundo commented May 31, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants