iview/src/components/input/input.vue

169 lines
5.2 KiB
Vue
Raw Normal View History

2016-09-09 14:29:19 +08:00
<template>
2016-11-07 14:16:20 +08:00
<div :class="wrapClasses">
<template v-if="type !== 'textarea'">
<div :class="[prefixCls + '-group-prepend']" v-if="prepend" v-show="slotReady" 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"
:class="inputClasses"
:placeholder="placeholder"
:disabled="disabled"
:maxlength="maxlength"
:readonly="readonly"
v-model="value"
@keyup.enter="handleEnter"
@focus="handleFocus"
@blur="handleBlur">
<div :class="[prefixCls + '-group-append']" v-if="append" v-show="slotReady" 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"
:disabled="disabled"
:rows="rows"
:maxlength="maxlength"
:readonly="readonly"
v-model="value"
@keyup.enter="handleEnter"
@focus="handleFocus"
@blur="handleBlur">
</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';
import calcTextareaHeight from '../../utils/calcTextareaHeight';
2016-09-09 14:29:19 +08:00
const prefixCls = 'ivu-input';
export default {
props: {
type: {
validator (value) {
2016-11-12 20:14:06 +08:00
return oneOf(value, ['text', 'textarea', 'password']);
},
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']);
}
},
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
},
readonly: {
type: Boolean,
default: false
2016-09-09 14:29:19 +08:00
}
},
data () {
return {
prefixCls: prefixCls,
prepend: true,
append: true,
slotReady: false,
textareaStyles: {}
2016-09-09 14:29:19 +08:00
}
},
computed: {
2016-11-07 14:16:20 +08:00
wrapClasses () {
return [
`${prefixCls}-wrapper`,
{
2016-11-11 15:35:26 +08:00
[`${prefixCls}-wrapper-${this.size}`]: !!this.size,
[`${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
},
textareaClasses () {
2016-09-09 14:29:19 +08:00
return [
`${prefixCls}`,
{
[`${prefixCls}-disabled`]: this.disabled
2016-09-09 14:29:19 +08:00
}
]
}
},
methods: {
handleEnter () {
this.$emit('on-enter');
},
handleIconClick () {
this.$emit('on-click');
},
handleFocus () {
this.$emit('on-focus');
},
handleBlur () {
this.$emit('on-blur');
},
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.slotReady = true;
this.resizeTextarea();
2016-09-09 14:29:19 +08:00
}
}
</script>