UNPKG

@fesjs/fes-design

Version:
219 lines (214 loc) 9.25 kB
import _defineProperty from '@babel/runtime/helpers/esm/defineProperty'; import { defineComponent, ref, computed, resolveComponent, openBlock, createElementBlock, normalizeClass, createCommentVNode, renderSlot, createElementVNode, normalizeStyle, withKeys, withModifiers, createBlock, Fragment } from 'vue'; import { debounce } from 'lodash-es'; import { EyeOutlined, EyeInvisibleOutlined, CloseCircleFilled } from '../icon'; import getPrefixCls from '../_util/getPrefixCls'; import { useNormalModel } from '../_util/use/useModel'; import { UPDATE_MODEL_EVENT } from '../_util/constants'; import { useInput } from '../_util/use/useInput'; import { useMouse } from './useInput'; import { commonInputProps } from './props'; 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; } const inputInnerProps = _objectSpread(_objectSpread({}, commonInputProps), {}, { // 内部使用,处理页面存在多个 input focus 样式场景 innerIsFocus: Boolean, innerIsError: Boolean, canEdit: { type: Boolean, default: true } }); const prefixCls = getPrefixCls('input-inner'); function usePassword(currentValue, props, focused) { const passwordVisible = ref(false); const handlePasswordVisible = () => { passwordVisible.value = !passwordVisible.value; }; const showPwdSwitchIcon = computed(() => props.showPassword && !props.readonly && !props.disabled && (currentValue.value != null || focused.value)); return { passwordVisible, handlePasswordVisible, showPwdSwitchIcon }; } function useClear(currentValue, props, focused, hovering, handleValueChange, emit) { const showClear = computed(() => props.clearable && !props.readonly && !props.disabled && currentValue.value && (focused.value || hovering.value)); const clear = () => { if (currentValue.value !== '') { handleValueChange(''); } emit('clear'); }; return { showClear, clear }; } var script = defineComponent({ name: 'FInputInner', components: { EyeOutlined, EyeInvisibleOutlined, CloseCircleFilled }, props: inputInnerProps, emits: [UPDATE_MODEL_EVENT, 'input', 'focus', 'blur', 'change', 'clear', 'keydown', 'mouseleave', 'mouseenter'], setup(props, _ref) { let { emit } = _ref; const inputRefEl = ref(); const [currentValue, updateCurrentValue] = useNormalModel(props, emit); const suffixVisible = computed(() => props.showPassword || props.clearable); const handleValueChange = value => { updateCurrentValue(value); emit('input', value); }; const focused = ref(false); const handleFocus = event => { focused.value = true; emit('focus', event); }; const handleBlur = event => { focused.value = false; emit('blur', event); }; const { hovering, onMouseLeave, onMouseEnter } = useMouse(emit); const handleChange = debounce(event => { const { value } = event.target; emit('change', value); }); const handleKeydown = e => { emit('keydown', e); }; const { showClear, clear } = useClear(currentValue, props, focused, hovering, handleValueChange, emit); const focus = () => { inputRefEl.value.focus(); }; const blur = () => { inputRefEl.value.blur(); }; const handleMousedown = e => { if (props.disabled) { return; } const { tagName } = e.target; if (tagName !== 'INPUT' && tagName !== 'TEXTAREA') { e.preventDefault(); if (!focused.value) { focus(); } } }; return _objectSpread(_objectSpread({ inputRefEl, // 外部使用 focus, blur, focused, handleFocus, handleBlur, showClear, clear, handleChange, onMouseLeave, onMouseEnter, handleMousedown, handleKeydown, prefixCls, suffixVisible, currentValue }, useInput(handleValueChange)), usePassword(currentValue, props, focused)); } }); const _hoisted_1 = ["tabindex", "value", "maxlength", "type", "readonly", "disabled", "placeholder", "autocomplete"]; function render(_ctx, _cache, $props, $setup, $data, $options) { const _component_CloseCircleFilled = resolveComponent("CloseCircleFilled"); const _component_EyeOutlined = resolveComponent("EyeOutlined"); const _component_EyeInvisibleOutlined = resolveComponent("EyeInvisibleOutlined"); return openBlock(), createElementBlock("span", { class: normalizeClass([_ctx.prefixCls, (_ctx.focused || _ctx.innerIsFocus) && `${_ctx.prefixCls}-focus`, _ctx.disabled && `${_ctx.prefixCls}-disabled`, _ctx.innerIsError && `${_ctx.prefixCls}-error`]), onMousedown: _cache[9] || (_cache[9] = function () { return _ctx.handleMousedown && _ctx.handleMousedown(...arguments); }), onMouseenter: _cache[10] || (_cache[10] = function () { return _ctx.onMouseEnter && _ctx.onMouseEnter(...arguments); }), onMouseleave: _cache[11] || (_cache[11] = function () { return _ctx.onMouseLeave && _ctx.onMouseLeave(...arguments); }) }, [createCommentVNode(" 前置内容 "), _ctx.$slots.prefix ? (openBlock(), createElementBlock("span", { key: 0, class: normalizeClass(`${_ctx.prefixCls}-prefix`) }, [renderSlot(_ctx.$slots, "prefix")], 2 /* CLASS */)) : createCommentVNode("v-if", true), createElementVNode("input", { ref: "inputRefEl", tabindex: !_ctx.disabled ? 0 : undefined, value: _ctx.currentValue, maxlength: _ctx.maxlength, type: _ctx.showPassword ? _ctx.passwordVisible ? 'text' : 'password' : _ctx.type, readonly: !_ctx.canEdit || _ctx.readonly, disabled: _ctx.disabled, placeholder: _ctx.placeholder, autocomplete: _ctx.autocomplete, style: normalizeStyle(_ctx.inputStyle), class: normalizeClass(`${_ctx.prefixCls}-el`), onCompositionstart: _cache[0] || (_cache[0] = function () { return _ctx.handleCompositionStart && _ctx.handleCompositionStart(...arguments); }), onCompositionend: _cache[1] || (_cache[1] = function () { return _ctx.handleCompositionEnd && _ctx.handleCompositionEnd(...arguments); }), onInput: _cache[2] || (_cache[2] = function () { return _ctx.handleInput && _ctx.handleInput(...arguments); }), onChange: _cache[3] || (_cache[3] = function () { return _ctx.handleChange && _ctx.handleChange(...arguments); }), onKeydown: [_cache[4] || (_cache[4] = withKeys(function () { return _ctx.handleChange && _ctx.handleChange(...arguments); }, ["enter"])), _cache[7] || (_cache[7] = function () { return _ctx.handleKeydown && _ctx.handleKeydown(...arguments); })], onFocus: _cache[5] || (_cache[5] = function () { return _ctx.handleFocus && _ctx.handleFocus(...arguments); }), onBlur: _cache[6] || (_cache[6] = function () { return _ctx.handleBlur && _ctx.handleBlur(...arguments); }) }, null, 46 /* CLASS, STYLE, PROPS, NEED_HYDRATION */, _hoisted_1), createCommentVNode(" 后置内容 "), _ctx.$slots.suffix || _ctx.suffixVisible ? (openBlock(), createElementBlock("span", { key: 1, class: normalizeClass(`${_ctx.prefixCls}-suffix`), onMousedown: _cache[8] || (_cache[8] = withModifiers(() => {}, ["prevent"])) }, [!_ctx.showClear && !_ctx.showPwdSwitchIcon ? renderSlot(_ctx.$slots, "suffix", { key: 0 }) : createCommentVNode("v-if", true), _ctx.showClear ? (openBlock(), createBlock(_component_CloseCircleFilled, { key: 1, class: normalizeClass(`${_ctx.prefixCls}-icon`), onClick: withModifiers(_ctx.clear, ["stop"]) }, null, 8 /* PROPS */, ["class", "onClick"])) : createCommentVNode("v-if", true), _ctx.showPwdSwitchIcon ? (openBlock(), createElementBlock(Fragment, { key: 2 }, [_ctx.passwordVisible ? (openBlock(), createBlock(_component_EyeOutlined, { key: 0, class: normalizeClass(`${_ctx.prefixCls}-icon`), onClick: withModifiers(_ctx.handlePasswordVisible, ["stop"]) }, null, 8 /* PROPS */, ["class", "onClick"])) : (openBlock(), createBlock(_component_EyeInvisibleOutlined, { key: 1, class: normalizeClass(`${_ctx.prefixCls}-icon`), onClick: withModifiers(_ctx.handlePasswordVisible, ["stop"]) }, null, 8 /* PROPS */, ["class", "onClick"]))], 64 /* STABLE_FRAGMENT */)) : createCommentVNode("v-if", true)], 34 /* CLASS, NEED_HYDRATION */)) : createCommentVNode("v-if", true)], 34 /* CLASS, NEED_HYDRATION */); } script.render = render; script.__file = "components/input/inputInner.vue"; export { script as default, useClear, usePassword };