UNPKG

@ionic/core

Version:
242 lines (241 loc) • 7.22 kB
import { debounceEvent, findItemLabel } from '../../utils/helpers'; import { createColorClasses } from '../../utils/theme'; export class Textarea { constructor() { this.inputId = `ion-input-${textareaIds++}`; this.didBlurAfterEdit = false; this.hasFocus = false; this.autocapitalize = 'none'; this.autofocus = false; this.clearOnEdit = false; this.debounce = 0; this.disabled = false; this.name = this.inputId; this.readonly = false; this.required = false; this.spellcheck = false; this.value = ''; this.onInput = (ev) => { if (this.nativeInput) { this.value = this.nativeInput.value; } this.emitStyle(); this.ionInput.emit(ev); }; this.onFocus = () => { this.hasFocus = true; this.focusChange(); this.ionFocus.emit(); }; this.onBlur = () => { this.hasFocus = false; this.focusChange(); this.ionBlur.emit(); }; this.onKeyDown = () => { this.checkClearOnEdit(); }; } debounceChanged() { this.ionChange = debounceEvent(this.ionChange, this.debounce); } disabledChanged() { this.emitStyle(); } valueChanged() { const nativeInput = this.nativeInput; const value = this.getValue(); if (nativeInput && nativeInput.value !== value) { nativeInput.value = value; } this.ionChange.emit({ value }); } componentWillLoad() { this.emitStyle(); } componentDidLoad() { this.debounceChanged(); } setFocus() { if (this.nativeInput) { this.nativeInput.focus(); } } getInputElement() { return Promise.resolve(this.nativeInput); } emitStyle() { this.ionStyle.emit({ 'interactive': true, 'textarea': true, 'input': true, 'interactive-disabled': this.disabled, 'has-placeholder': this.placeholder != null, 'has-value': this.hasValue(), 'has-focus': this.hasFocus }); } checkClearOnEdit() { if (!this.clearOnEdit) { return; } if (this.didBlurAfterEdit && this.hasValue()) { this.value = ''; } this.didBlurAfterEdit = false; } focusChange() { if (this.clearOnEdit && !this.hasFocus && this.hasValue()) { this.didBlurAfterEdit = true; } this.emitStyle(); } hasValue() { return this.getValue() !== ''; } getValue() { return this.value || ''; } hostData() { return { 'aria-disabled': this.disabled ? 'true' : null, class: createColorClasses(this.color) }; } render() { const value = this.getValue(); const labelId = this.inputId + '-lbl'; const label = findItemLabel(this.el); if (label) { label.id = labelId; } return (h("textarea", { class: "native-textarea", ref: el => this.nativeInput = el, autoCapitalize: this.autocapitalize, autoFocus: this.autofocus, disabled: this.disabled, maxLength: this.maxlength, minLength: this.minlength, name: this.name, placeholder: this.placeholder || '', readOnly: this.readonly, required: this.required, spellCheck: this.spellcheck, cols: this.cols, rows: this.rows, wrap: this.wrap, onInput: this.onInput, onBlur: this.onBlur, onFocus: this.onFocus, onKeyDown: this.onKeyDown }, value)); } static get is() { return "ion-textarea"; } static get encapsulation() { return "scoped"; } static get properties() { return { "autocapitalize": { "type": String, "attr": "autocapitalize" }, "autofocus": { "type": Boolean, "attr": "autofocus" }, "clearOnEdit": { "type": Boolean, "attr": "clear-on-edit", "mutable": true }, "color": { "type": String, "attr": "color" }, "cols": { "type": Number, "attr": "cols" }, "debounce": { "type": Number, "attr": "debounce", "watchCallbacks": ["debounceChanged"] }, "disabled": { "type": Boolean, "attr": "disabled", "watchCallbacks": ["disabledChanged"] }, "el": { "elementRef": true }, "getInputElement": { "method": true }, "hasFocus": { "state": true }, "maxlength": { "type": Number, "attr": "maxlength" }, "minlength": { "type": Number, "attr": "minlength" }, "mode": { "type": String, "attr": "mode" }, "name": { "type": String, "attr": "name" }, "placeholder": { "type": String, "attr": "placeholder" }, "readonly": { "type": Boolean, "attr": "readonly" }, "required": { "type": Boolean, "attr": "required" }, "rows": { "type": Number, "attr": "rows" }, "setFocus": { "method": true }, "spellcheck": { "type": Boolean, "attr": "spellcheck" }, "value": { "type": String, "attr": "value", "mutable": true, "watchCallbacks": ["valueChanged"] }, "wrap": { "type": String, "attr": "wrap" } }; } static get events() { return [{ "name": "ionChange", "method": "ionChange", "bubbles": true, "cancelable": true, "composed": true }, { "name": "ionInput", "method": "ionInput", "bubbles": true, "cancelable": true, "composed": true }, { "name": "ionStyle", "method": "ionStyle", "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 }]; } static get style() { return "/**style-placeholder:ion-textarea:**/"; } static get styleMode() { return "/**style-id-placeholder:ion-textarea:**/"; } } let textareaIds = 0;