2016-09-09 14:29:19 +08:00
|
|
|
<template>
|
2016-11-07 14:16:20 +08:00
|
|
|
<div :class="wrapClasses">
|
2016-11-08 17:53:04 +08:00
|
|
|
<template v-if="type !== 'textarea'">
|
2017-04-01 12:28:32 +08:00
|
|
|
<div :class="[prefixCls + '-group-prepend']" v-if="prepend" v-show="slotReady"><slot name="prepend"></slot></div>
|
2017-04-01 12:08:08 +08:00
|
|
|
<i class="ivu-icon" :class="['ivu-icon-' + icon, prefixCls + '-icon', prefixCls + '-icon-normal']" v-if="icon" @click="handleIconClick"></i>
|
2017-03-01 15:23:12 +08:00
|
|
|
<transition name="fade">
|
|
|
|
<i class="ivu-icon ivu-icon-load-c ivu-load-loop" :class="[prefixCls + '-icon', prefixCls + '-icon-validate']" v-if="!icon"></i>
|
|
|
|
</transition>
|
2016-11-08 17:53:04 +08:00
|
|
|
<input
|
2017-05-10 23:34:15 +08:00
|
|
|
ref="input"
|
2016-11-12 20:14:06 +08:00
|
|
|
:type="type"
|
2016-11-08 17:53:04 +08:00
|
|
|
:class="inputClasses"
|
|
|
|
:placeholder="placeholder"
|
|
|
|
:disabled="disabled"
|
|
|
|
:maxlength="maxlength"
|
2016-11-15 10:43:00 +08:00
|
|
|
:readonly="readonly"
|
2016-11-28 15:49:03 +08:00
|
|
|
:name="name"
|
2017-03-01 15:23:12 +08:00
|
|
|
:value="currentValue"
|
2017-01-06 11:30:01 +08:00
|
|
|
:number="number"
|
2017-04-25 16:52:51 +08:00
|
|
|
:autofocus="autofocus"
|
2016-11-15 10:43:00 +08:00
|
|
|
@keyup.enter="handleEnter"
|
2017-07-18 19:33:58 +08:00
|
|
|
@keyup="handleKeyup"
|
|
|
|
@keypress="handleKeypress"
|
|
|
|
@keydown="handleKeydown"
|
2016-11-15 10:43:00 +08:00
|
|
|
@focus="handleFocus"
|
2016-12-15 20:16:58 +08:00
|
|
|
@blur="handleBlur"
|
2017-03-07 18:06:56 +08:00
|
|
|
@input="handleInput"
|
|
|
|
@change="handleChange">
|
2017-04-01 12:28:32 +08:00
|
|
|
<div :class="[prefixCls + '-group-append']" v-if="append" v-show="slotReady"><slot name="append"></slot></div>
|
2016-11-08 17:53:04 +08:00
|
|
|
</template>
|
|
|
|
<textarea
|
|
|
|
v-else
|
2017-03-01 15:23:12 +08:00
|
|
|
ref="textarea"
|
2016-11-08 17:53:04 +08:00
|
|
|
:class="textareaClasses"
|
|
|
|
:style="textareaStyles"
|
2016-11-07 14:16:20 +08:00
|
|
|
:placeholder="placeholder"
|
2016-11-08 17:53:04 +08:00
|
|
|
:disabled="disabled"
|
|
|
|
:rows="rows"
|
|
|
|
:maxlength="maxlength"
|
2016-11-15 10:43:00 +08:00
|
|
|
:readonly="readonly"
|
2016-11-28 15:49:03 +08:00
|
|
|
:name="name"
|
2017-07-19 16:37:07 +08:00
|
|
|
:value="currentValue"
|
2017-05-26 16:38:40 +08:00
|
|
|
:autofocus="autofocus"
|
2016-11-15 10:43:00 +08:00
|
|
|
@keyup.enter="handleEnter"
|
2017-07-18 19:33:58 +08:00
|
|
|
@keyup="handleKeyup"
|
|
|
|
@keypress="handleKeypress"
|
|
|
|
@keydown="handleKeydown"
|
2016-11-15 10:43:00 +08:00
|
|
|
@focus="handleFocus"
|
2016-12-15 20:16:58 +08:00
|
|
|
@blur="handleBlur"
|
2017-03-01 15:23:12 +08:00
|
|
|
@input="handleInput">
|
2016-11-08 17:53:04 +08:00
|
|
|
</textarea>
|
2016-11-07 14:16:20 +08:00
|
|
|
</div>
|
2016-09-09 14:29:19 +08:00
|
|
|
</template>
|
|
|
|
<script>
|
2017-03-13 18:58:31 +08:00
|
|
|
import { oneOf, findComponentUpward } from '../../utils/assist';
|
2016-11-08 17:53:04 +08:00
|
|
|
import calcTextareaHeight from '../../utils/calcTextareaHeight';
|
2017-03-09 11:14:40 +08:00
|
|
|
import Emitter from '../../mixins/emitter';
|
2016-09-09 14:29:19 +08:00
|
|
|
|
|
|
|
const prefixCls = 'ivu-input';
|
|
|
|
|
|
|
|
export default {
|
2017-03-01 17:01:22 +08:00
|
|
|
name: 'Input',
|
2017-03-09 11:14:40 +08:00
|
|
|
mixins: [ Emitter ],
|
2016-09-09 14:29:19 +08:00
|
|
|
props: {
|
|
|
|
type: {
|
2016-11-08 17:53:04 +08:00
|
|
|
validator (value) {
|
2016-11-12 20:14:06 +08:00
|
|
|
return oneOf(value, ['text', 'textarea', 'password']);
|
2016-11-08 17:53:04 +08:00
|
|
|
},
|
2016-09-09 14:29:19 +08:00
|
|
|
default: 'text'
|
|
|
|
},
|
|
|
|
value: {
|
|
|
|
type: [String, Number],
|
2017-03-01 15:23:12 +08:00
|
|
|
default: ''
|
2016-09-09 14:29:19 +08:00
|
|
|
},
|
|
|
|
size: {
|
|
|
|
validator (value) {
|
2017-08-16 13:52:50 +08:00
|
|
|
return oneOf(value, ['small', 'large', 'default']);
|
2016-09-09 14:29:19 +08:00
|
|
|
}
|
2016-11-08 17:53:04 +08:00
|
|
|
},
|
|
|
|
placeholder: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
maxlength: {
|
|
|
|
type: Number
|
|
|
|
},
|
|
|
|
disabled: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
icon: String,
|
|
|
|
autosize: {
|
|
|
|
type: [Boolean, Object],
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
rows: {
|
|
|
|
type: Number,
|
|
|
|
default: 2
|
2016-11-15 10:43:00 +08:00
|
|
|
},
|
|
|
|
readonly: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2016-11-28 15:49:03 +08:00
|
|
|
},
|
|
|
|
name: {
|
|
|
|
type: String
|
2017-01-06 11:30:01 +08:00
|
|
|
},
|
|
|
|
number: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2017-04-25 16:52:51 +08:00
|
|
|
},
|
|
|
|
autofocus: {
|
2017-04-28 14:53:17 +08:00
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2016-09-09 14:29:19 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
data () {
|
|
|
|
return {
|
2017-03-01 15:23:12 +08:00
|
|
|
currentValue: this.value,
|
2016-11-08 17:53:04 +08:00
|
|
|
prefixCls: prefixCls,
|
|
|
|
prepend: true,
|
|
|
|
append: true,
|
2016-11-15 11:24:28 +08:00
|
|
|
slotReady: false,
|
2016-11-08 17:53:04 +08:00
|
|
|
textareaStyles: {}
|
2016-12-25 22:49:42 +08:00
|
|
|
};
|
2016-09-09 14:29:19 +08:00
|
|
|
},
|
|
|
|
computed: {
|
2016-11-07 14:16:20 +08:00
|
|
|
wrapClasses () {
|
2016-11-08 17:53:04 +08:00
|
|
|
return [
|
|
|
|
`${prefixCls}-wrapper`,
|
|
|
|
{
|
2016-11-11 15:35:26 +08:00
|
|
|
[`${prefixCls}-wrapper-${this.size}`]: !!this.size,
|
2016-11-08 17:53:04 +08:00
|
|
|
[`${prefixCls}-type`]: this.type,
|
|
|
|
[`${prefixCls}-group`]: this.prepend || this.append,
|
2017-04-01 12:48:45 +08:00
|
|
|
[`${prefixCls}-group-${this.size}`]: (this.prepend || this.append) && !!this.size,
|
2017-04-01 13:25:47 +08:00
|
|
|
[`${prefixCls}-group-with-prepend`]: this.prepend,
|
|
|
|
[`${prefixCls}-group-with-append`]: this.append,
|
2017-04-01 12:48:45 +08:00
|
|
|
[`${prefixCls}-hide-icon`]: this.append // #554
|
2016-11-08 17:53:04 +08:00
|
|
|
}
|
2016-12-25 22:49:42 +08:00
|
|
|
];
|
2016-11-08 17:53:04 +08:00
|
|
|
},
|
|
|
|
inputClasses () {
|
|
|
|
return [
|
|
|
|
`${prefixCls}`,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-${this.size}`]: !!this.size,
|
|
|
|
[`${prefixCls}-disabled`]: this.disabled
|
|
|
|
}
|
2016-12-25 22:49:42 +08:00
|
|
|
];
|
2016-11-07 14:16:20 +08:00
|
|
|
},
|
2016-11-08 17:53:04 +08:00
|
|
|
textareaClasses () {
|
2016-09-09 14:29:19 +08:00
|
|
|
return [
|
|
|
|
`${prefixCls}`,
|
|
|
|
{
|
2016-11-08 17:53:04 +08:00
|
|
|
[`${prefixCls}-disabled`]: this.disabled
|
2016-09-09 14:29:19 +08:00
|
|
|
}
|
2016-12-25 22:49:42 +08:00
|
|
|
];
|
2016-09-09 14:29:19 +08:00
|
|
|
}
|
2016-11-08 17:53:04 +08:00
|
|
|
},
|
|
|
|
methods: {
|
2017-03-14 14:25:02 +08:00
|
|
|
handleEnter (event) {
|
|
|
|
this.$emit('on-enter', event);
|
2016-11-08 17:53:04 +08:00
|
|
|
},
|
2017-07-18 19:33:58 +08:00
|
|
|
handleKeydown (event) {
|
|
|
|
this.$emit('on-keydown', event);
|
|
|
|
},
|
|
|
|
handleKeypress(event) {
|
|
|
|
this.$emit('on-keypress', event);
|
|
|
|
},
|
|
|
|
handleKeyup (event) {
|
|
|
|
this.$emit('on-keyup', event);
|
|
|
|
},
|
2017-03-14 14:25:02 +08:00
|
|
|
handleIconClick (event) {
|
|
|
|
this.$emit('on-click', event);
|
2016-11-08 17:53:04 +08:00
|
|
|
},
|
2017-03-14 14:25:02 +08:00
|
|
|
handleFocus (event) {
|
|
|
|
this.$emit('on-focus', event);
|
2016-11-15 10:43:00 +08:00
|
|
|
},
|
2017-03-14 14:25:02 +08:00
|
|
|
handleBlur (event) {
|
2017-03-16 11:49:14 +08:00
|
|
|
this.$emit('on-blur', event);
|
2017-03-15 19:59:46 +08:00
|
|
|
if (!findComponentUpward(this, ['DatePicker', 'TimePicker', 'Cascader', 'Search'])) {
|
2017-03-13 18:58:31 +08:00
|
|
|
this.dispatch('FormItem', 'on-form-blur', this.currentValue);
|
|
|
|
}
|
2016-11-15 10:43:00 +08:00
|
|
|
},
|
2017-03-01 15:23:12 +08:00
|
|
|
handleInput (event) {
|
2017-03-21 14:13:33 +08:00
|
|
|
let value = event.target.value;
|
|
|
|
if (this.number) value = Number.isNaN(Number(value)) ? value : Number(value);
|
2017-03-01 15:23:12 +08:00
|
|
|
this.$emit('input', value);
|
|
|
|
this.setCurrentValue(value);
|
2016-12-15 23:29:31 +08:00
|
|
|
this.$emit('on-change', event);
|
2016-12-15 20:16:58 +08:00
|
|
|
},
|
2017-03-07 18:06:56 +08:00
|
|
|
handleChange (event) {
|
|
|
|
this.$emit('on-input-change', event);
|
|
|
|
},
|
2017-03-01 15:23:12 +08:00
|
|
|
setCurrentValue (value) {
|
|
|
|
if (value === this.currentValue) return;
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.resizeTextarea();
|
|
|
|
});
|
|
|
|
this.currentValue = value;
|
2017-03-15 19:59:46 +08:00
|
|
|
if (!findComponentUpward(this, ['DatePicker', 'TimePicker', 'Cascader', 'Search'])) {
|
2017-03-13 18:58:31 +08:00
|
|
|
this.dispatch('FormItem', 'on-form-change', value);
|
|
|
|
}
|
2017-03-01 15:23:12 +08:00
|
|
|
},
|
2016-11-08 17:53:04 +08:00
|
|
|
resizeTextarea () {
|
|
|
|
const autosize = this.autosize;
|
|
|
|
if (!autosize || this.type !== 'textarea') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const minRows = autosize.minRows;
|
|
|
|
const maxRows = autosize.maxRows;
|
|
|
|
|
2017-03-01 15:23:12 +08:00
|
|
|
this.textareaStyles = calcTextareaHeight(this.$refs.textarea, minRows, maxRows);
|
2017-05-10 23:34:15 +08:00
|
|
|
},
|
|
|
|
focus() {
|
|
|
|
if (this.type === 'textarea') {
|
|
|
|
this.$refs.textarea.focus();
|
|
|
|
} else {
|
|
|
|
this.$refs.input.focus();
|
|
|
|
}
|
2016-11-08 17:53:04 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
2017-03-01 15:23:12 +08:00
|
|
|
value (val) {
|
|
|
|
this.setCurrentValue(val);
|
2016-11-08 17:53:04 +08:00
|
|
|
}
|
|
|
|
},
|
2017-03-01 15:23:12 +08:00
|
|
|
mounted () {
|
|
|
|
if (this.type !== 'textarea') {
|
|
|
|
this.prepend = this.$slots.prepend !== undefined;
|
|
|
|
this.append = this.$slots.append !== undefined;
|
|
|
|
} else {
|
|
|
|
this.prepend = false;
|
|
|
|
this.append = false;
|
|
|
|
}
|
|
|
|
this.slotReady = true;
|
|
|
|
this.resizeTextarea();
|
2016-09-09 14:29:19 +08:00
|
|
|
}
|
2016-12-25 22:49:42 +08:00
|
|
|
};
|
|
|
|
</script>
|