-
Notifications
You must be signed in to change notification settings - Fork 638
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
Swaps Rearchitecture #5705
Swaps Rearchitecture #5705
Changes from all commits
1a362a2
79ead15
ad7a7db
aa44b64
6128df9
640808a
e63a868
7c69214
8b9ece0
203f9e6
ed61347
0cc9498
311ee19
414c4dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import React, { useMemo } from 'react'; | ||
import { TextInput } from 'react-native'; | ||
import Animated, { runOnUI, useAnimatedRef, useDerivedValue } from 'react-native-reanimated'; | ||
import React, { useCallback, useMemo } from 'react'; | ||
import { NativeSyntheticEvent, TextInputChangeEventData } from 'react-native'; | ||
import Animated, { useAnimatedProps, useDerivedValue } from 'react-native-reanimated'; | ||
import { ButtonPressAnimation } from '@/components/animations'; | ||
import { Input } from '@/components/inputs'; | ||
import { AnimatedText, Bleed, Box, Column, Columns, Text, useColorMode, useForegroundColor } from '@/design-system'; | ||
import { LIGHT_SEPARATOR_COLOR, SEPARATOR_COLOR, THICK_BORDER_WIDTH } from '@/__swaps__/screens/Swap/constants'; | ||
import { opacity } from '@/__swaps__/utils/swaps'; | ||
import { useSwapContext } from '@/__swaps__/screens/Swap/providers/swap-provider'; | ||
import { userAssetsStore } from '@/state/assets/userAssets'; | ||
|
||
const AnimatedInput = Animated.createAnimatedComponent(Input); | ||
|
||
|
@@ -21,11 +22,9 @@ export const SearchInput = ({ | |
handleFocusSearch: () => void; | ||
output?: boolean; | ||
}) => { | ||
const { inputProgress, outputProgress, SwapInputController, AnimatedSwapStyles } = useSwapContext(); | ||
const { searchInputRef, inputProgress, outputProgress, AnimatedSwapStyles } = useSwapContext(); | ||
const { isDarkMode } = useColorMode(); | ||
|
||
const inputRef = useAnimatedRef<TextInput>(); | ||
|
||
const fillTertiary = useForegroundColor('fillTertiary'); | ||
const label = useForegroundColor('label'); | ||
const labelQuaternary = useForegroundColor('labelQuaternary'); | ||
|
@@ -38,9 +37,22 @@ export const SearchInput = ({ | |
return 'Close'; | ||
}); | ||
|
||
const initialValue = useMemo(() => { | ||
return SwapInputController.searchQuery.value; | ||
}, [SwapInputController.searchQuery.value]); | ||
const defaultValue = useMemo(() => { | ||
return userAssetsStore.getState().searchQuery; | ||
}, []); | ||
|
||
const onSearchQueryChange = useCallback((event: NativeSyntheticEvent<TextInputChangeEventData>) => { | ||
userAssetsStore.setState({ searchQuery: event.nativeEvent.text }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also unrelated to the main focus of this pr, but i feel like this would be cleaner if it just accepts a string. that way you wouldn't be creating artificial NativeSyntheticEvents below |
||
}, []); | ||
|
||
const searchInputValue = useAnimatedProps(() => { | ||
const isFocused = inputProgress.value === 1 || outputProgress.value === 1; | ||
|
||
// Removing the value when the input is focused allows the input to be reset to the correct value on blur | ||
const query = isFocused ? undefined : defaultValue; | ||
|
||
return { defaultValue, text: query }; | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i dont think default value needs to be an animated prop but we can change this later There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, but when I removed it typescript was yelling at me for whatever reason.. |
||
|
||
return ( | ||
<Box width="full"> | ||
|
@@ -68,18 +80,20 @@ export const SearchInput = ({ | |
</Box> | ||
</Column> | ||
<AnimatedInput | ||
onChange={e => { | ||
runOnUI(SwapInputController.onChangeSearchQuery)(e.nativeEvent.text); | ||
}} | ||
animatedProps={searchInputValue} | ||
onChange={onSearchQueryChange} | ||
onBlur={() => { | ||
onSearchQueryChange({ | ||
nativeEvent: { | ||
text: '', | ||
}, | ||
} as NativeSyntheticEvent<TextInputChangeEventData>); | ||
handleExitSearch(); | ||
}} | ||
onFocus={() => { | ||
handleFocusSearch(); | ||
}} | ||
onFocus={handleFocusSearch} | ||
placeholder={output ? 'Find a token to buy' : 'Search your tokens'} | ||
placeholderTextColor={isDarkMode ? opacity(labelQuaternary, 0.3) : labelQuaternary} | ||
ref={inputRef} | ||
ref={searchInputRef} | ||
selectionColor={color} | ||
spellCheck={false} | ||
style={{ | ||
|
@@ -89,7 +103,6 @@ export const SearchInput = ({ | |
height: 44, | ||
zIndex: 10, | ||
}} | ||
defaultValue={initialValue} | ||
/> | ||
</Columns> | ||
</Box> | ||
|
@@ -98,8 +111,13 @@ export const SearchInput = ({ | |
<Column width="content"> | ||
<ButtonPressAnimation | ||
onPress={() => { | ||
onSearchQueryChange({ | ||
nativeEvent: { | ||
text: '', | ||
}, | ||
} as NativeSyntheticEvent<TextInputChangeEventData>); | ||
handleExitSearch(); | ||
inputRef.current?.blur(); | ||
searchInputRef.current?.blur(); | ||
}} | ||
scaleTo={0.8} | ||
> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i am most likely just misunderstanding but why do we need to set the default value to the initial searchQuery? Wouldn't the initial searchQuery just be ""?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think that assumption is always true, good point.