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'">
|
|
|
|
<div :class="[prefixCls + '-group-prepend']" v-if="prepend" v-el:prepend><slot name="prepend"></slot></div>
|
|
|
|
<i class="ivu-icon" :class="['ivu-icon-' + icon, prefixCls + '-icon']" v-if="icon" @click="handleIconClick"></i>
|
|
|
|
<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"
|
|
|
|
v-model="value"
|
|
|
|
@keyup.enter="handleEnter">
|
|
|
|
<div :class="[prefixCls + '-group-append']" v-if="append" v-el:append><slot name="append"></slot></div>
|
|
|
|
</template>
|
|
|
|
<textarea
|
|
|
|
v-else
|
|
|
|
v-el:textarea
|
|
|
|
: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"
|
|
|
|
v-model="value"
|
|
|
|
@keyup.enter="handleEnter">
|
|
|
|
</textarea>
|
2016-11-07 14:16:20 +08:00
|
|
|
</div>
|
2016-09-09 14:29:19 +08:00
|
|
|
</template>
|
|
|
|
<script>
|
|
|
|
import { oneOf } from '../../utils/assist';
|
2016-11-08 17:53:04 +08:00
|
|
|
import calcTextareaHeight from '../../utils/calcTextareaHeight';
|
2016-09-09 14:29:19 +08:00
|
|
|
|
|
|
|
const prefixCls = 'ivu-input';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
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],
|
|
|
|
default: '',
|
|
|
|
twoWay: true
|
|
|
|
},
|
|
|
|
size: {
|
|
|
|
validator (value) {
|
|
|
|
return oneOf(value, ['small', 'large']);
|
|
|
|
}
|
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-09-09 14:29:19 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
data () {
|
|
|
|
return {
|
2016-11-08 17:53:04 +08:00
|
|
|
prefixCls: prefixCls,
|
|
|
|
prepend: true,
|
|
|
|
append: true,
|
|
|
|
textareaStyles: {}
|
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,
|
|
|
|
[`${prefixCls}-group-${this.size}`]: (this.prepend || this.append) && !!this.size
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
inputClasses () {
|
|
|
|
return [
|
|
|
|
`${prefixCls}`,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-${this.size}`]: !!this.size,
|
|
|
|
[`${prefixCls}-disabled`]: this.disabled
|
|
|
|
}
|
|
|
|
]
|
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-11-08 17:53:04 +08:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
handleEnter () {
|
|
|
|
this.$emit('on-enter');
|
|
|
|
},
|
|
|
|
handleIconClick () {
|
|
|
|
this.$emit('on-click');
|
|
|
|
},
|
|
|
|
resizeTextarea () {
|
|
|
|
const autosize = this.autosize;
|
|
|
|
if (!autosize || this.type !== 'textarea') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const minRows = autosize.minRows;
|
|
|
|
const maxRows = autosize.maxRows;
|
|
|
|
|
|
|
|
this.textareaStyles = calcTextareaHeight(this.$els.textarea, minRows, maxRows);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
value (val) {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.resizeTextarea();
|
|
|
|
});
|
|
|
|
this.$emit('on-change', val);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ready () {
|
|
|
|
if (this.type === 'text') {
|
|
|
|
this.prepend = this.$els.prepend.innerHTML !== '';
|
|
|
|
this.append = this.$els.append.innerHTML !== '';
|
|
|
|
} else {
|
|
|
|
this.prepend = false;
|
|
|
|
this.append = false;
|
|
|
|
}
|
|
|
|
this.resizeTextarea();
|
2016-09-09 14:29:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|