@fesjs/fes-design
Version:
fes-design for PC
265 lines (260 loc) • 10.4 kB
JavaScript
import _defineProperty from '@babel/runtime/helpers/esm/defineProperty';
import { defineComponent, ref, computed, shallowRef, watch, nextTick, onMounted, resolveComponent, openBlock, createElementBlock, normalizeClass, Fragment, renderSlot, createCommentVNode, createVNode, createSlots, withCtx, toDisplayString, normalizeStyle } from 'vue';
import { UPDATE_MODEL_EVENT } from '../_util/constants';
import useFormAdaptor from '../_util/use/useFormAdaptor';
import getPrefixCls from '../_util/getPrefixCls';
import { useTheme } from '../_theme/useTheme';
import { useNormalModel } from '../_util/use/useModel';
import { useInput } from '../_util/use/useInput';
import calcTextareaHeight from './calcTextareaHeight';
import InputInner from './inputInner.js';
import { commonInputProps } from './props';
import { useFocus, useMouse } from './useInput';
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 prefixCls = getPrefixCls('input');
const textareaPrefixCls = getPrefixCls('textarea');
const inputProps = _objectSpread(_objectSpread({}, commonInputProps), {}, {
rows: {
type: Number,
default: 2
},
showWordLimit: {
type: Boolean,
default: false
},
autosize: {
type: [Boolean, Object],
default: false
},
autofocus: {
type: Boolean,
default: false
},
resize: String
});
function useWordLimit(currentValue, props) {
const isWordLimitVisible = computed(() => props.showWordLimit && props.maxlength && !props.disabled);
const textLength = computed(() => {
var _currentValue$value;
return ((_currentValue$value = currentValue.value) === null || _currentValue$value === void 0 ? void 0 : _currentValue$value.toString().length) || 0;
});
return {
isWordLimitVisible,
textLength
};
}
var script = defineComponent({
name: 'FInput',
components: {
InputInner
},
props: inputProps,
emits: [UPDATE_MODEL_EVENT, 'change', 'input', 'keydown', 'blur', 'focus', 'clear', 'mouseleave', 'mouseenter'],
setup(props, _ref) {
let {
emit
} = _ref;
useTheme();
const {
validate,
isError,
isFormDisabled
} = useFormAdaptor();
const inputRef = ref();
const textareaRef = ref();
const {
handleFocus,
handleBlur
} = useFocus(emit, validate);
const {
onMouseLeave,
onMouseEnter
} = useMouse(emit);
const [currentValue, updateCurrentValue] = useNormalModel(props, emit);
const handleChange = value => {
emit('change', value);
};
const handleTextareaChange = event => {
const {
value
} = event.target;
handleChange(value);
};
const handleValueChange = value => {
updateCurrentValue(value);
emit('input', value);
// 对于 form 表单校验,Input 的 input 事件就是 change 事件
validate('input');
validate('change');
};
const classes = computed(() => [props.type === 'textarea' ? textareaPrefixCls : prefixCls, {
'is-error': isError.value
}]);
const innerDisabled = computed(() => props.disabled || isFormDisabled.value);
const textareaCalcStyle = shallowRef(props.inputStyle);
const textareaStyle = computed(() => [props.inputStyle, textareaCalcStyle.value, {
resize: props.resize
}]);
const resizeTextarea = () => {
const {
type,
autosize
} = props;
if (type !== 'textarea' || !textareaRef.value) {
return;
}
if (autosize) {
let minRows;
let maxRows;
if (typeof autosize === 'object') {
minRows = autosize.minRows;
maxRows = autosize.maxRows;
}
textareaCalcStyle.value = _objectSpread({}, calcTextareaHeight(textareaRef.value, minRows, maxRows));
} else {
textareaCalcStyle.value = {
minHeight: calcTextareaHeight(textareaRef.value).minHeight
};
}
};
watch(currentValue, () => {
nextTick(resizeTextarea);
});
onMounted(() => {
nextTick(resizeTextarea);
});
const handleKeydown = e => {
emit('keydown', e);
};
const currentInput = computed(() => props.type === 'textarea' ? textareaRef.value : inputRef.value);
const focus = () => {
currentInput.value.focus();
};
const blur = () => {
currentInput.value.blur();
};
const handleInputClear = () => {
emit('clear');
};
onMounted(() => {
if (props.autofocus) {
focus();
}
});
return _objectSpread(_objectSpread({
innerDisabled,
isError,
inputRef,
textareaRef,
prefixCls,
textareaPrefixCls,
classes,
currentValue
}, useInput(handleValueChange)), {}, {
handleFocus,
handleBlur,
focus,
blur,
handleTextareaChange,
handleChange,
handleKeydown,
handleInputClear,
onMouseLeave,
onMouseEnter,
textareaStyle
}, useWordLimit(currentValue, props));
}
});
const _hoisted_1 = ["value", "readonly", "disabled", "autocomplete", "maxlength", "placeholder", "rows"];
function render(_ctx, _cache, $props, $setup, $data, $options) {
const _component_InputInner = resolveComponent("InputInner");
return openBlock(), createElementBlock("div", {
class: normalizeClass([..._ctx.classes, (_ctx.$slots.prepend || _ctx.$slots.append) && `${_ctx.prefixCls}-group`, _ctx.$slots.prepend && `${_ctx.prefixCls}-group-prepend`, _ctx.$slots.append && `${_ctx.prefixCls}-group-append`]),
onMouseenter: _cache[7] || (_cache[7] = function () {
return _ctx.onMouseEnter && _ctx.onMouseEnter(...arguments);
}),
onMouseleave: _cache[8] || (_cache[8] = function () {
return _ctx.onMouseLeave && _ctx.onMouseLeave(...arguments);
})
}, [_ctx.type !== 'textarea' ? (openBlock(), createElementBlock(Fragment, {
key: 0
}, [_ctx.$slots.prepend ? (openBlock(), createElementBlock("div", {
key: 0,
class: normalizeClass(`${_ctx.prefixCls}-prepend`)
}, [renderSlot(_ctx.$slots, "prepend")], 2 /* CLASS */)) : createCommentVNode("v-if", true), createVNode(_component_InputInner, {
ref: "inputRef",
modelValue: _ctx.currentValue,
type: _ctx.type,
placeholder: _ctx.placeholder,
readonly: _ctx.readonly,
disabled: _ctx.innerDisabled,
clearable: _ctx.clearable,
maxlength: _ctx.maxlength,
showPassword: _ctx.showPassword,
inputStyle: _ctx.inputStyle,
autocomplete: _ctx.autocomplete,
innerIsError: _ctx.isError,
onInput: _ctx.handleInput,
onChange: _ctx.handleChange,
onFocus: _ctx.handleFocus,
onBlur: _ctx.handleBlur,
onKeydown: _ctx.handleKeydown,
onClear: _ctx.handleInputClear
}, createSlots({
_: 2 /* DYNAMIC */
}, [_ctx.$slots.prefix ? {
name: "prefix",
fn: withCtx(() => [renderSlot(_ctx.$slots, "prefix")]),
key: "0"
} : undefined, _ctx.$slots.suffix || _ctx.isWordLimitVisible ? {
name: "suffix",
fn: withCtx(() => [renderSlot(_ctx.$slots, "suffix"), _ctx.isWordLimitVisible ? (openBlock(), createElementBlock("span", {
key: 0,
class: normalizeClass(`${_ctx.prefixCls}-count`)
}, toDisplayString(_ctx.textLength) + "/" + toDisplayString(_ctx.maxlength), 3 /* TEXT, CLASS */)) : createCommentVNode("v-if", true)]),
key: "1"
} : undefined]), 1032 /* PROPS, DYNAMIC_SLOTS */, ["modelValue", "type", "placeholder", "readonly", "disabled", "clearable", "maxlength", "showPassword", "inputStyle", "autocomplete", "innerIsError", "onInput", "onChange", "onFocus", "onBlur", "onKeydown", "onClear"]), _ctx.$slots.append ? (openBlock(), createElementBlock("div", {
key: 1,
class: normalizeClass(`${_ctx.prefixCls}-append`)
}, [renderSlot(_ctx.$slots, "append")], 2 /* CLASS */)) : createCommentVNode("v-if", true)], 64 /* STABLE_FRAGMENT */)) : (openBlock(), createElementBlock("textarea", {
key: 1,
ref: "textareaRef",
value: _ctx.currentValue,
style: normalizeStyle(_ctx.textareaStyle),
class: normalizeClass(`${_ctx.textareaPrefixCls}-inner`),
readonly: _ctx.readonly,
disabled: _ctx.innerDisabled,
autocomplete: _ctx.autocomplete,
maxlength: _ctx.maxlength,
placeholder: _ctx.placeholder,
rows: _ctx.rows,
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.handleTextareaChange && _ctx.handleTextareaChange(...arguments);
}),
onFocus: _cache[4] || (_cache[4] = function () {
return _ctx.handleFocus && _ctx.handleFocus(...arguments);
}),
onBlur: _cache[5] || (_cache[5] = function () {
return _ctx.handleBlur && _ctx.handleBlur(...arguments);
}),
onKeydown: _cache[6] || (_cache[6] = function () {
return _ctx.handleKeydown && _ctx.handleKeydown(...arguments);
})
}, null, 46 /* CLASS, STYLE, PROPS, NEED_HYDRATION */, _hoisted_1)), _ctx.isWordLimitVisible && _ctx.type === 'textarea' ? (openBlock(), createElementBlock("span", {
key: 2,
class: normalizeClass(`${_ctx.textareaPrefixCls}-count`)
}, toDisplayString(_ctx.textLength) + "/" + toDisplayString(_ctx.maxlength), 3 /* TEXT, CLASS */)) : createCommentVNode("v-if", true)], 34 /* CLASS, NEED_HYDRATION */);
}
script.render = render;
script.__file = "components/input/input.vue";
export { script as default, inputProps, useWordLimit };