Skip to content

Commit a3bf28e

Browse files
CP-9931: Move SearchBar to k2-alpine (#2328)
1 parent aa65ec1 commit a3bf28e

File tree

8 files changed

+90
-17
lines changed

8 files changed

+90
-17
lines changed

packages/core-mobile/app/new/routes/(signedIn)/(modals)/tokenManagement/addCustomToken.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
Button,
33
Icons,
4+
SearchBar,
45
Text,
56
TouchableOpacity,
67
useTheme,
@@ -12,7 +13,6 @@ import useAddCustomToken from 'screens/tokenManagement/hooks/useAddCustomToken'
1213
import { GlobalLoadingState } from 'common/components/GlobalLoadingState'
1314
import { LocalTokenWithBalance } from 'store/balance'
1415
import { useLocalSearchParams, useRouter } from 'expo-router'
15-
import { SearchBar } from 'features/portfolio/assets/components/SearchBar'
1616
import { LogoWithNetwork } from 'features/portfolio/assets/components/LogoWithNetwork'
1717

1818
const AddCustomTokenScreen = (): JSX.Element => {
@@ -86,7 +86,7 @@ const AddCustomTokenScreen = (): JSX.Element => {
8686
onTextChanged={setTokenAddress}
8787
searchText={tokenAddress}
8888
placeholder="Token contract address"
89-
rightIconWhenBlur={
89+
rightComponent={
9090
<TouchableOpacity
9191
onPress={goToScanQrCode}
9292
hitSlop={16}

packages/core-mobile/app/new/routes/(signedIn)/(modals)/tokenManagement/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ListRenderItemInfo } from 'react-native'
33
import { LocalTokenWithBalance } from 'store/balance/types'
44
import {
55
Icons,
6+
SearchBar,
67
Text,
78
TouchableOpacity,
89
useTheme,
@@ -14,7 +15,6 @@ import { TokenType } from '@avalabs/vm-module-types'
1415
import { useRouter } from 'expo-router'
1516
import { useSearchableTokenList } from 'common/hooks/useSearchableTokenList'
1617
import { FlatList } from 'react-native-gesture-handler'
17-
import { SearchBar } from 'features/portfolio/assets/components/SearchBar'
1818
import TokenManagementItem from 'features/portfolio/assets/components/TokenManagementItem'
1919

2020
const TokenManagementScreen = (): JSX.Element => {

packages/core-mobile/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
"jail-monkey": "2.8.0",
117117
"jimp": "1.6.0",
118118
"jsc-android": "294992.0.0",
119+
"lodash.debounce": "4.0.8",
119120
"lodash.isempty": "4.4.0",
120121
"lodash.isstring": "4.0.1",
121122
"lodash.merge": "4.6.2",

packages/k2-alpine/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"expo-linear-gradient": "12.7.2",
2525
"expo-splash-screen": "0.27.6",
2626
"expo-status-bar": "1.12.1",
27+
"lodash.debounce": "4.0.8",
2728
"react": "18.3.1",
2829
"react-content-loader": "6.2.0",
2930
"react-native": "0.73.7",
@@ -57,6 +58,7 @@
5758
"@storybook/addon-ondevice-backgrounds": "7.6.20",
5859
"@storybook/addon-ondevice-controls": "7.6.20",
5960
"@storybook/react-native": "7.6.20",
61+
"@types/lodash.debounce": "4.0.9",
6062
"@types/react": "18.3.11",
6163
"@types/tinycolor2": "1.4.5",
6264
"babel-loader": "9.2.1",
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React, { useState } from 'react'
2+
import { ScrollView, TouchableOpacity, View } from '../Primitives'
3+
import { Icons, showAlert } from '../..'
4+
import { SearchBar } from './SearchBar'
5+
6+
export default {
7+
title: 'SearchBar'
8+
}
9+
10+
export const All = (): JSX.Element => {
11+
const [value, setValue] = useState('')
12+
const [value1, setValue1] = useState('')
13+
14+
return (
15+
<ScrollView
16+
style={{ width: '100%', backgroundColor: 'transparent' }}
17+
contentContainerStyle={{ padding: 16 }}>
18+
<View
19+
style={{
20+
marginBottom: 20,
21+
gap: 20
22+
}}>
23+
<SearchBar
24+
onTextChanged={setValue}
25+
searchText={value}
26+
placeholder="SearchBar with custom icon"
27+
rightComponent={
28+
<TouchableOpacity
29+
onPress={() =>
30+
showAlert({
31+
title: 'custom icon pressed',
32+
buttons: [{ text: 'OK' }]
33+
})
34+
}
35+
hitSlop={16}>
36+
<Icons.Custom.QRCodeScanner />
37+
</TouchableOpacity>
38+
}
39+
/>
40+
<SearchBar
41+
onTextChanged={setValue1}
42+
searchText={value1}
43+
placeholder="SearchBar without custom icon"
44+
/>
45+
</View>
46+
</ScrollView>
47+
)
48+
}
Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import {
88
} from 'react-native'
99
import React, { FC, useCallback, useEffect, useRef, useState } from 'react'
1010
import debounce from 'lodash.debounce'
11-
import { Icons, TouchableOpacity, View, useTheme } from '@avalabs/k2-alpine'
11+
import { TouchableOpacity, View } from '../Primitives'
12+
import { useTheme } from '../../hooks'
13+
import { Icons } from '../../theme/tokens/Icons'
1214

1315
interface Props extends Omit<TextInputProps, 'value' | 'onChangeText'> {
1416
onTextChanged: (value: string) => void
@@ -20,25 +22,27 @@ interface Props extends Omit<TextInputProps, 'value' | 'onChangeText'> {
2022
testID?: string
2123
setSearchBarFocused?: (value: boolean) => void
2224
containerStyle?: ViewStyle
23-
rightIconWhenBlur?: JSX.Element
25+
rightComponent?: JSX.Element
2426
}
2527

2628
const SCREEN_WIDTH = Dimensions.get('window').width
2729
const INPUT_SIZE = SCREEN_WIDTH - 106
2830
const DEFAULT_DEBOUNCE_MILLISECONDS = 150
31+
const HEIGHT = 40
2932

3033
/**
3134
* SearchBar component. Text state is handled outside the
3235
* component except for when the text is cleared.
3336
*
34-
* Shows QRCodeScanner button when search bar has no search text.
37+
* Shows custom right component when search bar has no search text and a right component is provided.
3538
* Shows Clear button when search bar has search text.
3639
*
3740
* @param onTextChanged callback to implementing view
3841
* @param searchText current search text
3942
* @param placeholder defaults to 'Search'
4043
* @param debounce if true, will delay calling 'onTextChanged' by default ms
4144
* @param textColor
45+
* @param rightComponent custom right component to show when search text is empty
4246
* @param rest all other props
4347
* @constructor
4448
*/
@@ -51,7 +55,7 @@ export const SearchBar: FC<Props> = ({
5155
textColor,
5256
setSearchBarFocused,
5357
containerStyle,
54-
rightIconWhenBlur,
58+
rightComponent,
5559
...rest
5660
}) => {
5761
const textInputRef = useRef<TextInput>(null)
@@ -85,15 +89,35 @@ export const SearchBar: FC<Props> = ({
8589
}
8690
}
8791

92+
const renderRightComponent = (): React.JSX.Element | undefined => {
93+
if (searchText.length > 0) {
94+
return (
95+
<TouchableOpacity onPress={clearText} hitSlop={16}>
96+
<Icons.Action.Clear color={colors.$textSecondary} />
97+
</TouchableOpacity>
98+
)
99+
}
100+
if (
101+
(searchText === undefined || searchText.length === 0) &&
102+
rightComponent
103+
) {
104+
return rightComponent
105+
}
106+
}
107+
88108
return (
89109
<View
90110
style={[
91-
{ backgroundColor: colors.$surfaceSecondary, borderRadius: 1000 },
111+
{
112+
backgroundColor: colors.$surfaceSecondary,
113+
borderRadius: 1000,
114+
height: HEIGHT,
115+
justifyContent: 'center'
116+
},
92117
containerStyle
93118
]}>
94119
<View
95120
style={{
96-
alignItems: 'center',
97121
flexDirection: 'row',
98122
paddingHorizontal: 12,
99123
marginVertical: Platform.OS === 'ios' ? 11 : 0
@@ -129,13 +153,7 @@ export const SearchBar: FC<Props> = ({
129153
{...rest}
130154
/>
131155
</View>
132-
{searchText && searchText.length > 0 ? (
133-
<TouchableOpacity onPress={clearText} hitSlop={16}>
134-
<Icons.Action.Clear color={colors.$textSecondary} />
135-
</TouchableOpacity>
136-
) : (
137-
rightIconWhenBlur
138-
)}
156+
{renderRightComponent()}
139157
</View>
140158
</View>
141159
)

packages/k2-alpine/src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ export * from './Dropdown/usePopoverAnchor'
2323
export * from './Toggle/Toggle'
2424
export * from './Animated/AnimatedText'
2525
export * from './Animated/AnimatedPressable'
26+
export * from './SearchBar/SearchBar'

yarn.lock

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ __metadata:
304304
jimp: 1.6.0
305305
jsc-android: 294992.0.0
306306
json-stringify-pretty-compact: 4.0.0
307+
lodash.debounce: 4.0.8
307308
lodash.isempty: 4.4.0
308309
lodash.isstring: 4.0.1
309310
lodash.merge: 4.6.2
@@ -504,6 +505,7 @@ __metadata:
504505
"@storybook/addon-ondevice-backgrounds": 7.6.20
505506
"@storybook/addon-ondevice-controls": 7.6.20
506507
"@storybook/react-native": 7.6.20
508+
"@types/lodash.debounce": 4.0.9
507509
"@types/react": 18.3.11
508510
"@types/tinycolor2": 1.4.5
509511
babel-loader: 9.2.1
@@ -518,6 +520,7 @@ __metadata:
518520
expo-splash-screen: 0.27.6
519521
expo-status-bar: 1.12.1
520522
json-stringify-pretty-compact: 4.0.0
523+
lodash.debounce: 4.0.8
521524
patch-package: 8.0.0
522525
react: 18.3.1
523526
react-content-loader: 6.2.0
@@ -27100,7 +27103,7 @@ react-native-webview@ava-labs/react-native-webview:
2710027103
peerDependencies:
2710127104
react: "*"
2710227105
react-native: "*"
27103-
checksum: 869028a5bb7a4a8a125d274753b703c2b3579b5efc7bb82db136a186fd88a11c5a4696505ebcd976d20fcdd41111abe229ae0e19c27c50bb30fe4f52bb6383bc
27106+
checksum: ae658f8c94177d1b419ffb73d26bd70474b6c688af507daaabc11451f983f77a906b2aafcdfdd09c96cab846d9722dcaf26a7c22ee7d19fb4cf70578c8812ac5
2710427107
languageName: node
2710527108
linkType: hard
2710627109

0 commit comments

Comments
 (0)