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 shouldClearOnSubmit-prop to search view #1639

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Example/src/screens/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const MainScreen = ({ navigation }: MainScreenProps): JSX.Element => {
const [autoCapitalize, setAutoCapitalize] =
useState<AutoCapitalize>('sentences');
const [inputType, setInputType] = useState<InputType>('text');
const [shouldClearOnSubmit, setShouldClearOnSubmit] = useState(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think clearOnSubmit would be a better name for the state and property overall.

const searchBarRef = useRef<SearchBarCommands>(null);

useLayoutEffect(() => {
Expand All @@ -62,6 +63,7 @@ const MainScreen = ({ navigation }: MainScreenProps): JSX.Element => {
autoCapitalize,
placeholder,
inputType,
shouldClearOnSubmit,
onChangeText: event => setSearch(event.nativeEvent.text),
onCancelButtonPress: () =>
toast.push({
Expand Down Expand Up @@ -108,6 +110,7 @@ const MainScreen = ({ navigation }: MainScreenProps): JSX.Element => {
hideNavigationBar,
autoCapitalize,
inputType,
shouldClearOnSubmit,
]);

return (
Expand Down Expand Up @@ -172,6 +175,11 @@ const MainScreen = ({ navigation }: MainScreenProps): JSX.Element => {
value={shouldShowHintSearchIcon}
onValueChange={setShouldShowHintSearchIcon}
/>
<SettingsSwitch
label="Clear on submit"
value={shouldClearOnSubmit}
onValueChange={setShouldClearOnSubmit}
/>
<Text style={styles.heading}>Imperative actions</Text>
<Button onPress={() => searchBarRef.current?.blur()} title="Blur" />
<Button onPress={() => searchBarRef.current?.focus()} title="Focus" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ class SearchBarManager : ViewGroupManager<SearchBarView>() {
}
}

@ReactProp(name = "shouldClearOnSubmit")
fun setShouldClearOnSubmit(view: SearchBarView, shouldClearOnSubmit: Boolean?) {
if (shouldClearOnSubmit != null) {
view.shouldClearOnSubmit = shouldClearOnSubmit
}
Comment on lines +82 to +84
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change this into

Suggested change
if (shouldClearOnSubmit != null) {
view.shouldClearOnSubmit = shouldClearOnSubmit
}
view.shouldClearOnSubmit = shouldClearOnSubmit ?: false

}

@ReactProp(name = "textColor", customType = "Color")
fun setTextColor(view: SearchBarView, color: Int?) {
view.textColor = color
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class SearchBarView(reactContext: ReactContext?) : ReactViewGroup(reactContext)
var shouldOverrideBackButton: Boolean = true
var autoFocus: Boolean = false
var shouldShowHintSearchIcon: Boolean = true
var shouldClearOnSubmit: Boolean = false

private var mSearchViewFormatter: SearchViewFormatter? = null

Expand Down Expand Up @@ -63,7 +64,7 @@ class SearchBarView(reactContext: ReactContext?) : ReactViewGroup(reactContext)
mSearchViewFormatter?.setHeaderIconColor(headerIconColor)
mSearchViewFormatter?.setHintTextColor(hintTextColor)
mSearchViewFormatter?.setPlaceholder(placeholder, shouldShowHintSearchIcon)
searchView.overrideBackAction = shouldOverrideBackButton
searchView?.overrideBackAction = shouldOverrideBackButton
}
}

Expand Down Expand Up @@ -124,6 +125,9 @@ class SearchBarView(reactContext: ReactContext?) : ReactViewGroup(reactContext)

private fun handleTextSubmit(newText: String?) {
sendEvent(SearchBarSearchButtonPressEvent(surfaceId, id, newText))
if (shouldClearOnSubmit) {
mSearchViewFormatter?.clearText()
}
}

private fun sendEvent(event: Event<*>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.appcompat.widget.SearchView
class SearchViewFormatter(var searchView: SearchView) {
private var mDefaultTextColor: Int? = null
private var mDefaultTintBackground: Drawable? = null
private var mDefaultText: String = ""
Copy link
Member

@tboba tboba Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is default text needed here? I believe placeholder is being used as a default text to show when the prompt is empty - if this prop is not being served as a Search Bar prop, let's remove it.


private val searchEditText
get() = searchView.findViewById<View>(R.id.search_src_text) as? EditText
Expand Down Expand Up @@ -64,4 +65,8 @@ class SearchViewFormatter(var searchView: SearchView) {
searchEditText?.hint = placeholder
}
}

fun clearText() {
searchEditText?.setText(mDefaultText)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the upper comment, let's change this to

Suggested change
searchEditText?.setText(mDefaultText)
searchEditText?.setText(null)

}
}
Copy link
Member

@tboba tboba Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we don't hold the implementation of the Native Stack v5 anymore, I believe this change of the file is unnecessary.

Empty file.
1 change: 1 addition & 0 deletions guides/GUIDE_FOR_LIBRARY_AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ To render a search bar use `ScreenStackHeaderSearchBarView` with `<SearchBar>` c
- `hintTextColor` - The search hint text color. (Android only)
- `headerIconColor` - The search and close icon color shown in the header. (Android only)
- `shouldShowHintSearchIcon` - Show the search hint icon when search bar is focused. (Android only)
- `shouldClearOnSubmit` - Clear the search input when the search button is pressed. (Android only) Defaults to `false`. (Android only)
- `ref` - A React ref to imperatively modify search bar.

Allowed imperative actions on search bar are:
Expand Down
6 changes: 6 additions & 0 deletions native-stack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,12 @@ The search and close icon color shown in the header. (Android only)

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

#### `shouldClearOnSubmit`

Clear the search input when the search button is pressed. (Android only)

Defaults to false.

#### `ref`

A React ref to imperatively modify search bar. Supported actions:
Expand Down
7 changes: 7 additions & 0 deletions src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -684,4 +684,11 @@ export interface SearchBarProps {
* @default true
*/
shouldShowHintSearchIcon?: boolean;
/**
* Clear the search input when search button is pressed.
*
* @plaform android
* @default false
*/
shouldClearOnSubmit?: boolean;
Comment on lines +687 to +693
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said, I'd suggest changing the name of this prop to clearOnSubmit.

}
Loading