| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <template>
- <!-- @focus="focus" -->
- <div class="app">
- <textarea v-if="currentIsFocus" class="textarea-input" :focus="currentIsFocus" :auto-height="autoHeight" @blur="blur" @focus="focus" :disabled="disabled" @input="changeInput" :placeholder="placeholder" :maxlength="maxlength" @linechange="lineChange" :placeholder-class="placeholderClass" @confirm="confirm" v-model="currentValue"></textarea>
- <div v-else class="textarea-box" @click="focus">
- <div class="textarea-placeholder" v-if="$util.isEmpty(currentValue)">{{placeholder}}</div>
- <div :style="{height:autoHeight?currentHeight:'auto'}" class="textarea-dec" v-else>{{currentValue}}</div>
- </div>
- </div>
- </template>
- <script>
- /**
- * 解决textarea 层级最高问题
- *
- */
- export default {
- name: 'appTextarea', //
- props: {
- value: {
- type: String,
- default: ''
- },
- placeholder: {
- type: String,
- default: ''
- },
- maxlength: {
- type: Number,
- default: 200
- },
- autoHeight: {
- type: Boolean,
- default: true
- },
- disabled: {
- type: Boolean,
- default: false
- },
- isFocus: {
- type: Boolean,
- default: false
- },
- placeholderClass: {
- type: String,
- default: 'placeholder'
- }
- },
- data() {
- return {
- currentValue: '',
- isBlur: true,
- currentHeight: '',
- currentIsFocus: false
- }
- },
- created() {
- this.currentValue = this.$util.isEmpty(this.value) ? '' : this.value
- this.currentIsFocus = this.isFocus
- },
- methods: {
- blur() {
- this.currentIsFocus = false
- this.$emit('blur')
- },
- focus() {
- this.currentIsFocus = true
- this.$emit('focus')
- },
- changeInput(event) {
- this.$emit('input', event.target.value)
- },
- lineChange(val) {
- this.currentHeight = val.detail.height + 'px'
- },
- confirm() {
- this.currentIsFocus = false
- }
- },
- watch: {
- value(val) {
- this.currentValue = val
- },
- currentValue(val) {
- this.$emit('input', val)
- },
- isFocus(val) {
- this.currentIsFocus = val
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .app {
- width: 100%;
- height: 100%;
- // line-height: 1;
- display: inline-block;
- & .textarea-input {
- width: 100%;
- min-height: 36upx;
- height: 100%;
- font-size: 28upx;
- color: #4d4d4d;
- }
- & .textarea-box {
- font-size: 0;
- display: block;
- position: relative;
- cursor: auto;
- min-height: 36upx;
- height: 100%;
- width: 100%;
- overflow-y: auto;
- & .textarea-dec {
- font-size: 28upx;
- color: #4d4d4d;
- word-wrap: break-word;
- word-break: break-all;
- white-space: pre-wrap;
- // line-height: 33upx;
- }
- & .textarea-placeholder {
- display: inline-block;
- font-size: 28upx;
- color: #ccc;
- vertical-align: middle;
- }
- }
- }
- .placeholder {
- font-size: 28upx;
- color: #ccc;
- }
- </style>
|