Skip to content

Commit

Permalink
[IOPAE-1235] Add onCancel prop on SearchInput (#286)
Browse files Browse the repository at this point in the history
## Short description
This PR adds `onCancel` prop on `SearchInput` component.

## How to test
1. Launch the example app
2. Go to the **SearchCustom** screen
  • Loading branch information
adelloste authored Jun 11, 2024
1 parent d6ee99a commit 37a6cf3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
3 changes: 3 additions & 0 deletions example/src/pages/SearchCustom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import {
SearchInput,
VStack
} from "@pagopa/io-app-design-system";
import { useNavigation } from "@react-navigation/native";
import React from "react";
import { Alert, ScrollView } from "react-native";

export const SearchCustom = () => {
const navigation = useNavigation();
const [inputValue, setInputValue] = React.useState("");

return (
Expand All @@ -26,6 +28,7 @@ export const SearchCustom = () => {
accessibilityLabel="Search input"
cancelButtonLabel="Annulla"
value={inputValue}
onCancel={() => navigation.goBack()}
onChangeText={setInputValue}
/>
</VStack>
Expand Down
29 changes: 14 additions & 15 deletions src/components/searchInput/SearchInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable functional/immutable-data */
import React, { useCallback, useMemo, useRef, useState } from "react";
import React, { useCallback, useRef, useState } from "react";
import {
ColorValue,
Dimensions,
Expand Down Expand Up @@ -58,11 +58,13 @@ type SearchInputPressableProps = {
type SearchInputActionProps =
| {
pressable: SearchInputPressableProps;
onCancel?: never;
onChangeText?: never;
value?: never;
}
| {
pressable?: never;
onCancel: (event: GestureResponderEvent) => void;
onChangeText: (value: string) => void;
value: string;
};
Expand All @@ -88,6 +90,7 @@ export const SearchInput = ({
accessibilityLabel,
cancelButtonLabel,
clearAccessibilityLabel,
onCancel,
onChangeText,
placeholder,
value = "",
Expand All @@ -105,11 +108,8 @@ export const SearchInput = ({
/* Widths used for the transition:
- `SearchInput` entire width
- `Cancel` button */
const inputWidth: number = useMemo(
() =>
Dimensions.get("window").width - IOVisualCostants.appMarginDefault * 2,
[]
);
const inputWidth: number =
Dimensions.get("window").width - IOVisualCostants.appMarginDefault * 2;

const [cancelButtonWidth, setCancelButtonWidth] =
useState<LayoutRectangle["width"]>(0);
Expand All @@ -118,10 +118,7 @@ export const SearchInput = ({
setCancelButtonWidth(nativeEvent.layout.width);
};

const inputWidthWithCancel: number = useMemo(
() => inputWidth - cancelButtonWidth,
[inputWidth, cancelButtonWidth]
);
const inputWidthWithCancel: number = inputWidth - cancelButtonWidth;

/* Reanimated styles */
const inputAnimatedWidth = useSharedValue<number>(inputWidth);
Expand Down Expand Up @@ -179,11 +176,13 @@ export const SearchInput = ({
inputAnimatedWidth.value = inputWidth;
};

const cancel = useCallback(() => {
onChangeText?.("");
searchInputRef.current?.clear();
searchInputRef.current?.blur();
}, [onChangeText]);
const cancel = useCallback(
(event: GestureResponderEvent) => {
onChangeText?.("");
onCancel?.(event);
},
[onCancel, onChangeText]
);

const clear = useCallback(() => {
onChangeText?.("");
Expand Down

0 comments on commit 37a6cf3

Please sign in to comment.