mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
refactor(all): enable strictPropertyInitialization
This commit is contained in:
@@ -9,49 +9,43 @@ export interface InputBaseComponent {
|
||||
|
||||
clearOnEdit: boolean;
|
||||
didBlurAfterEdit: boolean;
|
||||
styleTmr: number;
|
||||
styleTmr?: number;
|
||||
|
||||
// Shared Attributes
|
||||
autocapitalize: string;
|
||||
autocomplete: string;
|
||||
autofocus: boolean;
|
||||
disabled: boolean;
|
||||
minlength: number;
|
||||
maxlength: number;
|
||||
name: string;
|
||||
placeholder: string;
|
||||
readonly: boolean;
|
||||
required: boolean;
|
||||
spellcheck: boolean;
|
||||
value: string;
|
||||
|
||||
// Shared Methods
|
||||
inputBlurred: (ev: Event) => void;
|
||||
inputChanged: (ev: Event) => void;
|
||||
inputFocused: (ev: Event) => void;
|
||||
inputKeydown: (ev: Event) => void;
|
||||
autocapitalize?: string;
|
||||
autocomplete?: string;
|
||||
autofocus?: boolean;
|
||||
disabled?: boolean;
|
||||
minlength?: number;
|
||||
maxlength?: number;
|
||||
name?: string;
|
||||
placeholder?: string;
|
||||
readonly?: boolean;
|
||||
required?: boolean;
|
||||
spellcheck?: boolean;
|
||||
value?: string;
|
||||
}
|
||||
|
||||
export interface InputComponent extends InputBaseComponent {
|
||||
clearInput: boolean;
|
||||
|
||||
// Input Attributes
|
||||
accept: string;
|
||||
autocorrect: string;
|
||||
min: string;
|
||||
max: string;
|
||||
multiple: boolean;
|
||||
pattern: string;
|
||||
results: number;
|
||||
step: string;
|
||||
size: number;
|
||||
type: string;
|
||||
accept?: string;
|
||||
autocorrect?: string;
|
||||
min?: string;
|
||||
max?: string;
|
||||
multiple?: boolean;
|
||||
pattern?: string;
|
||||
results?: number;
|
||||
step?: string;
|
||||
size?: number;
|
||||
type?: string;
|
||||
}
|
||||
|
||||
export interface TextareaComponent extends InputBaseComponent {
|
||||
|
||||
// Textarea Attributes
|
||||
cols: number;
|
||||
rows: number;
|
||||
wrap: string;
|
||||
cols?: number;
|
||||
rows?: number;
|
||||
wrap?: string;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { Component, Element, Event, EventEmitter, Prop, Watch } from '@stencil/core';
|
||||
|
||||
import { debounceEvent } from '../../utils/helpers';
|
||||
import { debounceEvent, deferEvent } from '../../utils/helpers';
|
||||
import { createThemedClasses } from '../../utils/theme';
|
||||
import { InputComponent } from './input-base';
|
||||
import { Mode } from '../..';
|
||||
|
||||
|
||||
@Component({
|
||||
@@ -17,49 +18,48 @@ import { InputComponent } from './input-base';
|
||||
})
|
||||
export class Input implements InputComponent {
|
||||
|
||||
mode!: Mode;
|
||||
color!: string;
|
||||
|
||||
private nativeInput: HTMLInputElement|undefined;
|
||||
mode: string;
|
||||
color: string;
|
||||
didBlurAfterEdit = false;
|
||||
|
||||
didBlurAfterEdit: boolean;
|
||||
styleTmr: number;
|
||||
|
||||
@Element() private el: HTMLElement;
|
||||
@Element() el!: HTMLElement;
|
||||
|
||||
/**
|
||||
* Emitted when the input value has changed.
|
||||
*/
|
||||
@Event() ionInput: EventEmitter;
|
||||
@Event() ionInput!: EventEmitter;
|
||||
|
||||
/**
|
||||
* Emitted when the styles change.
|
||||
*/
|
||||
@Event() ionStyle: EventEmitter;
|
||||
@Event() ionStyle!: EventEmitter;
|
||||
|
||||
/**
|
||||
* Emitted when the input loses focus.
|
||||
*/
|
||||
@Event() ionBlur: EventEmitter;
|
||||
@Event() ionBlur!: EventEmitter;
|
||||
|
||||
/**
|
||||
* Emitted when the input has focus.
|
||||
*/
|
||||
@Event() ionFocus: EventEmitter;
|
||||
@Event() ionFocus!: EventEmitter;
|
||||
|
||||
/**
|
||||
* Emitted when the input has been created.
|
||||
*/
|
||||
@Event() ionInputDidLoad: EventEmitter;
|
||||
@Event() ionInputDidLoad!: EventEmitter;
|
||||
|
||||
/**
|
||||
* Emitted when the input has been removed.
|
||||
*/
|
||||
@Event() ionInputDidUnload: EventEmitter;
|
||||
@Event() ionInputDidUnload!: EventEmitter;
|
||||
|
||||
/**
|
||||
* If the value of the type attribute is `"file"`, then this attribute will indicate the types of files that the server accepts, otherwise it will be ignored. The value must be a comma-separated list of unique content type specifiers.
|
||||
*/
|
||||
@Prop() accept: string;
|
||||
@Prop() accept?: string;
|
||||
|
||||
/**
|
||||
* Indicates whether and how the text value should be automatically capitalized as it is entered/edited by the user. Defaults to `"none"`.
|
||||
@@ -99,7 +99,7 @@ export class Input implements InputComponent {
|
||||
/**
|
||||
* If true, the value will be cleared after focus upon edit. Defaults to `true` when `type` is `"password"`, `false` for all other types.
|
||||
*/
|
||||
@Prop({ mutable: true }) clearOnEdit: boolean;
|
||||
@Prop({ mutable: true }) clearOnEdit!: boolean;
|
||||
|
||||
/**
|
||||
* Set the amount of time, in milliseconds, to wait to trigger the `ionInput` event after each keystroke. Default `0`.
|
||||
@@ -124,47 +124,47 @@ export class Input implements InputComponent {
|
||||
/**
|
||||
* A hint to the browser for which keyboard to display. This attribute applies when the value of the type attribute is `"text"`, `"password"`, `"email"`, or `"url"`. Possible values are: `"verbatim"`, `"latin"`, `"latin-name"`, `"latin-prose"`, `"full-width-latin"`, `"kana"`, `"katakana"`, `"numeric"`, `"tel"`, `"email"`, `"url"`.
|
||||
*/
|
||||
@Prop() inputmode: string;
|
||||
@Prop() inputmode?: string;
|
||||
|
||||
/**
|
||||
* The maximum value, which must not be less than its minimum (min attribute) value.
|
||||
*/
|
||||
@Prop() max: string;
|
||||
@Prop() max?: string;
|
||||
|
||||
/**
|
||||
* If the value of the type attribute is `text`, `email`, `search`, `password`, `tel`, or `url`, this attribute specifies the maximum number of characters that the user can enter.
|
||||
*/
|
||||
@Prop() maxlength: number;
|
||||
@Prop() maxlength?: number;
|
||||
|
||||
/**
|
||||
* The minimum value, which must not be greater than its maximum (max attribute) value.
|
||||
*/
|
||||
@Prop() min: string;
|
||||
@Prop() min?: string;
|
||||
|
||||
/**
|
||||
* If the value of the type attribute is `text`, `email`, `search`, `password`, `tel`, or `url`, this attribute specifies the minimum number of characters that the user can enter.
|
||||
*/
|
||||
@Prop() minlength: number;
|
||||
@Prop() minlength?: number;
|
||||
|
||||
/**
|
||||
* If true, the user can enter more than one value. This attribute applies when the type attribute is set to `"email"` or `"file"`, otherwise it is ignored.
|
||||
*/
|
||||
@Prop() multiple: boolean;
|
||||
@Prop() multiple?: boolean;
|
||||
|
||||
/**
|
||||
* The name of the control, which is submitted with the form data.
|
||||
*/
|
||||
@Prop() name: string;
|
||||
@Prop() name?: string;
|
||||
|
||||
/**
|
||||
* A regular expression that the value is checked against. The pattern must match the entire value, not just some subset. Use the title attribute to describe the pattern to help the user. This attribute applies when the value of the type attribute is `"text"`, `"search"`, `"tel"`, `"url"`, `"email"`, or `"password"`, otherwise it is ignored.
|
||||
*/
|
||||
@Prop() pattern: string;
|
||||
@Prop() pattern?: string;
|
||||
|
||||
/**
|
||||
* Instructional text that shows before the input has a value.
|
||||
*/
|
||||
@Prop() placeholder: string;
|
||||
@Prop() placeholder?: string;
|
||||
|
||||
/**
|
||||
* If true, the user cannot modify the value. Defaults to `false`.
|
||||
@@ -179,7 +179,7 @@ export class Input implements InputComponent {
|
||||
/**
|
||||
* This is a nonstandard attribute supported by Safari that only applies when the type is `"search"`. Its value should be a nonnegative decimal integer.
|
||||
*/
|
||||
@Prop() results: number;
|
||||
@Prop() results?: number;
|
||||
|
||||
/**
|
||||
* If true, the element will have its spelling and grammar checked. Defaults to `false`.
|
||||
@@ -189,12 +189,12 @@ export class Input implements InputComponent {
|
||||
/**
|
||||
* Works with the min and max attributes to limit the increments at which a value can be set. Possible values are: `"any"` or a positive floating point number.
|
||||
*/
|
||||
@Prop() step: string;
|
||||
@Prop() step?: string;
|
||||
|
||||
/**
|
||||
* The initial size of the control. This value is in pixels unless the value of the type attribute is `"text"` or `"password"`, in which case it is an integer number of characters. This attribute applies only when the `type` attribute is set to `"text"`, `"search"`, `"tel"`, `"url"`, `"email"`, or `"password"`, otherwise it is ignored.
|
||||
*/
|
||||
@Prop() size: number;
|
||||
@Prop() size?: number;
|
||||
|
||||
/**
|
||||
* The type of control to display. The default type is text. Possible values are: `"text"`, `"password"`, `"email"`, `"number"`, `"search"`, `"tel"`, or `"url"`.
|
||||
@@ -204,7 +204,7 @@ export class Input implements InputComponent {
|
||||
/**
|
||||
* The value of the input.
|
||||
*/
|
||||
@Prop({ mutable: true }) value: string;
|
||||
@Prop({ mutable: true }) value = '';
|
||||
|
||||
|
||||
/**
|
||||
@@ -218,14 +218,18 @@ export class Input implements InputComponent {
|
||||
}
|
||||
}
|
||||
|
||||
componentWillLoad() {
|
||||
// By default, password inputs clear after focus when they have content
|
||||
if (this.clearOnEdit === undefined && this.type === 'password') {
|
||||
this.clearOnEdit = true;
|
||||
}
|
||||
}
|
||||
|
||||
componentDidLoad() {
|
||||
this.ionStyle = deferEvent(this.ionStyle);
|
||||
this.debounceChanged();
|
||||
this.emitStyle();
|
||||
|
||||
// By default, password inputs clear after focus when they have content
|
||||
if (this.type === 'password' && this.clearOnEdit !== false) {
|
||||
this.clearOnEdit = true;
|
||||
}
|
||||
this.ionInputDidLoad.emit(this.el);
|
||||
}
|
||||
|
||||
@@ -235,56 +239,50 @@ export class Input implements InputComponent {
|
||||
}
|
||||
|
||||
private emitStyle() {
|
||||
clearTimeout(this.styleTmr);
|
||||
|
||||
const styles = {
|
||||
this.ionStyle.emit({
|
||||
'input': true,
|
||||
'input-checked': this.checked,
|
||||
'input-disabled': this.disabled,
|
||||
'input-has-value': this.hasValue(),
|
||||
'input-has-focus': this.hasFocus()
|
||||
};
|
||||
|
||||
this.styleTmr = setTimeout(() => {
|
||||
this.ionStyle.emit(styles);
|
||||
});
|
||||
}
|
||||
|
||||
inputBlurred(ev: Event) {
|
||||
private inputBlurred(ev: Event) {
|
||||
this.ionBlur.emit(ev);
|
||||
|
||||
this.focusChange(this.hasFocus());
|
||||
this.emitStyle();
|
||||
}
|
||||
|
||||
inputChanged(ev: Event) {
|
||||
private inputChanged(ev: Event) {
|
||||
this.value = ev.target && (ev.target as HTMLInputElement).value || '';
|
||||
this.ionInput.emit(ev);
|
||||
this.emitStyle();
|
||||
}
|
||||
|
||||
inputFocused(ev: Event) {
|
||||
private inputFocused(ev: Event) {
|
||||
this.ionFocus.emit(ev);
|
||||
|
||||
this.focusChange(this.hasFocus());
|
||||
this.emitStyle();
|
||||
}
|
||||
|
||||
focusChange(inputHasFocus: boolean) {
|
||||
private focusChange(inputHasFocus: boolean) {
|
||||
// If clearOnEdit is enabled and the input blurred but has a value, set a flag
|
||||
if (this.clearOnEdit && !inputHasFocus && this.hasValue()) {
|
||||
this.didBlurAfterEdit = true;
|
||||
}
|
||||
}
|
||||
|
||||
inputKeydown(ev: Event) {
|
||||
private inputKeydown(ev: Event) {
|
||||
this.checkClearOnEdit(ev);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we need to clear the text input if clearOnEdit is enabled
|
||||
*/
|
||||
checkClearOnEdit(ev: Event) {
|
||||
private checkClearOnEdit(ev: Event) {
|
||||
if (!this.clearOnEdit) {
|
||||
return;
|
||||
}
|
||||
@@ -299,17 +297,17 @@ export class Input implements InputComponent {
|
||||
this.didBlurAfterEdit = false;
|
||||
}
|
||||
|
||||
clearTextInput(ev: Event) {
|
||||
private clearTextInput(ev: Event) {
|
||||
this.value = '';
|
||||
this.ionInput.emit(ev);
|
||||
}
|
||||
|
||||
hasFocus(): boolean {
|
||||
private hasFocus(): boolean {
|
||||
// check if an input has focus or not
|
||||
return this.nativeInput === document.activeElement;
|
||||
}
|
||||
|
||||
hasValue(): boolean {
|
||||
private hasValue(): boolean {
|
||||
return (this.value !== null && this.value !== undefined && this.value !== '');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user