UNPKG

@ionic/core

Version:
298 lines (297 loc) • 9.21 kB
import { debounceEvent, findItemLabel } from '../../utils/helpers'; import { createColorClasses } from '../../utils/theme'; export class Input { constructor() { this.inputId = `ion-input-${inputIds++}`; this.didBlurAfterEdit = false; this.hasFocus = false; this.autocapitalize = 'off'; this.autocomplete = 'off'; this.autocorrect = 'off'; this.autofocus = false; this.clearInput = false; this.debounce = 0; this.disabled = false; this.name = this.inputId; this.readonly = false; this.required = false; this.spellcheck = false; this.type = 'text'; this.value = ''; this.onInput = (ev) => { const input = ev.target; if (input) { this.value = input.value || ''; } this.ionInput.emit(ev); }; this.onBlur = () => { this.hasFocus = false; this.focusChanged(); this.emitStyle(); this.ionBlur.emit(); }; this.onFocus = () => { this.hasFocus = true; this.focusChanged(); this.emitStyle(); this.ionFocus.emit(); }; this.onKeydown = () => { if (this.clearOnEdit) { if (this.didBlurAfterEdit && this.hasValue()) { this.clearTextInput(); } this.didBlurAfterEdit = false; } }; this.clearTextInput = () => { this.value = ''; }; } debounceChanged() { this.ionChange = debounceEvent(this.ionChange, this.debounce); } disabledChanged() { this.emitStyle(); } valueChanged() { this.emitStyle(); this.ionChange.emit({ value: this.value }); } componentWillLoad() { if (this.clearOnEdit === undefined && this.type === 'password') { this.clearOnEdit = true; } this.emitStyle(); } componentDidLoad() { this.debounceChanged(); this.ionInputDidLoad.emit(); } componentDidUnload() { this.ionInputDidUnload.emit(); } setFocus() { if (this.nativeInput) { this.nativeInput.focus(); } } getInputElement() { return Promise.resolve(this.nativeInput); } getValue() { return this.value || ''; } emitStyle() { this.ionStyle.emit({ 'interactive': true, 'input': true, 'has-placeholder': this.placeholder != null, 'has-value': this.hasValue(), 'has-focus': this.hasFocus, 'interactive-disabled': this.disabled, }); } focusChanged() { if (this.clearOnEdit && !this.hasFocus && this.hasValue()) { this.didBlurAfterEdit = true; } } hasValue() { return this.getValue().length > 0; } hostData() { return { 'aria-disabled': this.disabled ? 'true' : null, class: Object.assign({}, createColorClasses(this.color), { 'has-value': this.hasValue(), 'has-focus': this.hasFocus }) }; } render() { const value = this.getValue(); const labelId = this.inputId + '-lbl'; const label = findItemLabel(this.el); if (label) { label.id = labelId; } return [ h("input", { class: "native-input", ref: input => this.nativeInput = input, "aria-labelledby": labelId, disabled: this.disabled, accept: this.accept, autoCapitalize: this.autocapitalize, autoComplete: this.autocomplete, autoCorrect: this.autocorrect, autoFocus: this.autofocus, inputMode: this.inputmode, min: this.min, max: this.max, minLength: this.minlength, maxLength: this.maxlength, multiple: this.multiple, name: this.name, pattern: this.pattern, placeholder: this.placeholder || '', readOnly: this.readonly, required: this.required, spellCheck: this.spellcheck, step: this.step, size: this.size, type: this.type, value: value, onInput: this.onInput, onBlur: this.onBlur, onFocus: this.onFocus, onKeyDown: this.onKeydown }), (this.clearInput && !this.readonly && !this.disabled) && h("button", { type: "button", class: "input-clear-icon", tabindex: "-1", onTouchStart: this.clearTextInput, onMouseDown: this.clearTextInput }) ]; } static get is() { return "ion-input"; } static get encapsulation() { return "scoped"; } static get properties() { return { "accept": { "type": String, "attr": "accept" }, "autocapitalize": { "type": String, "attr": "autocapitalize" }, "autocomplete": { "type": String, "attr": "autocomplete" }, "autocorrect": { "type": String, "attr": "autocorrect" }, "autofocus": { "type": Boolean, "attr": "autofocus" }, "clearInput": { "type": Boolean, "attr": "clear-input" }, "clearOnEdit": { "type": Boolean, "attr": "clear-on-edit", "mutable": true }, "color": { "type": String, "attr": "color" }, "debounce": { "type": Number, "attr": "debounce", "watchCallbacks": ["debounceChanged"] }, "disabled": { "type": Boolean, "attr": "disabled", "watchCallbacks": ["disabledChanged"] }, "el": { "elementRef": true }, "getInputElement": { "method": true }, "hasFocus": { "state": true }, "inputmode": { "type": String, "attr": "inputmode" }, "max": { "type": String, "attr": "max" }, "maxlength": { "type": Number, "attr": "maxlength" }, "min": { "type": String, "attr": "min" }, "minlength": { "type": Number, "attr": "minlength" }, "mode": { "type": String, "attr": "mode" }, "multiple": { "type": Boolean, "attr": "multiple" }, "name": { "type": String, "attr": "name" }, "pattern": { "type": String, "attr": "pattern" }, "placeholder": { "type": String, "attr": "placeholder" }, "readonly": { "type": Boolean, "attr": "readonly" }, "required": { "type": Boolean, "attr": "required" }, "setFocus": { "method": true }, "size": { "type": Number, "attr": "size" }, "spellcheck": { "type": Boolean, "attr": "spellcheck" }, "step": { "type": String, "attr": "step" }, "type": { "type": String, "attr": "type" }, "value": { "type": String, "attr": "value", "mutable": true, "watchCallbacks": ["valueChanged"] } }; } static get events() { return [{ "name": "ionInput", "method": "ionInput", "bubbles": true, "cancelable": true, "composed": true }, { "name": "ionChange", "method": "ionChange", "bubbles": true, "cancelable": true, "composed": true }, { "name": "ionBlur", "method": "ionBlur", "bubbles": true, "cancelable": true, "composed": true }, { "name": "ionFocus", "method": "ionFocus", "bubbles": true, "cancelable": true, "composed": true }, { "name": "ionInputDidLoad", "method": "ionInputDidLoad", "bubbles": true, "cancelable": true, "composed": true }, { "name": "ionInputDidUnload", "method": "ionInputDidUnload", "bubbles": true, "cancelable": true, "composed": true }, { "name": "ionStyle", "method": "ionStyle", "bubbles": true, "cancelable": true, "composed": true }]; } static get style() { return "/**style-placeholder:ion-input:**/"; } static get styleMode() { return "/**style-id-placeholder:ion-input:**/"; } } let inputIds = 0;