Skip to content

Commit

Permalink
feat: optional search clear icon (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikDanielewicz committed Mar 25, 2024
1 parent b4ef98c commit 9939016
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/components/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const SearchBar = ({ scrollEmojiCategoryListToIndex }: SearchBarProps) =>
const {
searchPhrase,
setSearchPhrase,
hideSearchBarClearIcon,
translation,
setActiveCategoryIndex,
renderList,
Expand All @@ -23,11 +24,11 @@ export const SearchBar = ({ scrollEmojiCategoryListToIndex }: SearchBarProps) =>
} = React.useContext(KeyboardContext)
const inputRef = React.useRef<TextInput>(null)

const handleSearch = async (text: string) => {
const handleSearch = (text: string) => {
setSearchPhrase(text)

if (text === '') {
await setActiveCategoryIndex(0)
setActiveCategoryIndex(0)
scrollEmojiCategoryListToIndex(0)
setShouldAnimateScroll(enableCategoryChangeAnimation)

Expand All @@ -45,8 +46,10 @@ export const SearchBar = ({ scrollEmojiCategoryListToIndex }: SearchBarProps) =>
const clearPhrase = () => {
setSearchPhrase('')
clearEmojiTonesData()
inputRef.current?.blur()
setActiveCategoryIndex(0)
setTimeout(() => {
inputRef.current?.blur()
}, 0)
scrollEmojiCategoryListToIndex(0)
}

Expand All @@ -67,7 +70,7 @@ export const SearchBar = ({ scrollEmojiCategoryListToIndex }: SearchBarProps) =>
onTouchEndCapture={clearEmojiTonesData}
placeholderTextColor={theme.search.placeholder}
/>
{!!searchPhrase && (
{!hideSearchBarClearIcon && !!searchPhrase && (
<TouchableOpacity onPress={clearPhrase} style={styles.button}>
<Icon
iconName={'Close'}
Expand Down
2 changes: 2 additions & 0 deletions src/contexts/KeyboardContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export type KeyboardProps = {
enableRecentlyUsed?: boolean
categoryPosition?: CategoryPosition
enableSearchBar?: boolean
hideSearchBarClearIcon?: boolean
customButtons?: React.ReactNode
categoryOrder?: CategoryTypes[]
onRequestClose?: () => void
Expand Down Expand Up @@ -176,6 +177,7 @@ export const defaultKeyboardContext: Required<KeyboardProps> & {
enableRecentlyUsed: false,
categoryPosition: 'floating',
enableSearchBar: false,
hideSearchBarClearIcon: false,
customButtons: null,
categoryOrder: [...CATEGORIES],
onRequestClose: () => {},
Expand Down

0 comments on commit 9939016

Please sign in to comment.