微信小程序textarea层级过高覆盖其他组件的解决方法,亲测优化有效(taro,微信小程序原代码类似)

今天在做微信小程序一个页面多个textarea输入框时,出现了textarea层级过高覆盖其他组件的问题,在网上搜了很多资料,按照某一个解决方案思路去尝试,但是还是遇到了部分问题,最终优化解决了这些问题,运行效果图如下:

具体代码如下:

app.styl:

.hide{
 display: none !important;
}

.show{
 display: block !important;
}

// @textare
.textarea_content {
  width:100%;
  height: 192rpx;
  padding: 18rpx;
  font-size:32rpx;
  line-height:1.5;
  border-radius:8rpx;
  background-color:#FFF;
  -webkit-box-sizing:border-box;
  box-sizing:border-box;
  border-top:1PX #d6e4ef solid;
  border-bottom:1PX #d6e4ef solid;
  border-right:1PX #d6e4ef solid;
  border-left:1PX #d6e4ef solid;
}
/*隐藏滚动条*/
::-webkit-scrollbar{
  width: 0;
  height: 0;
  color: transparent;
}
.textarea_text {
  word-break: keep-all;
  word-wrap: break-word;
  width:100%;
  font-size:32rpx;
  line-height:1;
  outline:none;
  resize:none;
  -webkit-appearance:none;
  border-radius:0;
  padding:0;
  margin:0;
  border:0;
}
.textarea_placeholder{
  font-size:32rpx;
  color: #CCCCCC;
}

maintenanceAdd.tsx:

import Taro, { Component, Config } from '@tarojs/taro'
import { View, Text, ScrollView } from '@tarojs/components'
export default class maintenanceAdd extends Component {

    /**
     * 指定config的类型声明为: Taro.Config
     *
     * 由于 typescript 对于 object 类型推导只能推出 Key 的基本类型
     * 对于像 navigationBarTextStyle: 'black' 这样的推导出的类型是 string
     * 提示和声明 navigationBarTextStyle: 'black' | 'white' 类型冲突, 需要显示声明类型
     */
    config: Config = {
        navigationBarTitleText: '填写维修报告'
    }

    constructor() {
        super(...arguments)
        this.state = {
            repairDesc: "",
            problemDesc: "",
            // @textare
            textAreaObj: [{
              isContentFocus: true,
              isInputContentFocus: false,
              isFocus: false
            },{
              isContentFocus: true,
              isInputContentFocus: false,
              isFocus: false
            }]
        }
    }

   ......

    repairDescHandleText(e) {
        const value = e.detail.value
        this.setState({
            repairDesc: value
        })
    }

    problemDescHandleText(e) {
        const value = e.detail.value
        this.setState({
            problemDesc: value
        })
    }
  ......

  // @textare处理事件
  bindTextareaHandle(index, type){
    if(type=='blur'){
      this.state.textAreaObj[index] = {
        isFocus: false, //触发焦点
        isContentFocus: true, //聚焦时隐藏内容文本标签
        isInputContentFocus: false
      };
    }else if(type=='focus'){
      this.state.textAreaObj[index] = {
        isFocus: true, //触发焦点
        isContentFocus: false, //聚焦时隐藏内容文本标签
        isInputContentFocus: true
      };
    }
    this.setState({
      textAreaObj: this.state.textAreaObj
    });
  }

    render() {
        const {
            repairDesc,
            problemDesc,
            // @textare
            textAreaObj
        } = this.state;
        const textAreaObj0 = textAreaObj[0], textAreaObj1 = textAreaObj[1];
        return (
            <View className='page90 bk-F3 pd24 mt128'>
                <Cnavbar title='填写维修报告' left={true}></Cnavbar>
                <View className='p_view bk'>

                      ......

                      <View className='resultFont'>维修描述</View>
                      <View className={textAreaObj0.isInputContentFocus ? 'show':'hide'}>
                        <AtTextarea
                          name='repairDesc'
                          value={repairDesc}
                          focus={textAreaObj0.isFocus}
                          onChange={this.repairDescHandleText.bind(this)}
                          onBlur={this.bindTextareaHandle.bind(this,0,'blur')}
                          maxlength='180'
                          placeholder='请输入'
                        />
                      </View>
                      <View className={textAreaObj0.isContentFocus ? 'show':'hide'} onClick={this.bindTextareaHandle.bind(this,0,'focus')}>
                        <ScrollView
                          scrollY={true}
                          className="textarea_content"
                        >
                          {repairDesc
                            ? <Text className="textarea_text" space="nbsp">{repairDesc}</Text>
                            : <View className="textarea_placeholder">请输入</View>}
                        </ScrollView>
                      </View>

                       ......

                      <View className='resultFont'>问题描述</View>
                      <View className={textAreaObj1.isInputContentFocus ? 'show':'hide'}>
                        <AtTextarea
                          name='problemDesc'
                          value={problemDesc}
                          focus={textAreaObj1.isFocus}
                          onChange={this.problemDescHandleText.bind(this)}
                          onBlur={this.bindTextareaHandle.bind(this,1,'blur')}
                          maxlength='180'
                          placeholder='请输入'
                        />
                      </View>
                      <View className={textAreaObj1.isContentFocus ? 'show':'hide'} onClick={this.bindTextareaHandle.bind(this,1,'focus')}>
                        <ScrollView
                          scrollY={true}
                          className="textarea_content"
                        >
                          {problemDesc
                            ? <Text className="textarea_text" space="nbsp">{problemDesc}</Text>
                            : <View className="textarea_placeholder">请输入</View>}
                        </ScrollView>
                      </View>
                    </View>

                    ......

                    <View className='mt26'>
                        <AtButton className='btn1' onClick={this.saveHandler}><View className='white-Font'>提交维修报告</View></AtButton>
                    </View>
                </View>
            </View>
        )
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

txp1993

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值