From c6335be53ccc95518baf5bc65ffbea655645d69f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jir=CC=8Ci=CC=81=20Ota=CC=81hal?= Date: Thu, 6 Oct 2016 10:33:39 +0200 Subject: [PATCH] Close toolbar search on BackAndroid press --- src/Toolbar/Toolbar.react.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Toolbar/Toolbar.react.js b/src/Toolbar/Toolbar.react.js index 890b91bb..967c5dc8 100644 --- a/src/Toolbar/Toolbar.react.js +++ b/src/Toolbar/Toolbar.react.js @@ -1,9 +1,11 @@ import React, { Component, PropTypes } from 'react'; import { Animated, + BackAndroid, findNodeHandle, - StyleSheet, NativeModules, + Platform, + StyleSheet, Text, TextInput, TouchableWithoutFeedback, @@ -220,6 +222,10 @@ class Toolbar extends Component { } ); }; + onSearchCloseRequested = () => { + this.onSearchClosePressed(); + return true; // because we need to stop propagation + } onSearchTextChanged = (value) => { const { searchable } = this.props; @@ -232,10 +238,16 @@ class Toolbar extends Component { onSearchPressed = () => { const { searchable } = this.props; + // on android it's typical that back button closes search input on toolbar + if (Platform.OS !== 'ios') { + BackAndroid.addEventListener('onSearchCloseRequested', this.onSearchCloseRequested); + } + if (searchable && isFunction(searchable.onSearchPressed)) { searchable.onSearchPressed(); } + this.setState({ isSearchActive: true, searchValue: '', @@ -244,10 +256,15 @@ class Toolbar extends Component { onSearchClosePressed = () => { const { searchable } = this.props; + if (Platform.OS !== 'ios') { + BackAndroid.removeEventListener('onSearchCloseRequested', this.onSearchCloseRequested); + } + if (searchable && isFunction(searchable.onSearchClosed)) { searchable.onSearchClosed(); } + this.setState({ isSearchActive: false, searchValue: '',