mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-16 10:01:59 +08:00
feat(value-accessors): set ionic classes
This commit is contained in:
@ -1,8 +1,7 @@
|
|||||||
import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core';
|
import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core';
|
||||||
import {
|
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
|
||||||
ControlValueAccessor,
|
|
||||||
NG_VALUE_ACCESSOR
|
import { setIonicClasses } from './util/set-ionic-classes';
|
||||||
} from '@angular/forms';
|
|
||||||
|
|
||||||
@Directive({
|
@Directive({
|
||||||
/* tslint:disable-next-line:directive-selector */
|
/* tslint:disable-next-line:directive-selector */
|
||||||
@ -26,16 +25,23 @@ export class BooleanValueAccessor implements ControlValueAccessor {
|
|||||||
|
|
||||||
writeValue(value: any) {
|
writeValue(value: any) {
|
||||||
this.renderer.setProperty(this.element.nativeElement, 'checked', value);
|
this.renderer.setProperty(this.element.nativeElement, 'checked', value);
|
||||||
|
setIonicClasses(this.element);
|
||||||
}
|
}
|
||||||
|
|
||||||
@HostListener('ionChange', ['$event.target.checked'])
|
@HostListener('ionChange', ['$event.target.checked'])
|
||||||
_handleIonChange(value: any) {
|
_handleIonChange(value: any) {
|
||||||
this.onChange(value);
|
this.onChange(value);
|
||||||
|
setTimeout(() => {
|
||||||
|
setIonicClasses(this.element);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@HostListener('ionBlur')
|
@HostListener('ionBlur')
|
||||||
_handleBlurEvent() {
|
_handleBlurEvent() {
|
||||||
this.onTouched();
|
this.onTouched();
|
||||||
|
setTimeout(() => {
|
||||||
|
setIonicClasses(this.element);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
registerOnChange(fn: (value: any) => void): void {
|
registerOnChange(fn: (value: any) => void): void {
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core';
|
import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core';
|
||||||
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
|
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
|
||||||
|
|
||||||
|
import { setIonicClasses } from './util/set-ionic-classes';
|
||||||
|
|
||||||
@Directive({
|
@Directive({
|
||||||
/* tslint:disable-next-line:directive-selector */
|
/* tslint:disable-next-line:directive-selector */
|
||||||
selector: 'ion-input[type=number]',
|
selector: 'ion-input[type=number]',
|
||||||
@ -30,21 +32,28 @@ export class NumericValueAccessor implements ControlValueAccessor {
|
|||||||
'value',
|
'value',
|
||||||
normalizedValue
|
normalizedValue
|
||||||
);
|
);
|
||||||
|
setIonicClasses(this.element);
|
||||||
}
|
}
|
||||||
|
|
||||||
@HostListener('input', ['$event.target.value'])
|
@HostListener('input', ['$event.target.value'])
|
||||||
_handleInputEvent(value: any): void {
|
_handleInputEvent(value: any): void {
|
||||||
this.onChange(value);
|
this.onChange(value);
|
||||||
|
setTimeout(() => {
|
||||||
|
setIonicClasses(this.element);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@HostListener('ionBlur')
|
@HostListener('ionBlur')
|
||||||
_handleBlurEvent(): void {
|
_handleBlurEvent(): void {
|
||||||
this.onTouched();
|
this.onTouched();
|
||||||
|
setTimeout(() => {
|
||||||
|
setIonicClasses(this.element);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
registerOnChange(fn: (_: number | null) => void): void {
|
registerOnChange(fn: (_: number | null) => void): void {
|
||||||
this.onChange = value => {
|
this.onChange = value => {
|
||||||
fn(value == '' ? null : parseFloat(value));
|
fn(value === '' ? null : parseFloat(value));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,14 +1,7 @@
|
|||||||
import {
|
import { Directive, ElementRef, HostListener, Input, Renderer2 } from '@angular/core';
|
||||||
Directive,
|
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
|
||||||
ElementRef,
|
|
||||||
HostListener,
|
import { setIonicClasses } from './util/set-ionic-classes';
|
||||||
Input,
|
|
||||||
Renderer2
|
|
||||||
} from '@angular/core';
|
|
||||||
import {
|
|
||||||
ControlValueAccessor,
|
|
||||||
NG_VALUE_ACCESSOR
|
|
||||||
} from '@angular/forms';
|
|
||||||
|
|
||||||
@Directive({
|
@Directive({
|
||||||
/* tslint:disable-next-line:directive-selector */
|
/* tslint:disable-next-line:directive-selector */
|
||||||
@ -38,16 +31,23 @@ export class RadioValueAccessor implements ControlValueAccessor {
|
|||||||
'checked',
|
'checked',
|
||||||
value === this.value
|
value === this.value
|
||||||
);
|
);
|
||||||
|
setIonicClasses(this.element);
|
||||||
}
|
}
|
||||||
|
|
||||||
@HostListener('ionSelect', ['$event.target.checked'])
|
@HostListener('ionSelect', ['$event.target.checked'])
|
||||||
_handleIonSelect(value: any) {
|
_handleIonSelect(value: any) {
|
||||||
this.onChange(value);
|
this.onChange(value);
|
||||||
|
setTimeout(() => {
|
||||||
|
setIonicClasses(this.element);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@HostListener('ionBlur')
|
@HostListener('ionBlur')
|
||||||
_handleBlurEvent() {
|
_handleBlurEvent() {
|
||||||
this.onTouched();
|
this.onTouched();
|
||||||
|
setTimeout(() => {
|
||||||
|
setIonicClasses(this.element);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
registerOnChange(fn: (value: any) => void): void {
|
registerOnChange(fn: (value: any) => void): void {
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core';
|
import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core';
|
||||||
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
|
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
|
||||||
|
|
||||||
|
import { setIonicClasses } from './util/set-ionic-classes';
|
||||||
|
|
||||||
// NOTE: May need to look at this to see if we need anything else:
|
// NOTE: May need to look at this to see if we need anything else:
|
||||||
// https://github.com/angular/angular/blob/5.0.2/packages/forms/src/directives/select_control_value_accessor.ts#L28-L158
|
// https://github.com/angular/angular/blob/5.0.2/packages/forms/src/directives/select_control_value_accessor.ts#L28-L158
|
||||||
@Directive({
|
@Directive({
|
||||||
@ -25,16 +27,23 @@ export class SelectValueAccessor implements ControlValueAccessor {
|
|||||||
|
|
||||||
writeValue(value: any) {
|
writeValue(value: any) {
|
||||||
this.renderer.setProperty(this.element.nativeElement, 'value', value);
|
this.renderer.setProperty(this.element.nativeElement, 'value', value);
|
||||||
|
setIonicClasses(this.element);
|
||||||
}
|
}
|
||||||
|
|
||||||
@HostListener('ionChange', ['$event.target.value'])
|
@HostListener('ionChange', ['$event.target.value'])
|
||||||
_handleChangeEvent(value: any) {
|
_handleChangeEvent(value: any) {
|
||||||
this.onChange(value);
|
this.onChange(value);
|
||||||
|
setTimeout(() => {
|
||||||
|
setIonicClasses(this.element);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@HostListener('ionBlur')
|
@HostListener('ionBlur')
|
||||||
_handleBlurEvent() {
|
_handleBlurEvent() {
|
||||||
this.onTouched();
|
this.onTouched();
|
||||||
|
setTimeout(() => {
|
||||||
|
setIonicClasses(this.element);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
registerOnChange(fn: (value: any) => void) {
|
registerOnChange(fn: (value: any) => void) {
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core';
|
import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core';
|
||||||
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
|
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
|
||||||
|
|
||||||
|
import { setIonicClasses } from './util/set-ionic-classes';
|
||||||
|
|
||||||
@Directive({
|
@Directive({
|
||||||
/* tslint:disable-next-line:directive-selector */
|
/* tslint:disable-next-line:directive-selector */
|
||||||
selector: 'ion-input:not([type=number]),ion-textarea,ion-searchbar',
|
selector: 'ion-input:not([type=number]),ion-textarea,ion-searchbar',
|
||||||
@ -23,16 +25,23 @@ export class TextValueAccessor implements ControlValueAccessor {
|
|||||||
|
|
||||||
writeValue(value: any) {
|
writeValue(value: any) {
|
||||||
this.renderer.setProperty(this.element.nativeElement, 'value', value);
|
this.renderer.setProperty(this.element.nativeElement, 'value', value);
|
||||||
|
setIonicClasses(this.element);
|
||||||
}
|
}
|
||||||
|
|
||||||
@HostListener('input', ['$event.target.value'])
|
@HostListener('input', ['$event.target.value'])
|
||||||
_handleInputEvent(value: any) {
|
_handleInputEvent(value: any) {
|
||||||
this.onChange(value);
|
this.onChange(value);
|
||||||
|
setTimeout(() => {
|
||||||
|
setIonicClasses(this.element);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@HostListener('ionBlur')
|
@HostListener('ionBlur')
|
||||||
_handleBlurEvent() {
|
_handleBlurEvent() {
|
||||||
this.onTouched();
|
this.onTouched();
|
||||||
|
setTimeout(() => {
|
||||||
|
setIonicClasses(this.element);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
registerOnChange(fn: (value: any) => void) {
|
registerOnChange(fn: (value: any) => void) {
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
import { ElementRef } from '@angular/core';
|
||||||
|
|
||||||
|
export function setIonicClasses(element: ElementRef) {
|
||||||
|
const classList = element.nativeElement.classList;
|
||||||
|
|
||||||
|
classList.remove('ion-invalid');
|
||||||
|
classList.remove('ion-valid');
|
||||||
|
classList.remove('ion-touched');
|
||||||
|
classList.remove('ion-untouched');
|
||||||
|
classList.remove('ion-dirty');
|
||||||
|
classList.remove('ion-pristine');
|
||||||
|
classList.forEach((cls: string) => {
|
||||||
|
if (cls === 'ng-invalid') { classList.add('ion-invalid'); }
|
||||||
|
if (cls === 'ng-valid') { classList.add('ion-valid'); }
|
||||||
|
if (cls === 'ng-touched') { classList.add('ion-touched'); }
|
||||||
|
if (cls === 'ng-untouched') { classList.add('ion-untouched'); }
|
||||||
|
if (cls === 'ng-dirty') { classList.add('ion-dirty'); }
|
||||||
|
if (cls === 'ng-pristine') { classList.add('ion-pristine'); }
|
||||||
|
});
|
||||||
|
}
|
Reference in New Issue
Block a user