Skip to content
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

feat(android): Add new properties to search view #1250

Merged
28 changes: 28 additions & 0 deletions Example/src/screens/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ const MainScreen = ({ navigation }: MainScreenProps): JSX.Element => {
const [search, setSearch] = useState('');
const [placeholder, setPlaceholder] = useState('Search for something...');
const [barTintColor, setBarTintColor] = useState<BarTintColor>('white');
const [textHintColor, setTextHintColor] = useState<BarTintColor>('orange');
const [headerIconColor, setHeaderIconColor] = useState<BarTintColor>(
'orange'
);
const [hintSearchIcon, setHintSearchIcon] = useState(false);
const [hideWhenScrolling, setHideWhenScrolling] = useState(false);
const [obscureBackground, setObscureBackground] = useState(false);
const [hideNavigationBar, setHideNavigationBar] = useState(false);
Expand All @@ -47,6 +52,9 @@ const MainScreen = ({ navigation }: MainScreenProps): JSX.Element => {
navigation.setOptions({
searchBar: {
barTintColor,
textHintColor,
headerIconColor,
hintSearchIcon,
hideWhenScrolling,
obscureBackground,
hideNavigationBar,
Expand Down Expand Up @@ -91,6 +99,9 @@ const MainScreen = ({ navigation }: MainScreenProps): JSX.Element => {
search,
placeholder,
barTintColor,
textHintColor,
headerIconColor,
hintSearchIcon,
hideWhenScrolling,
obscureBackground,
hideNavigationBar,
Expand Down Expand Up @@ -143,6 +154,23 @@ const MainScreen = ({ navigation }: MainScreenProps): JSX.Element => {
onValueChange={setInputType}
items={['text', 'number', 'email', 'phone']}
/>
<SettingsPicker<BarTintColor>
label="Text hint color"
value={textHintColor}
onValueChange={setTextHintColor}
items={['lightcoral', 'orange', 'darkslategray', 'white']}
/>
<SettingsPicker<BarTintColor>
label="Header icon color"
value={headerIconColor}
onValueChange={setHeaderIconColor}
items={['lightcoral', 'orange', 'darkslategray', 'white']}
/>
<SettingsSwitch
label="Show search hint icon"
value={hintSearchIcon}
onValueChange={setHintSearchIcon}
/>
<Text style={styles.heading}>Other</Text>
<Button
onPress={() => navigation.navigate('Search')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.view.*
rvasseur31 marked this conversation as resolved.
Show resolved Hide resolved
import android.view.animation.Animation
import android.view.animation.AnimationSet
import android.view.animation.Transformation
Expand Down Expand Up @@ -191,6 +186,7 @@ class ScreenStackFragment : ScreenFragment {
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
item.actionView = searchView
}

}

fun canNavigateBack(): Boolean {
Expand Down
15 changes: 15 additions & 0 deletions android/src/main/java/com/swmansion/rnscreens/SearchBarManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ class SearchBarManager : ViewGroupManager<SearchBarView>() {
view.textColor = color
}

@ReactProp(name = "headerIconColor", customType = "Color")
fun setHeaderIconColor(view: SearchBarView, color: Int?) {
view.headerIconColor = color
}

@ReactProp(name = "textHintColor", customType = "Color")
fun setTextHintColor(view: SearchBarView, color: Int?) {
view.textHintColor = color
}

@ReactProp(name = "hintSearchIcon")
rvasseur31 marked this conversation as resolved.
Show resolved Hide resolved
fun setHintSearchIcon(view: SearchBarView, hintSearchIcon: Boolean?) {
view.hintSearchIcon = hintSearchIcon ?: true
}

override fun getExportedCustomDirectEventTypeConstants(): Map<String, Any>? {
return MapBuilder.builder<String, Any>()
.put("onChangeText", MapBuilder.of("registrationName", "onChangeText"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ class SearchBarView(reactContext: ReactContext?) : ReactViewGroup(reactContext)
var autoCapitalize: SearchBarAutoCapitalize = SearchBarAutoCapitalize.NONE
var textColor: Int? = null
var tintColor: Int? = null
var placeholder: String? = null
var headerIconColor: Int? = null
var textHintColor: Int? = null
var placeholder: String? = ""
rvasseur31 marked this conversation as resolved.
Show resolved Hide resolved
var shouldOverrideBackButton: Boolean = true
var autoFocus: Boolean = false
var hintSearchIcon: Boolean = true
rvasseur31 marked this conversation as resolved.
Show resolved Hide resolved

private var mSearchViewFormatter: SearchViewFormatter? = null

Expand Down Expand Up @@ -45,9 +48,11 @@ class SearchBarView(reactContext: ReactContext?) : ReactViewGroup(reactContext)
}

searchView.inputType = inputType.toAndroidInputType(autoCapitalize)
searchView.queryHint = placeholder
mSearchViewFormatter?.setTextColor(textColor)
mSearchViewFormatter?.setTintColor(tintColor)
mSearchViewFormatter?.setHeaderIconColor(headerIconColor)
mSearchViewFormatter?.setTextHintColor(textHintColor)
mSearchViewFormatter?.setHintSearchIcon(hintSearchIcon, placeholder)
searchView.overrideBackAction = shouldOverrideBackButton
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@ package com.swmansion.rnscreens
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import androidx.appcompat.R
import androidx.appcompat.widget.SearchView

class SearchViewFormatter(var searchView: SearchView) {
private var mDefaultTextColor: Int? = null
private var mDefaultTintBackground: Drawable? = null

private val searchEditText
get() = searchView.findViewById<View>(androidx.appcompat.R.id.search_src_text) as? EditText
get() = searchView.findViewById<View>(R.id.search_src_text) as? EditText
private val searchTextPlate
get() = searchView.findViewById<View>(androidx.appcompat.R.id.search_plate)
get() = searchView.findViewById<View>(R.id.search_plate)
private val searchIcon
get() = searchView.findViewById<ImageView>(R.id.search_button)
private val searchCloseIcon
get() = searchView.findViewById<ImageView>(R.id.search_close_btn)
private val searchHintIcon
get() = searchView.findViewById<ImageView>(R.id.search_mag_icon)

fun setTextColor(textColor: Int?) {
val currentDefaultTextColor = mDefaultTextColor
Expand All @@ -37,4 +45,27 @@ class SearchViewFormatter(var searchView: SearchView) {
searchTextPlate.background = currentDefaultTintColor
}
}

fun setHeaderIconColor(headerIconColor: Int?) {
headerIconColor?.let {
searchIcon.setColorFilter(headerIconColor)
searchCloseIcon.setColorFilter(headerIconColor)
}
rvasseur31 marked this conversation as resolved.
Show resolved Hide resolved
}

fun setTextHintColor(textHintColor: Int?) {
textHintColor?.let {
searchEditText?.setHintTextColor(textHintColor)
}
rvasseur31 marked this conversation as resolved.
Show resolved Hide resolved
}

fun setHintSearchIcon(hintSearchIcon: Boolean?, placeholder: String?) {
rvasseur31 marked this conversation as resolved.
Show resolved Hide resolved
hintSearchIcon?.let {
if (hintSearchIcon) {
searchView.queryHint = placeholder
} else {
searchEditText?.hint = placeholder
}
}
rvasseur31 marked this conversation as resolved.
Show resolved Hide resolved
}
}
12 changes: 12 additions & 0 deletions createNativeStackNavigator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,18 @@ Defaults to an empty string.

The search field text color.

#### `textHintColor`

The search hint text color. (Android only)

#### `headerIconColor`

The search and close icon color shown in the header. (Android only)

#### `hintSearchIcon`

Show the search hint icon when search bar is focused. (Android only)

### Helpers

The stack navigator adds the following methods to the navigation prop:
Expand Down
4 changes: 3 additions & 1 deletion guides/GUIDE_FOR_LIBRARY_AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ To render a search bar use `ScreenStackHeaderSearchBarView` with `<SearchBar>` c
- `onSearchButtonPress` - A callback that gets called when the search button is pressed. It receives the current text value of the search bar.
- `placeholder` - Text displayed when search field is empty. Defaults to an empty string.
- `textColor` - The search field text color.

- `textHintColor` - The search hint text color. (Android only)
- `headerIconColor` - The search and close icon color shown in the header. (Android only)
- `hintSearchIcon` - Show the search hint icon when search bar is focused. (Android only)

Below is a list of properties that can be set with `ScreenStackHeaderConfig` component:

Expand Down
12 changes: 12 additions & 0 deletions native-stack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,18 @@ Defaults to an empty string.

The search field text color.

#### `textHintColor`

The search hint text color. (Android only)

#### `headerIconColor`

The search and close icon color shown in the header. (Android only)

#### `hintSearchIcon`

Show the search hint icon when search bar is focused. (Android only)

### Events

The navigator can [emit events](https://reactnavigation.org/docs/navigation-events) on certain actions. Supported events are:
Expand Down
19 changes: 19 additions & 0 deletions src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -470,4 +470,23 @@ export interface SearchBarProps {
* The search field text color
*/
textColor?: string;
/**
* The search hint text color
*
* @plaform android
*/
textHintColor?: string;
/**
* The search and close icon color shown in the header
*
* @plaform android
*/
headerIconColor?: string;
/**
* Show the search hint icon when search bar is focused
*
* @plaform android
* @default true
*/
hintSearchIcon?: boolean;
}