fix(select): cssClass + strong typed

This commit is contained in:
Manu Mtz.-Almeida
2018-04-27 18:25:03 +02:00
parent c0ec02e534
commit 826e02bcf0
8 changed files with 54 additions and 55 deletions

View File

@ -58,6 +58,7 @@ import {
RouterOutletOptions, RouterOutletOptions,
RouteWrite, RouteWrite,
SelectInputChangeEvent, SelectInputChangeEvent,
SelectInterface,
SelectPopoverOption, SelectPopoverOption,
StyleEvent, StyleEvent,
ToastOptions, ToastOptions,
@ -5674,7 +5675,7 @@ declare global {
/** /**
* The interface the select should use: `action-sheet`, `popover` or `alert`. Default: `alert`. * The interface the select should use: `action-sheet`, `popover` or `alert`. Default: `alert`.
*/ */
'interface': string; 'interface': SelectInterface;
/** /**
* Any additional options that the `alert`, `action-sheet` or `popover` interface can take. See the [AlertController API docs](../../alert/AlertController/#create), the [ActionSheetController API docs](../../action-sheet/ActionSheetController/#create) and the [PopoverController API docs](../../popover/PopoverController/#create) for the create options for each interface. * Any additional options that the `alert`, `action-sheet` or `popover` interface can take. See the [AlertController API docs](../../alert/AlertController/#create), the [ActionSheetController API docs](../../action-sheet/ActionSheetController/#create) and the [PopoverController API docs](../../popover/PopoverController/#create) for the create options for each interface.
*/ */
@ -5736,7 +5737,7 @@ declare global {
/** /**
* The interface the select should use: `action-sheet`, `popover` or `alert`. Default: `alert`. * The interface the select should use: `action-sheet`, `popover` or `alert`. Default: `alert`.
*/ */
'interface'?: string; 'interface'?: SelectInterface;
/** /**
* Any additional options that the `alert`, `action-sheet` or `popover` interface can take. See the [AlertController API docs](../../alert/AlertController/#create), the [ActionSheetController API docs](../../action-sheet/ActionSheetController/#create) and the [PopoverController API docs](../../popover/PopoverController/#create) for the create options for each interface. * Any additional options that the `alert`, `action-sheet` or `popover` interface can take. See the [AlertController API docs](../../alert/AlertController/#create), the [ActionSheetController API docs](../../action-sheet/ActionSheetController/#create) and the [PopoverController API docs](../../popover/PopoverController/#create) for the create options for each interface.
*/ */

View File

@ -2,7 +2,7 @@
export interface ActionSheetOptions { export interface ActionSheetOptions {
header?: string; header?: string;
subHeader?: string; subHeader?: string;
cssClass?: string; cssClass?: string | string[];
buttons: (ActionSheetButton | string)[]; buttons: (ActionSheetButton | string)[];
enableBackdropDismiss?: boolean; enableBackdropDismiss?: boolean;
translucent?: boolean; translucent?: boolean;
@ -12,7 +12,7 @@ export interface ActionSheetButton {
text?: string; text?: string;
role?: 'cancel' | 'destructive' | 'selected' | string; role?: 'cancel' | 'destructive' | 'selected' | string;
icon?: string; icon?: string;
cssClass?: string; cssClass?: string | string[];
handler?: () => boolean | void; handler?: () => boolean | void;
} }

View File

@ -10,7 +10,6 @@ export interface InputBaseComponent {
clearOnEdit: boolean; clearOnEdit: boolean;
didBlurAfterEdit: boolean; didBlurAfterEdit: boolean;
styleTmr?: number;
// Shared Attributes // Shared Attributes
autocapitalize?: string; autocapitalize?: string;

View File

@ -0,0 +1 @@
export type SelectInterface = 'action-sheet' | 'popover' | 'alert';

View File

@ -1,7 +1,8 @@
import { Component, Element, Event, EventEmitter, Listen, Prop, State, Watch } from '@stencil/core'; import { Component, Element, Event, EventEmitter, Listen, Prop, State, Watch } from '@stencil/core';
import { ActionSheetButton, ActionSheetOptions, AlertOptions, CssClassMap, import { ActionSheetButton, ActionSheetOptions, AlertInput, AlertOptions,
Mode, PopoverOptions, SelectInputChangeEvent, SelectPopoverOption, StyleEvent CssClassMap, Mode, PopoverOptions, SelectInputChangeEvent, SelectInterface, SelectPopoverOption, StyleEvent
} from '../../interface'; } from '../../interface';
import { deferEvent } from '../../utils/helpers';
@Component({ @Component({
@ -20,7 +21,7 @@ export class Select {
private selectId = `ion-sel-${selectIds++}`; private selectId = `ion-sel-${selectIds++}`;
private labelId?: string; private labelId?: string;
private overlay?: HTMLIonActionSheetElement | HTMLIonAlertElement | HTMLIonPopoverElement; private overlay?: HTMLIonActionSheetElement | HTMLIonAlertElement | HTMLIonPopoverElement;
private styleTmr: any;
mode!: Mode; mode!: Mode;
@Element() el!: HTMLIonSelectElement; @Element() el!: HTMLIonSelectElement;
@ -71,7 +72,7 @@ export class Select {
/** /**
* The interface the select should use: `action-sheet`, `popover` or `alert`. Default: `alert`. * The interface the select should use: `action-sheet`, `popover` or `alert`. Default: `alert`.
*/ */
@Prop() interface = 'alert'; @Prop() interface: SelectInterface = 'alert';
/** /**
* Any additional options that the `alert`, `action-sheet` or `popover` interface * Any additional options that the `alert`, `action-sheet` or `popover` interface
@ -231,6 +232,8 @@ export class Select {
} }
componentDidLoad() { componentDidLoad() {
this.ionStyle = deferEvent(this.ionStyle);
const label = this.getLabel(); const label = this.getLabel();
if (label) { if (label) {
this.labelId = label.id = this.name + '-lbl'; this.labelId = label.id = this.name + '-lbl';
@ -292,9 +295,11 @@ export class Select {
} }
private async openPopover(ev: UIEvent) { private async openPopover(ev: UIEvent) {
const interfaceOptions = {...this.interfaceOptions}; const interfaceOptions = this.interfaceOptions;
const popoverOpts: PopoverOptions = {
...interfaceOptions,
const popoverOpts: PopoverOptions = Object.assign(interfaceOptions, {
component: 'ion-select-popover', component: 'ion-select-popover',
componentProps: { componentProps: {
header: interfaceOptions.header, header: interfaceOptions.header,
@ -314,9 +319,9 @@ export class Select {
} as SelectPopoverOption; } as SelectPopoverOption;
}) })
}, },
cssClass: 'select-popover' + (interfaceOptions.cssClass ? ' ' + interfaceOptions.cssClass : ''), cssClass: ['select-popover', interfaceOptions.cssClass],
ev: ev ev: ev
}); };
const popover = this.overlay = await this.popoverCtrl.create(popoverOpts); const popover = this.overlay = await this.popoverCtrl.create(popoverOpts);
await popover.present(); await popover.present();
@ -325,8 +330,6 @@ export class Select {
} }
private async openActionSheet() { private async openActionSheet() {
const interfaceOptions = {...this.interfaceOptions};
const actionSheetButtons = this.childOpts.map(option => { const actionSheetButtons = this.childOpts.map(option => {
return { return {
role: (option.selected ? 'selected' : ''), role: (option.selected ? 'selected' : ''),
@ -345,10 +348,13 @@ export class Select {
} }
}); });
const actionSheetOpts: ActionSheetOptions = Object.assign(interfaceOptions, { const interfaceOptions = this.interfaceOptions;
const actionSheetOpts: ActionSheetOptions = {
...interfaceOptions,
buttons: actionSheetButtons, buttons: actionSheetButtons,
cssClass: 'select-action-sheet' + (interfaceOptions.cssClass ? ' ' + interfaceOptions.cssClass : '') cssClass: ['select-action-sheet', interfaceOptions.cssClass]
}); };
const actionSheet = this.overlay = await this.actionSheetCtrl.create(actionSheetOpts); const actionSheet = this.overlay = await this.actionSheetCtrl.create(actionSheetOpts);
await actionSheet.present(); await actionSheet.present();
@ -358,12 +364,14 @@ export class Select {
} }
private async openAlert() { private async openAlert() {
const interfaceOptions = {...this.interfaceOptions};
const label = this.getLabel(); const label = this.getLabel();
const labelText = (label) ? label.textContent : null; const labelText = (label) ? label.textContent : null;
const alertOpts: AlertOptions = Object.assign(interfaceOptions, { const interfaceOptions = this.interfaceOptions;
const alertOpts: AlertOptions = {
...interfaceOptions,
header: interfaceOptions.header ? interfaceOptions.header : labelText, header: interfaceOptions.header ? interfaceOptions.header : labelText,
inputs: this.childOpts.map(o => { inputs: this.childOpts.map(o => {
return { return {
@ -372,27 +380,23 @@ export class Select {
value: o.value, value: o.value,
checked: o.selected, checked: o.selected,
disabled: o.disabled disabled: o.disabled
}; } as AlertInput;
}), }),
buttons: [ buttons: [{
{
text: this.cancelText, text: this.cancelText,
role: 'cancel', role: 'cancel',
handler: () => { handler: () => {
this.ionCancel.emit(); this.ionCancel.emit();
} }
}, }, {
{
text: this.okText, text: this.okText,
handler: (selectedValues: any) => { handler: (selectedValues: any) => {
this.value = selectedValues; this.value = selectedValues;
} }
} }],
], cssClass: ['select-alert', interfaceOptions.cssClass,
cssClass: 'select-alert ' + (this.multiple ? 'multiple-select-alert' : 'single-select-alert')]
(this.multiple ? 'multiple-select-alert' : 'single-select-alert') + };
(interfaceOptions.cssClass ? ' ' + interfaceOptions.cssClass : '')
});
const alert = this.overlay = await this.alertCtrl.create(alertOpts); const alert = this.overlay = await this.alertCtrl.create(alertOpts);
await alert.present(); await alert.present();
@ -412,7 +416,6 @@ export class Select {
const overlay = this.overlay; const overlay = this.overlay;
this.overlay = undefined; this.overlay = undefined;
this.isExpanded = false; this.isExpanded = false;
return overlay.dismiss(); return overlay.dismiss();
@ -439,15 +442,11 @@ export class Select {
} }
private emitStyle() { private emitStyle() {
clearTimeout(this.styleTmr);
this.styleTmr = setTimeout(() => {
this.ionStyle.emit({ this.ionStyle.emit({
'select': true, 'select': true,
'select-disabled': this.disabled, 'select-disabled': this.disabled,
'input-has-value': this.hasValue() 'input-has-value': this.hasValue()
}); });
});
} }
hostData() { hostData() {

View File

@ -22,7 +22,6 @@ export class Textarea implements TextareaComponent {
color!: string; color!: string;
didBlurAfterEdit = false; didBlurAfterEdit = false;
styleTmr?: number;
@Element() el!: HTMLElement; @Element() el!: HTMLElement;

View File

@ -12,6 +12,7 @@ export * from './components/loading/loading-interface';
export * from './components/popover/popover-interface'; export * from './components/popover/popover-interface';
export * from './components/nav/nav-interface'; export * from './components/nav/nav-interface';
export * from './components/router/utils/interface'; export * from './components/router/utils/interface';
export * from './components/select/select-interface';
export * from './components/select-popover/select-popover-interface'; export * from './components/select-popover/select-popover-interface';
export * from './components/toast/toast-interface'; export * from './components/toast/toast-interface';

View File

@ -52,12 +52,11 @@ export function getButtonClassMap(buttonType: string, mode: Mode): CssClassMap {
export function getClassList(classes: string | string[] | undefined): string[] { export function getClassList(classes: string | string[] | undefined): string[] {
if (classes) { if (classes) {
if (Array.isArray(classes)) { const array = Array.isArray(classes) ? classes : classes.split(' ');
return classes; return array
} .filter(c => c != null)
return classes .map(c => c.trim())
.split(' ') .filter(c => c !== '');
.filter(c => c.trim() !== '');
} }
return []; return [];
} }