refactor(select): rename alertOptions to selectOptions, add ability to pass them for action-sheet

BREAKING CHANGES:

Select’s `alertOptions` input has been renamed to `selectOptions`. It
now allows you to pass options for either the alert or action-sheet
interface. Refer to their documentation for the options each of them
accept.

http://ionicframework.com/docs/v2/api/components/action-sheet/ActionShee
tController/#create
http://ionicframework.com/docs/v2/api/components/alert/AlertController/#
create

fixes #7764
This commit is contained in:
Brandy Carney
2016-08-23 20:15:49 -04:00
parent 8c1662d5e9
commit b8285b7ba8
3 changed files with 34 additions and 24 deletions

View File

@ -96,18 +96,18 @@ export const SELECT_VALUE_ACCESSOR = new Provider(
* ### Alert Options * ### Alert Options
* *
* Since `ion-select` is a wrapper to `Alert`, by default, it can be * Since `ion-select` is a wrapper to `Alert`, by default, it can be
* passed options in the `alertOptions` property. This can be used to * passed options in the `selectOptions` property. This can be used to
* pass a custom alert title, subtitle or message. See the {@link ../../alert/Alert Alert API docs} * pass a custom alert title, subtitle or message. See the {@link ../../alert/Alert Alert API docs}
* for more properties. * for more properties.
* *
* ```html * ```html
* <ion-select [alertOptions]="alertOptions"> * <ion-select [selectOptions]="selectOptions">
* ... * ...
* </ion-select> * </ion-select>
* ``` * ```
* *
* ```ts * ```ts
* this.alertOptions = { * this.selectOptions = {
* title: 'Pizza Toppings', * title: 'Pizza Toppings',
* subTitle: 'Select your toppings' * subTitle: 'Select your toppings'
* }; * };
@ -169,10 +169,12 @@ export class Select implements AfterContentInit, ControlValueAccessor, OnDestroy
@Input() placeholder: string; @Input() placeholder: string;
/** /**
* @input {any} Any addition options that the alert interface can take. * @input {any} Any additional options that the `alert` or `action-sheet` interface can take.
* See the [Alert API docs](../../alert/Alert) for the create options. * See the [Alert API docs](../../alert/AlertController/#create) and the
* [ActionSheetController API docs](../../action-sheet/ActionSheetController/#create) for the
* create options for each interface.
*/ */
@Input() alertOptions: any = {}; @Input() selectOptions: any = {};
/** /**
* @input {string} The interface the select should use: `action-sheet` or `alert`. Default: `alert`. * @input {string} The interface the select should use: `action-sheet` or `alert`. Default: `alert`.
@ -240,11 +242,11 @@ export class Select implements AfterContentInit, ControlValueAccessor, OnDestroy
console.debug('select, open alert'); console.debug('select, open alert');
// the user may have assigned some options specifically for the alert // the user may have assigned some options specifically for the alert
let alertOptions = merge({}, this.alertOptions); let selectOptions = merge({}, this.selectOptions);
// make sure their buttons array is removed from the options // make sure their buttons array is removed from the options
// and we create a new array for the alert's two buttons // and we create a new array for the alert's two buttons
alertOptions.buttons = [{ selectOptions.buttons = [{
text: this.cancelText, text: this.cancelText,
role: 'cancel', role: 'cancel',
handler: () => { handler: () => {
@ -252,9 +254,9 @@ export class Select implements AfterContentInit, ControlValueAccessor, OnDestroy
} }
}]; }];
// if the alertOptions didn't provide an title then use the label's text // if the selectOptions didn't provide a title then use the label's text
if (!alertOptions.title && this._item) { if (!selectOptions.title && this._item) {
alertOptions.title = this._item.getLabelText(); selectOptions.title = this._item.getLabelText();
} }
let options = this._options.toArray(); let options = this._options.toArray();
@ -270,7 +272,7 @@ export class Select implements AfterContentInit, ControlValueAccessor, OnDestroy
let overlay: any; let overlay: any;
if (this.interface === 'action-sheet') { if (this.interface === 'action-sheet') {
alertOptions.buttons = alertOptions.buttons.concat(options.map(input => { selectOptions.buttons = selectOptions.buttons.concat(options.map(input => {
return { return {
role: (input.selected ? 'selected' : ''), role: (input.selected ? 'selected' : ''),
text: input.text, text: input.text,
@ -280,17 +282,21 @@ export class Select implements AfterContentInit, ControlValueAccessor, OnDestroy
} }
}; };
})); }));
alertOptions.cssClass = 'select-action-sheet'; var selectCssClass = 'select-action-sheet';
overlay = new ActionSheet(this._app, alertOptions); // If the user passed a cssClass for the select, add it
selectCssClass += selectOptions.cssClass ? ' ' + selectOptions.cssClass : '';
selectOptions.cssClass = selectCssClass;
overlay = new ActionSheet(this._app, selectOptions);
} else { } else {
// default to use the alert interface // default to use the alert interface
this.interface = 'alert'; this.interface = 'alert';
// user cannot provide inputs from alertOptions // user cannot provide inputs from selectOptions
// alert inputs must be created by ionic from ion-options // alert inputs must be created by ionic from ion-options
alertOptions.inputs = this._options.map(input => { selectOptions.inputs = this._options.map(input => {
return { return {
type: (this._multi ? 'checkbox' : 'radio'), type: (this._multi ? 'checkbox' : 'radio'),
label: input.text, label: input.text,
@ -302,8 +308,8 @@ export class Select implements AfterContentInit, ControlValueAccessor, OnDestroy
var selectCssClass = 'select-alert'; var selectCssClass = 'select-alert';
// create the alert instance from our built up alertOptions // create the alert instance from our built up selectOptions
overlay = new Alert(this._app, alertOptions); overlay = new Alert(this._app, selectOptions);
if (this._multi) { if (this._multi) {
// use checkboxes // use checkboxes
@ -314,7 +320,7 @@ export class Select implements AfterContentInit, ControlValueAccessor, OnDestroy
} }
// If the user passed a cssClass for the select, add it // If the user passed a cssClass for the select, add it
selectCssClass += alertOptions.cssClass ? ' ' + alertOptions.cssClass : ''; selectCssClass += selectOptions.cssClass ? ' ' + selectOptions.cssClass : '';
overlay.setCssClass(selectCssClass); overlay.setCssClass(selectCssClass);
overlay.addButton({ overlay.addButton({
@ -327,7 +333,7 @@ export class Select implements AfterContentInit, ControlValueAccessor, OnDestroy
} }
overlay.present(alertOptions); overlay.present(selectOptions);
this._isOpen = true; this._isOpen = true;
overlay.onDidDismiss(() => { overlay.onDidDismiss(() => {

View File

@ -11,11 +11,15 @@ export interface Currency {
templateUrl: 'main.html' templateUrl: 'main.html'
}) })
class E2EPage { class E2EPage {
musicAlertOpts: any = { musicSelectOpts: any = {
title: '1994 Music', title: '1994 Music',
subTitle: 'Select your favorite', subTitle: 'Select your favorite',
cssClass: 'music-select' cssClass: 'music-select'
}; };
notificationSelectOpts: any = {
title: 'Mute notifications',
cssClass: 'notification-select'
};
gender: string; gender: string;
gaming: string = ''; gaming: string = '';
os: string = 'win3.1'; os: string = 'win3.1';
@ -24,7 +28,7 @@ class E2EPage {
month: string = '12'; month: string = '12';
year: string = '1994'; year: string = '1994';
notification: string = 'enable'; notification: string = 'enable';
status: string = "checked"; status: string = 'checked';
currencies: Currency[] = [ currencies: Currency[] = [
{ {

View File

@ -44,7 +44,7 @@
<ion-item> <ion-item>
<ion-label>Notifications</ion-label> <ion-label>Notifications</ion-label>
<ion-select [(ngModel)]="notifications" interface="action-sheet" cancelText="Cancel!"> <ion-select [(ngModel)]="notifications" [selectOptions]="notificationSelectOpts" interface="action-sheet" cancelText="Cancel!">
<ion-option value="enable">Enable</ion-option> <ion-option value="enable">Enable</ion-option>
<ion-option value="mute">Mute</ion-option> <ion-option value="mute">Mute</ion-option>
<ion-option value="mute_week">Mute for a week</ion-option> <ion-option value="mute_week">Mute for a week</ion-option>
@ -54,7 +54,7 @@
<ion-item> <ion-item>
<ion-label>Music</ion-label> <ion-label>Music</ion-label>
<ion-select [(ngModel)]="music" [alertOptions]="musicAlertOpts" placeholder="Select Music"> <ion-select [(ngModel)]="music" [selectOptions]="musicSelectOpts" placeholder="Select Music">
<ion-option>Alice in Chains</ion-option> <ion-option>Alice in Chains</ion-option>
<ion-option>Green Day</ion-option> <ion-option>Green Day</ion-option>
<ion-option>Nirvana</ion-option> <ion-option>Nirvana</ion-option>