Skip to content

Commit

Permalink
feat: Rename showCancelWhileEditing to showCancelButtonWhileEditing prop
Browse files Browse the repository at this point in the history
Also handles the cancel button behaviour on blur, cancel, search, and focus.
  • Loading branch information
iRoachie committed Jan 8, 2018
1 parent 7e2771a commit 732faf4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
4 changes: 2 additions & 2 deletions ios/RNSearchBarManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ + (BOOL)requiresMainQueueSetup
RCT_EXPORT_VIEW_PROPERTY(text, NSString)
RCT_CUSTOM_VIEW_PROPERTY(showsCancelButton, BOOL, RNSearchBar)
{
BOOL value = [RCTConvert BOOL:json];
view.showsCancelButton = value;
BOOL value = [RCTConvert BOOL:json];
view.showsCancelButton = value;
}
RCT_EXPORT_VIEW_PROPERTY(barTintColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(tintColor, UIColor)
Expand Down
40 changes: 33 additions & 7 deletions src/SearchBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class SearchBar extends React.PureComponent {
searchBarStyle: PropTypes.oneOf(['default', 'prominent', 'minimal']),
editable: PropTypes.bool,
returnKeyType: PropTypes.string,
showsCancelButtonWhileEditing: PropTypes.bool,
}

static defaultProps = {
Expand All @@ -66,6 +67,7 @@ class SearchBar extends React.PureComponent {
autoCapitalize: 'sentences',
autoCorrect: false,
spellCheck: false,
showsCancelButtonWhileEditing: true,
onChange: () => null,
onChangeText: () => null,
onFocus: () => null,
Expand All @@ -80,9 +82,37 @@ class SearchBar extends React.PureComponent {
}

onSearchButtonPress = (e) => {
if (this.props.showsCancelButtonWhileEditing) {
NativeModules.RNSearchBarManager.toggleCancelButton(findNodeHandle(this), true)
}

this.props.onSearchButtonPress(e.nativeEvent.searchText)
}

onFocus = () => {
if (this.props.showsCancelButtonWhileEditing) {
NativeModules.RNSearchBarManager.toggleCancelButton(findNodeHandle(this), true)
}

this.props.onFocus()
}

onCancelButtonPress = () => {
if (this.props.showsCancelButtonWhileEditing) {
NativeModules.RNSearchBarManager.toggleCancelButton(findNodeHandle(this), false)
}

this.props.onCancelButtonPress()
}

onBlur = () => {
if (this.props.showsCancelButtonWhileEditing) {
NativeModules.RNSearchBarManager.toggleCancelButton(findNodeHandle(this), false)
}

this.props.onBlur()
}

blur() {
return NativeModules.RNSearchBarManager.blur(findNodeHandle(this))
}
Expand All @@ -98,12 +128,6 @@ class SearchBar extends React.PureComponent {
unFocus() {
return NativeModules.RNSearchBarManager.unFocus(findNodeHandle(this))
}

componentDidUpdate (prevProps) {
if (prevProps.showsCancelButton !== this.props.showsCancelButton) {
NativeModules.RNSearchBarManager.toggleCancelButton(findNodeHandle(this), this.props.showsCancelButton);
}
}

render() {
return (
Expand All @@ -112,8 +136,10 @@ class SearchBar extends React.PureComponent {
{...this.props}
onChange={this.onChange}
onPress={this.onPress}
onFocus={this.onFocus}
onBlur={this.onBlur}
onSearchButtonPress={this.onSearchButtonPress}
onCancelButtonPress={this.props.onCancelButtonPress}
onCancelButtonPress={this.onCancelButtonPress}
/>
)
}
Expand Down
7 changes: 7 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ interface Props {
*/
showsCancelButton?: boolean

/**
* Only shows the cancel button while the search bar has focus
*
* Default is true
*/
showsCancelButtonWhileEditing?: boolean

/**
* Indicates whether the Return key is automatically enabled when the user is entering text.
*
Expand Down

0 comments on commit 732faf4

Please sign in to comment.