mirror of
https://github.com/youzan/vant.git
synced 2025-10-18 01:17:15 +00:00
directory adjust: delete entry index.js
This commit is contained in:
150
packages/field/index.vue
Normal file
150
packages/field/index.vue
Normal file
@@ -0,0 +1,150 @@
|
||||
<template>
|
||||
<van-cell
|
||||
class="van-field"
|
||||
:title="label"
|
||||
:required="required"
|
||||
:class="{
|
||||
'van-field--hastextarea': type === 'textarea',
|
||||
'van-field--nolabel': !label,
|
||||
'van-field--disabled': disabled,
|
||||
'van-field--error': error,
|
||||
'van-field--border': border,
|
||||
'van-field--autosize': autosize,
|
||||
'van-field--has-icon': showIcon
|
||||
}">
|
||||
<textarea
|
||||
v-if="type === 'textarea'"
|
||||
ref="textareaElement"
|
||||
class="van-field__control"
|
||||
v-model="currentValue"
|
||||
@focus="handleInputFocus"
|
||||
@blur="handleInputBlur"
|
||||
:placeholder="placeholder"
|
||||
:maxlength="maxlength"
|
||||
:disabled="disabled"
|
||||
:readonly="readonly"
|
||||
:rows="rows"
|
||||
:cols="cols">
|
||||
</textarea>
|
||||
<input
|
||||
v-else
|
||||
class="van-field__control"
|
||||
:value="currentValue"
|
||||
@input="handleInput"
|
||||
@focus="handleInputFocus"
|
||||
@blur="handleInputBlur"
|
||||
:type="type"
|
||||
:placeholder="placeholder"
|
||||
:maxlength="maxlength"
|
||||
:disabled="disabled"
|
||||
:readonly="readonly">
|
||||
<div v-if="showIcon" class="van-field__icon" @click="onIconClick">
|
||||
<slot name="icon">
|
||||
<van-icon :name="icon"></van-icon>
|
||||
</slot>
|
||||
</div>
|
||||
</van-cell>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
const VALID_TYPES = ['text', 'number', 'email', 'url', 'tel', 'date', 'time', 'datetime', 'password', 'textarea'];
|
||||
import vanCell from '../cell';
|
||||
import vanIcon from '../icon';
|
||||
|
||||
export default {
|
||||
name: 'van-field',
|
||||
|
||||
components: {
|
||||
vanCell,
|
||||
vanIcon
|
||||
},
|
||||
|
||||
props: {
|
||||
type: {
|
||||
type: String,
|
||||
default: 'text',
|
||||
validator(value) {
|
||||
return VALID_TYPES.indexOf(value) > -1;
|
||||
}
|
||||
},
|
||||
placeholder: String,
|
||||
value: {},
|
||||
label: String,
|
||||
disabled: Boolean,
|
||||
error: Boolean,
|
||||
readonly: Boolean,
|
||||
required: Boolean,
|
||||
maxlength: [String, Number],
|
||||
border: Boolean,
|
||||
rows: [String, Number],
|
||||
cols: [String, Number],
|
||||
autosize: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
icon: String,
|
||||
onIconClick: {
|
||||
type: Function,
|
||||
default: () => {}
|
||||
}
|
||||
},
|
||||
|
||||
data() {
|
||||
return {
|
||||
currentValue: this.value
|
||||
};
|
||||
},
|
||||
|
||||
mounted() {
|
||||
if (this.autosize && this.type === 'textarea') {
|
||||
const el = this.$refs.textareaElement;
|
||||
el.style.height = el.scrollHeight + 'px';
|
||||
el.style.overflowY = 'hidden';
|
||||
}
|
||||
},
|
||||
|
||||
watch: {
|
||||
value(val) {
|
||||
this.currentValue = val;
|
||||
},
|
||||
|
||||
currentValue(val) {
|
||||
if (this.autosize && this.type === 'textarea') {
|
||||
this.$nextTick(this.sizeAdjust);
|
||||
}
|
||||
this.$emit('input', val);
|
||||
}
|
||||
},
|
||||
|
||||
computed: {
|
||||
showIcon() {
|
||||
// 有icon的slot,就认为一直展示
|
||||
if (this.$slots.icon) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return this.icon && this.currentValue;
|
||||
}
|
||||
},
|
||||
|
||||
methods: {
|
||||
handleInput(event) {
|
||||
this.currentValue = event.target.value;
|
||||
},
|
||||
|
||||
sizeAdjust() {
|
||||
const el = this.$refs.textareaElement;
|
||||
el.style.height = 'auto';
|
||||
el.style.height = el.scrollHeight + 'px';
|
||||
},
|
||||
|
||||
handleInputFocus() {
|
||||
this.$emit('focus');
|
||||
},
|
||||
|
||||
handleInputBlur() {
|
||||
this.$emit('blur');
|
||||
}
|
||||
}
|
||||
};
|
||||
</script>
|
Reference in New Issue
Block a user