Skip to content

Commit 81096f1

Browse files
committed
feat(select): using action-sheet as ion-select interface
1 parent b3bea83 commit 81096f1

File tree

7 files changed

+102
-35
lines changed

7 files changed

+102
-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
@@ -82,6 +82,11 @@ ion-action-sheet {
8282
}
8383
}
8484

85+
.action-sheet-selected {
86+
font-weight: bold;
87+
background: white;
88+
}
89+
8590
.action-sheet-destructive {
8691
color: $action-sheet-ios-button-destructive-text-color;
8792
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ $action-sheet-md-icon-margin: 0 28px 0 0 !default;
6464
margin-bottom: $action-sheet-md-group-margin-bottom;
6565
}
6666
}
67+
68+
.action-sheet-selected {
69+
font-weight: bold;
70+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ class ActionSheetCmp {
240240
} else {
241241
if (button.role === 'destructive') {
242242
button.cssClass = (button.cssClass + ' ' || '') + 'action-sheet-destructive';
243+
} else if (button.role === 'selected') {
244+
button.cssClass = (button.cssClass + ' ' || '') + 'action-sheet-selected';
243245
}
244246
buttons.push(button);
245247
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ $action-sheet-wp-icon-margin: 0 16px 0 0 !default;
6969
}
7070
}
7171

72+
.action-sheet-selected {
73+
font-weight: bold;
74+
}
75+
7276
.action-sheet-cancel {
7377
background: $action-sheet-wp-button-background;
7478
}

ionic/components/select/select.ts

Lines changed: 63 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';
@@ -156,6 +157,11 @@ export class Select {
156157
*/
157158
@Input() checked: any = false;
158159

160+
/**
161+
* @private
162+
*/
163+
@Input() interface: string = '';
164+
159165
/**
160166
* @output {any} Any expression you want to evaluate when the selection has changed
161167
*/
@@ -205,7 +211,9 @@ export class Select {
205211
}
206212

207213
private _open() {
208-
if (this._disabled) return;
214+
if (this._disabled)
215+
return;
216+
209217
console.debug('select, open alert');
210218

211219
// the user may have assigned some options specifically for the alert
@@ -215,6 +223,7 @@ export class Select {
215223
// and we create a new array for the alert's two buttons
216224
alertOptions.buttons = [{
217225
text: this.cancelText,
226+
role: 'cancel',
218227
handler: () => {
219228
this.cancel.emit(null);
220229
}
@@ -225,41 +234,67 @@ export class Select {
225234
alertOptions.title = this._item.getLabelText();
226235
}
227236

228-
// user cannot provide inputs from alertOptions
229-
// alert inputs must be created by ionic from ion-options
230-
alertOptions.inputs = this._options.toArray().map(input => {
231-
return {
232-
type: (this._multi ? 'checkbox' : 'radio'),
233-
label: input.text,
234-
value: input.value,
235-
checked: input.checked
237+
let modal;
238+
if (this.interface === 'action-sheet') {
239+
if (this._multi) {
240+
throw new Error('multivalue selector can not be have action-sheet interface');
236241
}
237-
});
242+
let options = this._options.toArray();
243+
if (options.length > 6) {
244+
throw new Error('action-sheet interface MUST NOT have more than 6 options');
245+
}
246+
alertOptions.buttons = alertOptions.buttons.concat(options.map(input => {
247+
return {
248+
role: (input.checked ? 'selected' : ''),
249+
text: input.text,
250+
handler: () => {
251+
this.onChange(input.value);
252+
this.change.emit(input.value);
253+
}
254+
}
255+
}));
256+
257+
modal = ActionSheet.create(alertOptions);
258+
259+
} else if (this.interface === '' || this.interface === 'alert') {
260+
// user cannot provide inputs from alertOptions
261+
// alert inputs must be created by ionic from ion-options
262+
alertOptions.inputs = this._options.toArray().map(input => {
263+
return {
264+
type: (this._multi ? 'checkbox' : 'radio'),
265+
label: input.text,
266+
value: input.value,
267+
checked: input.checked
268+
}
269+
});
238270

239-
// create the alert instance from our built up alertOptions
240-
let alert = Alert.create(alertOptions);
271+
// create the alert instance from our built up alertOptions
272+
modal = Alert.create(alertOptions);
241273

242-
if (this._multi) {
243-
// use checkboxes
244-
alert.setCssClass('select-alert multiple-select-alert');
274+
if (this._multi) {
275+
// use checkboxes
276+
modal.setCssClass('select-alert multiple-select-alert');
245277

278+
} else {
279+
// use radio buttons
280+
modal.setCssClass('select-alert single-select-alert');
281+
}
282+
283+
modal.addButton({
284+
text: this.okText,
285+
handler: selectedValues => {
286+
this.onChange(selectedValues);
287+
this.change.emit(selectedValues);
288+
}
289+
});
246290
} else {
247-
// use radio buttons
248-
alert.setCssClass('select-alert single-select-alert');
291+
throw new Error('unknown interface value: ' + this.interface);
249292
}
250293

251-
alert.addButton({
252-
text: this.okText,
253-
handler: selectedValues => {
254-
this.onChange(selectedValues);
255-
this.change.emit(selectedValues);
256-
}
257-
});
258-
259-
this._nav.present(alert, alertOptions);
294+
this._nav.present(modal, alertOptions);
260295

261296
this._isOpen = true;
262-
alert.onDismiss(() => {
297+
modal.onDismiss(() => {
263298
this._isOpen = false;
264299
});
265300
}
@@ -385,7 +420,7 @@ export class Select {
385420
/**
386421
* @private
387422
*/
388-
onTouched() {}
423+
onTouched() { }
389424

390425
/**
391426
* @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)