Skip to content

Commit 1c67b02

Browse files
committed
feat(select): fallback to alert interface when more than 6 opts
2 parents 8141a38 + 81096f1 commit 1c67b02

File tree

7 files changed

+108
-35
lines changed

7 files changed

+108
-35
lines changed

ionic/components/action-sheet/action-sheet.ios.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ ion-action-sheet {
8989
}
9090
}
9191

92+
.action-sheet-selected {
93+
font-weight: bold;
94+
background: white;
95+
}
96+
9297
.action-sheet-destructive {
9398
color: $action-sheet-ios-button-destructive-text-color;
9499
}

ionic/components/action-sheet/action-sheet.md.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,7 @@ $action-sheet-md-icon-margin: 0 28px 0 0 !default;
7171
margin-bottom: $action-sheet-md-group-margin-bottom;
7272
}
7373
}
74+
75+
.action-sheet-selected {
76+
font-weight: bold;
77+
}

ionic/components/action-sheet/action-sheet.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ class ActionSheetCmp {
244244
} else {
245245
if (button.role === 'destructive') {
246246
button.cssClass = (button.cssClass + ' ' || '') + 'action-sheet-destructive';
247+
} else if (button.role === 'selected') {
248+
button.cssClass = (button.cssClass + ' ' || '') + 'action-sheet-selected';
247249
}
248250
buttons.push(button);
249251
}

ionic/components/action-sheet/action-sheet.wp.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ $action-sheet-wp-icon-margin: 0 16px 0 0 !default;
7575
}
7676
}
7777

78+
.action-sheet-selected {
79+
font-weight: bold;
80+
}
81+
7882
.action-sheet-cancel {
7983
background: $action-sheet-wp-button-background;
8084
}

ionic/components/select/select.ts

Lines changed: 69 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {Component, Optional, ElementRef, Renderer, Input, Output, Provider, forw
22
import {NG_VALUE_ACCESSOR} from 'angular2/common';
33

44
import {Alert} from '../alert/alert';
5+
import {ActionSheet} from '../action-sheet/action-sheet';
56
import {Form} from '../../util/form';
67
import {Item} from '../item/item';
78
import {merge, isTrueProperty, isBlank, isCheckedProperty} from '../../util/util';
@@ -157,6 +158,11 @@ export class Select {
157158
*/
158159
@Input() checked: any = false;
159160

161+
/**
162+
* @private
163+
*/
164+
@Input() interface: string = '';
165+
160166
/**
161167
* @output {any} Any expression you want to evaluate when the selection has changed
162168
*/
@@ -206,7 +212,10 @@ export class Select {
206212
}
207213

208214
private _open() {
209-
if (this._disabled) return;
215+
if (this._disabled) {
216+
return;
217+
}
218+
210219
console.debug('select, open alert');
211220

212221
// the user may have assigned some options specifically for the alert
@@ -216,6 +225,7 @@ export class Select {
216225
// and we create a new array for the alert's two buttons
217226
alertOptions.buttons = [{
218227
text: this.cancelText,
228+
role: 'cancel',
219229
handler: () => {
220230
this.cancel.emit(null);
221231
}
@@ -226,41 +236,72 @@ export class Select {
226236
alertOptions.title = this._item.getLabelText();
227237
}
228238

229-
// user cannot provide inputs from alertOptions
230-
// alert inputs must be created by ionic from ion-options
231-
alertOptions.inputs = this._options.toArray().map(input => {
232-
return {
233-
type: (this._multi ? 'checkbox' : 'radio'),
234-
label: input.text,
235-
value: input.value,
236-
checked: input.checked
237-
};
238-
});
239+
let options = this._options.toArray();
240+
if (this.interface === 'action-sheet' && options.length > 6) {
241+
this.interface = null;
242+
}
239243

240-
// create the alert instance from our built up alertOptions
241-
let alert = Alert.create(alertOptions);
244+
let overlay;
245+
if (this.interface === 'action-sheet') {
246+
if (this._multi) {
247+
throw new Error('action-sheet interface cannot use multivalue selector');
248+
}
249+
250+
alertOptions.buttons = alertOptions.buttons.concat(options.map(input => {
251+
return {
252+
role: (input.checked ? 'selected' : ''),
253+
text: input.text,
254+
handler: () => {
255+
this.onChange(input.value);
256+
this.change.emit(input.value);
257+
}
258+
}
259+
}));
260+
alertOptions.cssClass = 'select-action-sheet';
242261

243-
if (this._multi) {
244-
// use checkboxes
245-
alert.setCssClass('select-alert multiple-select-alert');
262+
overlay = ActionSheet.create(alertOptions);
246263

247264
} else {
248-
// use radio buttons
249-
alert.setCssClass('select-alert single-select-alert');
250-
}
265+
// default to use the alert interface
266+
this.interface = 'alert';
267+
268+
// user cannot provide inputs from alertOptions
269+
// alert inputs must be created by ionic from ion-options
270+
alertOptions.inputs = this._options.toArray().map(input => {
271+
return {
272+
type: (this._multi ? 'checkbox' : 'radio'),
273+
label: input.text,
274+
value: input.value,
275+
checked: input.checked
276+
}
277+
});
251278

252-
alert.addButton({
253-
text: this.okText,
254-
handler: selectedValues => {
255-
this.onChange(selectedValues);
256-
this.change.emit(selectedValues);
279+
// create the alert instance from our built up alertOptions
280+
overlay = Alert.create(alertOptions);
281+
282+
if (this._multi) {
283+
// use checkboxes
284+
overlay.setCssClass('select-alert multiple-select-alert');
285+
286+
} else {
287+
// use radio buttons
288+
overlay.setCssClass('select-alert single-select-alert');
257289
}
258-
});
259290

260-
this._nav.present(alert, alertOptions);
291+
overlay.addButton({
292+
text: this.okText,
293+
handler: selectedValues => {
294+
this.onChange(selectedValues);
295+
this.change.emit(selectedValues);
296+
}
297+
});
298+
299+
}
300+
301+
this._nav.present(overlay, alertOptions);
261302

262303
this._isOpen = true;
263-
alert.onDismiss(() => {
304+
overlay.onDismiss(() => {
264305
this._isOpen = false;
265306
});
266307
}
@@ -386,7 +427,7 @@ export class Select {
386427
/**
387428
* @private
388429
*/
389-
onTouched() {}
430+
onTouched() { }
390431

391432
/**
392433
* @private

ionic/components/select/test/single-value/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ class E2EPage {
1616
month: string;
1717
year: string;
1818
years: Array<number>;
19+
notification: string;
1920

2021
constructor() {
2122
this.gaming = '';
2223
this.os = 'win3.1';
2324
this.music = null;
2425
this.month = '12';
2526
this.year = '1994';
27+
this.notification = 'enable';
2628

2729
this.years = [1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999];
2830

ionic/components/select/test/single-value/main.html

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
<ion-item>
2828
<ion-label>Operating System</ion-label>
29-
<ion-select [(ngModel)]="os" submitText="Okay" cancelText="Nah">
29+
<ion-select [(ngModel)]="os" interface="alert" submitText="Okay" cancelText="Nah">
3030
<ion-option value="dos">DOS</ion-option>
3131
<ion-option value="lunix">Linux</ion-option>
3232
<ion-option value="mac7">Mac OS 7</ion-option>
@@ -37,6 +37,16 @@
3737
</ion-select>
3838
</ion-item>
3939

40+
<ion-item>
41+
<ion-label>Notifications</ion-label>
42+
<ion-select [(ngModel)]="notification" interface="action-sheet" cancelText="Cancel!">
43+
<ion-option value="enable">Enable</ion-option>
44+
<ion-option value="mute">Mute</ion-option>
45+
<ion-option value="mute_week">Mute for a week</ion-option>
46+
<ion-option value="mute_year">Mute for a year</ion-option>
47+
</ion-select>
48+
</ion-item>
49+
4050
<ion-item>
4151
<ion-label>Music</ion-label>
4252
<ion-select [(ngModel)]="music" [alertOptions]="musicAlertOpts">
@@ -74,11 +84,16 @@
7484
<button (click)="resetGender()">Reset Gender</button>
7585

7686
<p aria-hidden="true" padding>
77-
<code>gender: {{gender}}</code><br>
78-
<code>gaming: {{gaming}}</code><br>
79-
<code>os: {{os}}</code><br>
80-
<code>music: {{music}}</code><br>
81-
<code>date: {{month}}/{{year}}</code><br>
87+
<code>gender: {{gender}}</code>
88+
<br>
89+
<code>gaming: {{gaming}}</code>
90+
<br>
91+
<code>os: {{os}}</code>
92+
<br>
93+
<code>music: {{music}}</code>
94+
<br>
95+
<code>date: {{month}}/{{year}}</code>
96+
<br>
8297
</p>
8398

84-
</ion-content>
99+
</ion-content>

0 commit comments

Comments
 (0)