Skip to content

allow passing props to SwipeableList div #145

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/SwipeableList.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ const SwipeableList = ({
children,
scrollStartThreshold,
swipeStartThreshold,
threshold
threshold,
className,
...rest
}) => (
<div className={styles.swipeableList} data-testid="list-wrapper">
<div
className={`${styles.swipeableList} ${className}`}
Copy link
Member

@przemyslawzalewski przemyslawzalewski Apr 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For missing className prop (allowed by prop types) this would evaluate to className="someGeneratedClassName undefined". To avoid this kind of issue, a class names concatenation helper is needed - for example, https://sandstreamdev.github.io/std/web/classNames/ or https://www.npmjs.com/package/classnames.

In my opinion, in the future, more feature requests would require rendering to be customized, so I propose the component to allow headless rendering and just use user-provided render functions. This way the caller is responsible for the final rendering and the SwipeableList just provides its props there.

Consider API like this:

<SwipeableList>
  {({ className, ...rest }) => (
    <div className={classNames(className, 'scrollable')} {...rest}>
      <SwipeableListItem>...<SwipeableListItem />
     </div>
  )}
</SwipeableList>

By default, it can provide the default renderer as it is, to keep compatibility. This is a very similar API to recently proposed one with #144 .

With the headless rendering, a hook based solution is also viable:

const { className, ...rest } = useSwipeableList();

return <div className={classNames(className, 'scrollable')} {...rest}>
  <SwipeableListItem>...<SwipeableListItem />
</div>

data-testid="list-wrapper"
{...rest}
>
{React.Children.map(children, child =>
React.cloneElement(child, {
scrollStartThreshold,
Expand All @@ -24,7 +30,8 @@ SwipeableList.propTypes = {
children: PropTypes.node,
scrollStartThreshold: PropTypes.number,
swipeStartThreshold: PropTypes.number,
threshold: PropTypes.number
threshold: PropTypes.number,
className: PropTypes.string
};

export default SwipeableList;
2 changes: 1 addition & 1 deletion src/module.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';

interface ISwipeableListProps {
interface ISwipeableListProps extends React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement> {
scrollStartThreshold?: number;
swipeStartThreshold?: number;
threshold?: number;
Expand Down