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

feat(EMI-2095): Add Swipeable component #10879

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 27 additions & 0 deletions src/app/Components/Swipeable/Swipeable.tests.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Text } from "@artsy/palette-mobile"
import { screen, fireEvent } from "@testing-library/react-native"
import { Swipeable } from "app/Components/Swipeable/Swipeable"
import { renderWithWrappers } from "app/utils/tests/renderWithWrappers"

describe("Swipeable Component", () => {
const mockAction = jest.fn()

it("should render without crashing", () => {
renderWithWrappers(<Swipeable {...mockProps} actionOnPress={mockAction} />)

expect(screen.getByTestId("swipeable-component")).toBeOnTheScreen()
})

it("should call the action when the action component is pressed", () => {
renderWithWrappers(<Swipeable {...mockProps} actionOnPress={mockAction} />)

fireEvent.press(screen.getByText("Swipe Me"))

expect(mockAction).toHaveBeenCalled()
})
})

const mockProps = {
actionComponent: <Text>Swipe Me</Text>,
children: <Text>Swipeable Component</Text>,
}
62 changes: 62 additions & 0 deletions src/app/Components/Swipeable/Swipeable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Color, Flex, Touchable } from "@artsy/palette-mobile"
import { forwardRef, useCallback } from "react"
import ReanimatedSwipeable, {
SwipeableMethods,
SwipeableRef,
} from "react-native-gesture-handler/ReanimatedSwipeable"
import { SharedValue } from "react-native-reanimated"

export interface SwipeableProps {
children: React.ReactNode
actionOnPress: () => void
actionComponent: React.ReactNode
actionBackground?: Color
swipeableProps?: SwipeableProps
enabled?: boolean
}

export const Swipeable = forwardRef((props: SwipeableProps, swipeableRef: SwipeableRef) => {
const {
children,
actionOnPress,
actionComponent,
actionBackground = "red100",
enabled = true,
swipeableProps,
} = props

const renderRightActions: (
progress: SharedValue<number>,
dragX: SharedValue<number>,
swipable: SwipeableMethods
) => React.ReactNode = useCallback(() => {
return (
<Touchable haptic onPress={actionOnPress}>
<Flex
ml={1}
p={1}
flex={1}
minWidth={71}
bg={actionBackground}
alignItems="center"
justifyContent="center"
>
{actionComponent}
</Flex>
</Touchable>
)
}, [actionBackground, actionComponent, actionOnPress])

return (
<ReanimatedSwipeable
testID="swipeable-component"
ref={swipeableRef}
enabled={enabled}
renderRightActions={renderRightActions}
friction={1.5}
{...swipeableProps}
>
{children}
</ReanimatedSwipeable>
)
})