UNPKG

@tarojs/components

Version:
174 lines (170 loc) 5.89 kB
import { r as registerInstance, c as createEvent, h, g as getElement } from './index-5bd7cbab.js'; const indexCss = "taro-input-core{display:block}input{display:block;overflow:hidden;height:1.4rem;text-overflow:clip;text-align:inherit;white-space:nowrap}"; function getTrueType(type, confirmType, password) { if (confirmType === 'search') type = 'search'; if (password) type = 'password'; if (typeof type === 'undefined') { return 'text'; } if (!type) { throw new Error('unexpected type'); } if (type === 'digit') type = 'number'; return type; } function fixControlledValue(value) { return value !== null && value !== void 0 ? value : ''; } let Input = class { constructor(hostRef) { registerInstance(this, hostRef); this.onInput = createEvent(this, "input", 7); this.onPaste = createEvent(this, "paste", 7); this.onFocus = createEvent(this, "focus", 7); this.onBlur = createEvent(this, "blur", 7); this.onConfirm = createEvent(this, "confirm", 7); this.onChange = createEvent(this, "change", 7); this.onKeyDown = createEvent(this, "keydown", 7); this.isOnComposition = false; this.isOnPaste = false; this.onInputExcuted = false; this.password = false; this.disabled = false; this.maxlength = 140; this.autoFocus = false; this.confirmType = 'done'; this.nativeProps = {}; this.handleInput = (e) => { e.stopPropagation(); const { type, maxlength, confirmType, password } = this; if (!this.isOnComposition && !this.onInputExcuted) { let value = e.target.value; const inputType = getTrueType(type, confirmType, password); this.onInputExcuted = true; /* 修复 number 类型 maxlength 无效 */ if (inputType === 'number' && value && maxlength <= value.length) { value = value.substring(0, maxlength); e.target.value = value; } // 修复 IOS 光标跳转问题 // if (!(['number', 'file'].indexOf(inputType) >= 0)) { // const pos = e.target.selectionEnd // setTimeout( // () => { // e.target.selectionStart = pos // e.target.selectionEnd = pos // } // ) // } this.value = value; this.onInput.emit({ value, cursor: value.length }); } }; this.handlePaste = (e) => { this.isOnPaste = true; this.onPaste.emit({ value: e.target.value }); }; this.handleFocus = (e) => { this.onInputExcuted = false; this.onFocus.emit({ value: e.target.value }); }; this.handleBlur = (e) => { this.onBlur.emit({ value: e.target.value }); }; this.handleChange = (e) => { e.stopPropagation(); this.onChange.emit({ value: e.target.value }); if (this.isOnPaste) { this.isOnPaste = false; this.value = e.target.value; this.onInput.emit({ value: e.target.value, cursor: e.target.value.length }); } }; this.handleKeyDown = (e) => { const { value } = e.target; const keyCode = e.keyCode || e.code; this.onInputExcuted = false; e.stopPropagation(); this.onKeyDown.emit({ value, cursor: value.length, keyCode }); keyCode === 13 && this.onConfirm.emit({ value }); }; this.handleComposition = (e) => { if (!(e.target instanceof HTMLInputElement)) return; if (e.type === 'compositionend') { this.isOnComposition = false; this.value = e.target.value; this.onInput.emit({ value: e.target.value, cursor: e.target.value.length }); } else { this.isOnComposition = true; } }; } watchFocus(newValue, oldValue) { var _a; if (!oldValue && newValue) { (_a = this.inputRef) === null || _a === void 0 ? void 0 : _a.focus(); } } componentDidLoad() { var _a, _b, _c; if (this.type === 'file') { this.fileListener = () => { this.onInput.emit(); }; (_a = this.inputRef) === null || _a === void 0 ? void 0 : _a.addEventListener('change', this.fileListener); } else { (_b = this.inputRef) === null || _b === void 0 ? void 0 : _b.addEventListener('compositionstart', this.handleComposition); (_c = this.inputRef) === null || _c === void 0 ? void 0 : _c.addEventListener('compositionend', this.handleComposition); } Object.defineProperty(this.el, 'value', { get: () => { var _a; return (_a = this.inputRef) === null || _a === void 0 ? void 0 : _a.value; }, set: value => (this.value = value), configurable: true }); } disconnectedCallback() { var _a; if (this.type === 'file') { (_a = this.inputRef) === null || _a === void 0 ? void 0 : _a.removeEventListener('change', this.fileListener); } } render() { const { value, type, password, placeholder, autoFocus, disabled, maxlength, confirmType, name, nativeProps } = this; return (h("input", Object.assign({ ref: input => { this.inputRef = input; }, class: 'weui-input', value: fixControlledValue(value), type: getTrueType(type, confirmType, password), placeholder: placeholder, autoFocus: autoFocus, disabled: disabled, maxlength: maxlength, name: name, onInput: this.handleInput, onFocus: this.handleFocus, onBlur: this.handleBlur, onChange: this.handleChange, onKeyDown: this.handleKeyDown, onPaste: this.handlePaste, onCompositionStart: this.handleComposition, onCompositionEnd: this.handleComposition }, nativeProps))); } get el() { return getElement(this); } static get watchers() { return { "autoFocus": ["watchFocus"] }; } }; Input.style = indexCss; export { Input as taro_input_core };