UNPKG

@ohu-mobile/core

Version:
244 lines (243 loc) 11.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _EyeCloseOutlined2 = _interopRequireDefault(require("@ohu-mobile/icons/lib/EyeCloseOutlined")); var _EyeOutlined2 = _interopRequireDefault(require("@ohu-mobile/icons/lib/EyeOutlined")); var _CloseCircleFilled2 = _interopRequireDefault(require("@ohu-mobile/icons/lib/CloseCircleFilled")); var _babelHelperVueJsxMergeProps = _interopRequireDefault(require("@vue/babel-helper-vue-jsx-merge-props")); var _defineComponent = require("../_utils/defineComponent"); var _directive = _interopRequireDefault(require("./directive")); var _iconUtils = require("../_utils/icon-utils"); var _Button = _interopRequireDefault(require("../Button")); var _fieldMixin = require("../Form/fieldMixin"); var _excluded = ["type", "value", "outline", "allowClear", "allowTogglePassword", "startAdornment", "endAdornment", "noBorder", "autofocus", "name"]; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var Input = (0, _defineComponent.defineComponent)('input').mixin((0, _fieldMixin.fieldMixin)('stateValue', 'value')).create({ model: { prop: 'value', event: 'valueChange' }, props: { name: (0, _defineComponent.props)(String).optional, type: (0, _defineComponent.props)(String).default('text'), outline: (0, _defineComponent.props)(Boolean).default(false), value: _defineComponent.props.ofAny().optional, allowClear: (0, _defineComponent.props)(Boolean).default(false), allowTogglePassword: (0, _defineComponent.props)(Boolean).default(true), noBorder: (0, _defineComponent.props)(Boolean).default(false), disabled: (0, _defineComponent.props)(Boolean).default(false), readonly: (0, _defineComponent.props)(Boolean).default(false), placeholder: (0, _defineComponent.props)(String).default(''), startAdornment: (0, _defineComponent.props)(Object, Object, String).optional, endAdornment: (0, _defineComponent.props)(Object, Object, String).optional, max: (0, _defineComponent.props)(String, Number).optional, min: (0, _defineComponent.props)(String, Number).optional, maxlength: (0, _defineComponent.props)(String, Number).optional, minlength: (0, _defineComponent.props)(String, Number).optional, multiple: (0, _defineComponent.props)(Boolean).optional, tabindex: (0, _defineComponent.props)(String, Number).optional, step: (0, _defineComponent.props)(String, Number).optional, size: (0, _defineComponent.props)(String, Number).optional, required: (0, _defineComponent.props)(Boolean).default(false), pattern: (0, _defineComponent.props)(String).optional, accept: (0, _defineComponent.props)(String, Number).optional, autocomplete: (0, _defineComponent.props)(String).optional, autofocus: (0, _defineComponent.props)(Boolean).default(false), autosave: (0, _defineComponent.props)(String).optional, rows: (0, _defineComponent.props)(String, Number).optional, cols: (0, _defineComponent.props)(String, Number).optional }, directives: { inputBase: _directive.default }, watch: { value: function value(cur) { this.stateValue = cur; } }, data: function data() { return { showPassword: false, isInputFocus: false }; }, methods: { togglePassword: function togglePassword(e) { e.stopPropagation(); this.showPassword = !this.showPassword; }, clear: function clear(e) { e.stopPropagation(); var input = this.$refs.input; if (input) { input.value = ''; } this.stateValue = ''; this.$emit('valueChange', this.stateValue); this.$emit('change', this.stateValue); this.$emit('clear'); }, focus: function focus() { var input = this.$refs.input; input && input.focus(); }, blur: function blur() { var input = this.$refs.input; input && input.blur(); }, handleBlur: function handleBlur(e) { this.isInputFocus = false; this.$emit('blur', e); }, handleFocus: function handleFocus(e) { this.isInputFocus = true; this.$emit('focus', e); }, handleChange: function handleChange(e) { var target = e.target; if (!target) return; this.stateValue = target.value; if (!target.composing) { this.$emit('valueChange', this.stateValue); } this.$emit('change', e); this.$emit('input', e); }, handleKeyDown: function handleKeyDown(e) { this.$emit('keydown', e); if (e.keyCode === 13) { e.preventDefault(); this.$emit('enter', e); } }, getAdornment: function getAdornment(adornment) { var h = this.$createElement; if (!adornment) return; if (typeof adornment === 'string') { return h("span", [adornment]); } else { return (0, _iconUtils.getIcon)(this.$createElement, adornment); } }, renderInput: function renderInput(root, attrs) { var h = this.$createElement; var type = this.type, allowClear = this.allowClear, allowTogglePassword = this.allowTogglePassword, startAdornment = this.startAdornment, endAdornment = this.endAdornment; var start = this.getAdornment(startAdornment); if (start) { start = h("div", { "class": root.element('adornment') }, [start]); } var end; if (this.$slots.endAdornment) { end = h("div", { "class": root.element('adornment').is('auto') }, [this.$slots.endAdornment]); } else { end = this.getAdornment(endAdornment); if (end) { end = h("div", { "class": root.element('adornment') }, [end]); } } var inputType = this.showPassword ? 'text' : type; var inputProps = { domProps: { value: this.stateValue, type: inputType }, class: root.element('base'), attrs: attrs, on: _objectSpread(_objectSpread({}, this.$listeners), {}, { focus: this.handleFocus, blur: this.handleBlur, input: this.handleChange, keydown: this.handleKeyDown }), directives: [{ name: 'inputBase' }], ref: 'input' }; if (this.type === 'textarea') { return h("textarea", (0, _babelHelperVueJsxMergeProps.default)([{}, inputProps])); } return [start, h("input", (0, _babelHelperVueJsxMergeProps.default)([{}, inputProps])), allowClear && !this.readonly && !this.disabled && h("div", { "class": root.element('adornment').is('button'), "attrs": { "tabindex": -1 } }, [this.stateValue && h(_Button.default, { "attrs": { "tabindex": -1, "link": true, "size": "md", "round": true, "icon": _CloseCircleFilled2.default }, "on": { "click": this.clear } })]), end, type === 'password' && allowTogglePassword && h("div", { "class": root.element('adornment').is('button'), "attrs": { "tabindex": -1 } }, [h(_Button.default, { "attrs": { "tabindex": -1, "link": true, "size": "md", "round": true, "icon": this.showPassword ? _EyeOutlined2.default : _EyeCloseOutlined2.default }, "on": { "click": this.togglePassword } })])]; } }, mounted: function mounted() { if (this.autofocus) { this.focus(); } }, render: function render() { var h = arguments[0]; var root = this.$rootCls(); var _this$$props = this.$props, type = _this$$props.type, value = _this$$props.value, outline = _this$$props.outline, allowClear = _this$$props.allowClear, allowTogglePassword = _this$$props.allowTogglePassword, startAdornment = _this$$props.startAdornment, endAdornment = _this$$props.endAdornment, noBorder = _this$$props.noBorder, autofocus = _this$$props.autofocus, name = _this$$props.name, attrs = _objectWithoutProperties(_this$$props, _excluded); if (attrs) { attrs.name = this.fieldName || name; } return h("div", { "class": root.has([this.startAdornment && 'start-adornment', !this.noBorder && 'border']).is([this.isInputFocus && 'focus', this.outline ? 'outline' : 'normal', this.disabled && 'disabled']) }, [this.renderInput(root, attrs)]); } }); var _default = exports.default = Input;