iview/src/components/input/input.vue

212 lines
7.1 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'">
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>
<input
2016-11-12 20:14:06 +08:00
:type="type"
:class="inputClasses"
:placeholder="placeholder"
:disabled="disabled"
:maxlength="maxlength"
: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"
@keyup.enter="handleEnter"
@focus="handleFocus"
2016-12-15 20:16:58 +08:00
@blur="handleBlur"
@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>
</template>
<textarea
v-else
2017-03-01 15:23:12 +08:00
ref="textarea"
:class="textareaClasses"
:style="textareaStyles"
2016-11-07 14:16:20 +08:00
:placeholder="placeholder"
:disabled="disabled"
:rows="rows"
:maxlength="maxlength"
:readonly="readonly"
2016-11-28 15:49:03 +08:00
:name="name"
2017-03-01 15:23:12 +08:00
:value="value"
@keyup.enter="handleEnter"
@focus="handleFocus"
2016-12-15 20:16:58 +08:00
@blur="handleBlur"
2017-03-01 15:23:12 +08:00
@input="handleInput">
</textarea>
2016-11-07 14:16:20 +08:00
</div>
2016-09-09 14:29:19 +08:00
</template>
<script>
import { oneOf, findComponentUpward } from '../../utils/assist';
import calcTextareaHeight from '../../utils/calcTextareaHeight';
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',
mixins: [ Emitter ],
2016-09-09 14:29:19 +08:00
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],
2017-03-01 15:23:12 +08:00
default: ''
2016-09-09 14:29:19 +08:00
},
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-11-28 15:49:03 +08:00
},
name: {
type: String
2017-01-06 11:30:01 +08:00
},
number: {
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,
prefixCls: prefixCls,
prepend: true,
append: true,
slotReady: false,
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 () {
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,
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-12-25 22:49:42 +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
},
textareaClasses () {
2016-09-09 14:29:19 +08:00
return [
`${prefixCls}`,
{
[`${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
}
},
methods: {
handleEnter (event) {
this.$emit('on-enter', event);
},
handleIconClick (event) {
this.$emit('on-click', event);
},
handleFocus (event) {
this.$emit('on-focus', event);
},
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'])) {
this.dispatch('FormItem', 'on-form-blur', this.currentValue);
}
},
2017-03-01 15:23:12 +08:00
handleInput (event) {
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
},
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'])) {
this.dispatch('FormItem', 'on-form-change', value);
}
2017-03-01 15:23:12 +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);
}
},
watch: {
2017-03-01 15:23:12 +08:00
value (val) {
this.setCurrentValue(val);
}
},
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>