Skip to content

Commit 96020b9

Browse files
authored
Added 'closeOnPress' to SwipeableItemButton (#694)
* Added 'closeOnPress' to SwipeableItemButton * Update how 'closeOnPress' is checked * Removed async from debouncing tests
1 parent cae81d0 commit 96020b9

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

packages/core/src/__tests__/Debouncing.test.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ beforeEach(() => jest.clearAllMocks());
1515
describe("Text Input debouncing test", () => {
1616
test.each([200, 500, 1000])(
1717
"should onChangeTextDelayed be called once with %s delay in TextInput",
18-
async (delay) => {
18+
(delay) => {
1919
const Wrapper: React.FC = () => {
2020
const [value, setValue] = React.useState("");
2121
return (
@@ -29,13 +29,13 @@ describe("Text Input debouncing test", () => {
2929
};
3030

3131
render(<Wrapper />);
32-
await testDebounce("first", "second", "", delay);
32+
testDebounce("first", "second", "", delay);
3333
}
3434
);
3535

3636
test.each([200, 500, 1000])(
3737
"should onChangeTextDelayed be called once with %s delay in TextField",
38-
async (delay) => {
38+
(delay) => {
3939
const Wrapper: React.FC = () => {
4040
const [value, setValue] = React.useState("");
4141
return (
@@ -52,13 +52,13 @@ describe("Text Input debouncing test", () => {
5252
};
5353

5454
render(<Wrapper />);
55-
await testDebounce("first", "second", "", delay);
55+
testDebounce("first", "second", "", delay);
5656
}
5757
);
5858

5959
test.each([200, 500, 1000])(
6060
"should onChangeTextDelayed be called once with %s delay in NumberInput",
61-
async (delay) => {
61+
(delay) => {
6262
const Wrapper: React.FC = () => {
6363
const [value, setValue] = React.useState<number | undefined>(0);
6464
return (
@@ -72,18 +72,18 @@ describe("Text Input debouncing test", () => {
7272
};
7373

7474
render(<Wrapper />);
75-
await testDebounce(1, 23, 0, delay);
75+
testDebounce(1, 23, 0, delay);
7676
}
7777
);
7878
});
7979

80-
async function testDebounce(
80+
function testDebounce(
8181
valueOne: string | number,
8282
valueTwo: string | number,
8383
initialValue: string | number,
8484
delay: number
8585
) {
86-
const textInput = await screen.findByTestId("native-text-input");
86+
const textInput = screen.getByTestId("native-text-input");
8787

8888
act(() => fireEvent.changeText(textInput, valueOne));
8989
act(() => fireEvent.changeText(textInput, valueTwo));

packages/core/src/components/SwipeableItem/SwipeableItem.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ const SwipeableItem: React.FC<React.PropsWithChildren<Props>> = ({
9696
const { onStartSwiping, onStopSwiping } =
9797
React.useContext(SwipeableListContext);
9898

99+
const swipeableRef = React.useRef<any>(null);
100+
99101
const { viewStyles, textStyles } = extractStyles(style);
100102

101103
const { borderStyles, marginStyles } =
@@ -176,7 +178,12 @@ const SwipeableItem: React.FC<React.PropsWithChildren<Props>> = ({
176178
const renderBehindItem = (item: SwipeableItemBehindItem, index: number) => (
177179
<Pressable
178180
key={index.toString()}
179-
onPress={(item as any).onPress}
181+
onPress={() => {
182+
item.onPress?.();
183+
if (item.closeOnPress !== false) {
184+
swipeableRef.current?.closeRow();
185+
}
186+
}}
180187
style={[
181188
styles.buttonContainer,
182189
{ backgroundColor: item.backgroundColor || theme.colors.primary },
@@ -211,6 +218,7 @@ const SwipeableItem: React.FC<React.PropsWithChildren<Props>> = ({
211218
>
212219
{/*@ts-ignore*/}
213220
<SwipeRow
221+
ref={swipeableRef}
214222
leftOpenValue={
215223
isRightSwipeHandled ? 0 : leftOpenValue || defaultLeftOpenValue //If in swiping mode, don't keep open
216224
}

packages/core/src/components/SwipeableItem/SwipeableItemCommon.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface SwipeableItemBehindItem {
55
revealSwipeDirection: "left" | "right";
66
onPress?: () => void;
77
onSwipe?: () => void;
8+
closeOnPress?: boolean;
89
icon?: string;
910
iconSize?: number;
1011
backgroundColor?: string;
@@ -53,7 +54,7 @@ export function extractLeftSwipeProps(object: object): LeftSwipeProps {
5354

5455
export function rightSwipeToSwipeableItemBehindItem(
5556
swipe: RightSwipeProps
56-
): Omit<SwipeableItemBehindItem, "onPress"> {
57+
): Omit<SwipeableItemBehindItem, "onPress" | "closeOnPress"> {
5758
return {
5859
title: swipe.rightSwipeTitle || "",
5960
revealSwipeDirection: "right",
@@ -67,7 +68,7 @@ export function rightSwipeToSwipeableItemBehindItem(
6768

6869
export function leftSwipeToSwipeableItemBehindItem(
6970
swipe: LeftSwipeProps
70-
): Omit<SwipeableItemBehindItem, "onPress"> {
71+
): Omit<SwipeableItemBehindItem, "onPress" | "closeOnPress"> {
7172
return {
7273
title: swipe.leftSwipeTitle || "",
7374
revealSwipeDirection: "left",

0 commit comments

Comments
 (0)