From b8ad42c7979f64db1277f7f8f6fa029733c6dfc3 Mon Sep 17 00:00:00 2001 From: Brandy Carney Date: Thu, 10 Dec 2015 18:52:19 -0500 Subject: [PATCH] refactor(searchbar): changed Cancel button to show by default MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Renamed some searchbar input properties. Fixed the cancel button showing when it wasn’t focused. --- ionic/components/searchbar/searchbar.ios.scss | 9 ++- ionic/components/searchbar/searchbar.ts | 58 ++++++++++--------- .../searchbar/test/floating/main.html | 8 +-- 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/ionic/components/searchbar/searchbar.ios.scss b/ionic/components/searchbar/searchbar.ios.scss index 34b9e549a6..9c7ebe4b9b 100644 --- a/ionic/components/searchbar/searchbar.ios.scss +++ b/ionic/components/searchbar/searchbar.ios.scss @@ -121,6 +121,12 @@ ion-searchbar { .searchbar-input { padding-left: 30px; } +} + +// Searchbar Focused +// ----------------------------------------- + +.searchbar-focused { .searchbar-ios-cancel { transform: translateX(0); flex: 0 0 auto; @@ -129,7 +135,6 @@ ion-searchbar { } } - // Searchbar in Toolbar // ----------------------------------------- @@ -148,7 +153,7 @@ ion-searchbar { } } - .searchbar-left-aligned .searchbar-ios-cancel { + .searchbar-focused .searchbar-ios-cancel { padding-left: 8px; } diff --git a/ionic/components/searchbar/searchbar.ts b/ionic/components/searchbar/searchbar.ts index 3266be9e36..df3fd32908 100644 --- a/ionic/components/searchbar/searchbar.ts +++ b/ionic/components/searchbar/searchbar.ts @@ -19,43 +19,38 @@ import {Button} from '../button/button'; * ``` * * @property [placeholder] - sets input placeholder to value passed in - * @property [show-cancel] - shows the cancel button based on boolean value passed in - * @property [cancel-text] - sets the cancel button text to the value passed in + * @property [hide-cancel-button] - hides the cancel button + * @property [cancel-button-text] - sets the cancel button text to the value passed in * @property [cancel-action] - the function that gets called by clicking the cancel button * @see {@link /docs/v2/components#search Search Component Docs} */ @ConfigComponent({ selector: 'ion-searchbar', + inputs: [ + 'cancelAction', + 'hideCancelButton', + 'cancelButtonText' + ], defaultInputs: { - 'showCancel': false, - 'cancelText': 'Cancel', 'placeholder': 'Search' }, - inputs: ['cancelAction'], host: { '[class.searchbar-left-aligned]': 'shouldLeftAlign', '[class.searchbar-focused]': 'isFocused', }, template: '
' + - '' + + '' + '
' + - '' + - '' + + '' + + '' + '
' + - '', + '', directives: [FORM_DIRECTIVES, NgIf, NgClass, Icon, Button, forwardRef(() => SearchbarInput)] }) export class Searchbar extends Ion { @ViewChild(forwardRef(() => SearchbarInput)) searchbarInput; - /** - * @private - * This holds the searchbar input value used for querying - */ - query: string; - constructor( elementRef: ElementRef, config: Config, @@ -71,17 +66,31 @@ export class Searchbar extends Ion { this.ngControl = ngControl; this.ngControl.valueAccessor = this; + + this.ngControl.value = this.ngControl.value || ''; + } + + /** + * @private + * On Initialization check for hideCancelButton + */ + ngOnInit() { + let hideCancelButton = this.hideCancelButton; + if (typeof hideCancelButton === 'string') { + this.hideCancelButton = (hideCancelButton === '' || hideCancelButton === 'true'); + } + console.log(this.cancelButtonText); + console.log(this.hideCancelButton); } /** * @private * After the view has initialized check if the searchbar has a value - * and then store that value in query */ ngAfterViewInit() { + console.log("testing"); // If the user passes in a value to the model we should left align - this.shouldLeftAlign = this.searchbarInput.value && this.searchbarInput.value.trim() != ''; - this.query = this.searchbarInput.value || ''; + this.shouldLeftAlign = this.ngControl.value && this.ngControl.value.trim() != ''; } /** @@ -110,7 +119,6 @@ export class Searchbar extends Ion { clearInput() { this.searchbarInput.writeValue(''); this.searchbarInput.onChange(''); - this.query = ''; } /** @@ -118,12 +126,12 @@ export class Searchbar extends Ion { * Blurs the input field, clears the input field and removes the left align * then calls the custom cancel function if the user passed one in. */ - cancelSearchbar(event, query) { + cancelSearchbar(event, value) { this.searchbarInput.elementRef.nativeElement.blur(); this.clearInput(); this.shouldLeftAlign = false; - this.cancelAction && this.cancelAction(event, query); + this.cancelAction && this.cancelAction(event, value); } } @@ -136,12 +144,6 @@ export class Searchbar extends Ion { } }) export class SearchbarInput { - /** - * @private - * This holds the searchbar input value used for querying - */ - query: string; - constructor( @Host() searchbar: Searchbar, elementRef: ElementRef, diff --git a/ionic/components/searchbar/test/floating/main.html b/ionic/components/searchbar/test/floating/main.html index 8824eee667..2c312e7f2d 100644 --- a/ionic/components/searchbar/test/floating/main.html +++ b/ionic/components/searchbar/test/floating/main.html @@ -9,14 +9,14 @@
Search - Custom Placeholder
-
Search - Default Cancel Button
- +
Search - Hide Cancel Button
+
Search - Custom Cancel Button Danger
- +
Search - Custom Cancel Action
- +
Clicked custom action with input = {{customCancelAction}}