-
Notifications
You must be signed in to change notification settings - Fork 581
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(EMI-2095): Add Swipeable component (#10879)
* feat(EMI-2095): Add Swipeable component * add tests * style update --------- Co-authored-by: Ole <[email protected]>
- Loading branch information
1 parent
a4eee80
commit ac0522f
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
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" | ||
|
||
const FRICTION = 1.2 | ||
|
||
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 RightActions: ( | ||
progress: SharedValue<number>, | ||
dragX: SharedValue<number>, | ||
swipable: SwipeableMethods | ||
) => React.ReactNode = useCallback(() => { | ||
return ( | ||
<Touchable haptic onPress={actionOnPress}> | ||
<Flex | ||
flex={1} | ||
ml={1} | ||
p={2} | ||
bg={actionBackground} | ||
justifyContent="center" | ||
border="1px solid" | ||
borderColor={actionBackground} | ||
borderRadius={5} | ||
> | ||
{actionComponent} | ||
</Flex> | ||
</Touchable> | ||
) | ||
}, [actionBackground, actionComponent, actionOnPress]) | ||
|
||
return ( | ||
<ReanimatedSwipeable | ||
testID="swipeable-component" | ||
ref={swipeableRef} | ||
enabled={enabled} | ||
renderRightActions={RightActions} | ||
friction={FRICTION} | ||
{...swipeableProps} | ||
> | ||
{children} | ||
</ReanimatedSwipeable> | ||
) | ||
}) |