From 81096f1c125b46102b0631091592fbb38af075e4 Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Wed, 9 Mar 2016 23:51:00 +0100 Subject: [PATCH] feat(select): using action-sheet as ion-select interface --- .../action-sheet/action-sheet.ios.scss | 5 + .../action-sheet/action-sheet.md.scss | 4 + ionic/components/action-sheet/action-sheet.ts | 2 + .../action-sheet/action-sheet.wp.scss | 4 + ionic/components/select/select.ts | 91 +++++++++++++------ .../select/test/single-value/index.ts | 2 + .../select/test/single-value/main.html | 29 ++++-- 7 files changed, 102 insertions(+), 35 deletions(-) diff --git a/ionic/components/action-sheet/action-sheet.ios.scss b/ionic/components/action-sheet/action-sheet.ios.scss index 06a70e926a..9ebfe90ebe 100644 --- a/ionic/components/action-sheet/action-sheet.ios.scss +++ b/ionic/components/action-sheet/action-sheet.ios.scss @@ -82,6 +82,11 @@ ion-action-sheet { } } +.action-sheet-selected { + font-weight: bold; + background: white; +} + .action-sheet-destructive { color: $action-sheet-ios-button-destructive-text-color; } diff --git a/ionic/components/action-sheet/action-sheet.md.scss b/ionic/components/action-sheet/action-sheet.md.scss index c319695746..dfedf70d18 100644 --- a/ionic/components/action-sheet/action-sheet.md.scss +++ b/ionic/components/action-sheet/action-sheet.md.scss @@ -64,3 +64,7 @@ $action-sheet-md-icon-margin: 0 28px 0 0 !default; margin-bottom: $action-sheet-md-group-margin-bottom; } } + +.action-sheet-selected { + font-weight: bold; +} diff --git a/ionic/components/action-sheet/action-sheet.ts b/ionic/components/action-sheet/action-sheet.ts index 20e54b70e3..05b3968dfd 100644 --- a/ionic/components/action-sheet/action-sheet.ts +++ b/ionic/components/action-sheet/action-sheet.ts @@ -240,6 +240,8 @@ class ActionSheetCmp { } else { if (button.role === 'destructive') { button.cssClass = (button.cssClass + ' ' || '') + 'action-sheet-destructive'; + } else if (button.role === 'selected') { + button.cssClass = (button.cssClass + ' ' || '') + 'action-sheet-selected'; } buttons.push(button); } diff --git a/ionic/components/action-sheet/action-sheet.wp.scss b/ionic/components/action-sheet/action-sheet.wp.scss index a87bed9545..69f645e432 100644 --- a/ionic/components/action-sheet/action-sheet.wp.scss +++ b/ionic/components/action-sheet/action-sheet.wp.scss @@ -69,6 +69,10 @@ $action-sheet-wp-icon-margin: 0 16px 0 0 !default; } } +.action-sheet-selected { + font-weight: bold; +} + .action-sheet-cancel { background: $action-sheet-wp-button-background; } diff --git a/ionic/components/select/select.ts b/ionic/components/select/select.ts index 52d459491b..0240b03df0 100644 --- a/ionic/components/select/select.ts +++ b/ionic/components/select/select.ts @@ -2,6 +2,7 @@ import {Component, Optional, ElementRef, Renderer, Input, Output, Provider, forw import {NG_VALUE_ACCESSOR} from 'angular2/common'; import {Alert} from '../alert/alert'; +import {ActionSheet} from '../action-sheet/action-sheet'; import {Form} from '../../util/form'; import {Item} from '../item/item'; import {merge, isTrueProperty, isBlank, isCheckedProperty} from '../../util/util'; @@ -156,6 +157,11 @@ export class Select { */ @Input() checked: any = false; + /** + * @private + */ + @Input() interface: string = ''; + /** * @output {any} Any expression you want to evaluate when the selection has changed */ @@ -205,7 +211,9 @@ export class Select { } private _open() { - if (this._disabled) return; + if (this._disabled) + return; + console.debug('select, open alert'); // the user may have assigned some options specifically for the alert @@ -215,6 +223,7 @@ export class Select { // and we create a new array for the alert's two buttons alertOptions.buttons = [{ text: this.cancelText, + role: 'cancel', handler: () => { this.cancel.emit(null); } @@ -225,41 +234,67 @@ export class Select { alertOptions.title = this._item.getLabelText(); } - // user cannot provide inputs from alertOptions - // alert inputs must be created by ionic from ion-options - alertOptions.inputs = this._options.toArray().map(input => { - return { - type: (this._multi ? 'checkbox' : 'radio'), - label: input.text, - value: input.value, - checked: input.checked + let modal; + if (this.interface === 'action-sheet') { + if (this._multi) { + throw new Error('multivalue selector can not be have action-sheet interface'); } - }); + let options = this._options.toArray(); + if (options.length > 6) { + throw new Error('action-sheet interface MUST NOT have more than 6 options'); + } + alertOptions.buttons = alertOptions.buttons.concat(options.map(input => { + return { + role: (input.checked ? 'selected' : ''), + text: input.text, + handler: () => { + this.onChange(input.value); + this.change.emit(input.value); + } + } + })); - // create the alert instance from our built up alertOptions - let alert = Alert.create(alertOptions); + modal = ActionSheet.create(alertOptions); - if (this._multi) { - // use checkboxes - alert.setCssClass('select-alert multiple-select-alert'); + } else if (this.interface === '' || this.interface === 'alert') { + // user cannot provide inputs from alertOptions + // alert inputs must be created by ionic from ion-options + alertOptions.inputs = this._options.toArray().map(input => { + return { + type: (this._multi ? 'checkbox' : 'radio'), + label: input.text, + value: input.value, + checked: input.checked + } + }); + // create the alert instance from our built up alertOptions + modal = Alert.create(alertOptions); + + if (this._multi) { + // use checkboxes + modal.setCssClass('select-alert multiple-select-alert'); + + } else { + // use radio buttons + modal.setCssClass('select-alert single-select-alert'); + } + + modal.addButton({ + text: this.okText, + handler: selectedValues => { + this.onChange(selectedValues); + this.change.emit(selectedValues); + } + }); } else { - // use radio buttons - alert.setCssClass('select-alert single-select-alert'); + throw new Error('unknown interface value: ' + this.interface); } - alert.addButton({ - text: this.okText, - handler: selectedValues => { - this.onChange(selectedValues); - this.change.emit(selectedValues); - } - }); - - this._nav.present(alert, alertOptions); + this._nav.present(modal, alertOptions); this._isOpen = true; - alert.onDismiss(() => { + modal.onDismiss(() => { this._isOpen = false; }); } @@ -385,7 +420,7 @@ export class Select { /** * @private */ - onTouched() {} + onTouched() { } /** * @private diff --git a/ionic/components/select/test/single-value/index.ts b/ionic/components/select/test/single-value/index.ts index e9100120dd..c9618d1856 100644 --- a/ionic/components/select/test/single-value/index.ts +++ b/ionic/components/select/test/single-value/index.ts @@ -16,6 +16,7 @@ class E2EPage { month: string; year: string; years: Array; + notification: string; constructor() { this.gaming = ''; @@ -23,6 +24,7 @@ class E2EPage { this.music = null; this.month = '12'; this.year = '1994'; + this.notification = "enable"; this.years = [1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999]; diff --git a/ionic/components/select/test/single-value/main.html b/ionic/components/select/test/single-value/main.html index d3ad57f11f..85f8974cce 100644 --- a/ionic/components/select/test/single-value/main.html +++ b/ionic/components/select/test/single-value/main.html @@ -26,7 +26,7 @@ Operating System - + DOS Linux Mac OS 7 @@ -37,6 +37,16 @@ + + Notifications + + Enable + Mute + Mute for a week + Mute for a year + + + Music @@ -74,11 +84,16 @@ - + \ No newline at end of file