【无标题】

.inputWrapper {
  width: 180px;
  /* max-height: 50px; */
  outline: 1px solid #000;
  position: relative;
  /* line-height: 19px; */
  min-height: 29px;
  box-sizing: border-box;
  overflow: hidden;
  margin: 20px;
}
.titleDiv {
  padding: 5px;
  margin: 0;
  font: 400 14px helvetica, arial, sans-serif;
  box-sizing: border-box;
  width: 100%;
  white-space: pre-wrap;
  word-wrap: break-word;
  visibility: hidden;
}
.title {
  padding: 5px;
  font: 400 14px helvetica, arial, sans-serif;
  box-sizing: border-box;
  width: 100%;
  white-space: pre-wrap;
  word-wrap: break-word;
  height: 100%;
  display: block;
  border: none;
  position: absolute;
  top: 0;
  left: 0;
  resize: none;
  overflow: hidden;
}
.title_normal {
  resize: none;
  border: 1px solid #000;
  min-height: 32px;
  box-sizing: border-box;
  font-size: 20px;
  width: 400px;
}
import {useRef, useEffect, useState} from 'react'
import './index.css'
function Title2() {
  const textWrapper = useRef(null)
  const textDiv = useRef(null)
  const textRef = useRef(null)
  const [str,setStr]=useState('我')
  const [inputLock,setInutLock]=useState(false)
  function makeExpandingArea(container){
    // textRef.current.addEventListener('input',function(){
    //   setStr(textRef.current.value)
    //   textDiv.current.textContent = textRef.current.value
    //   // console.log('textWrapper-style.height',textWrapper.current.style.height)
    //   // // console.log('titleDiv-scrollHeight',titleDiv.current.scrollHeight)
    //   // console.log('textRef-scrollHeight',textRef.current.scrollHeight)
    //   // if(textRef.current.scrollHeight>textWrapper.current.style.height.replace('px','')*1){
    //   //   console.log('!=')
    //   //   textWrapper.current.style.height=textRef.current.scrollHeight+'px'
    //   // }
    // },false)
    textDiv.current.textContent = textRef.current.value
    // console.log('scrollHeight',textWrapper.current.scrollHeight)
  }
  useEffect(() => {
    makeExpandingArea(textWrapper.current)
  })
  function handleKeyDown(e){
    if(e.key==='Enter'){
      e.preventDefault()
    }
  }
  function limitStr(strValue){
    console.log('strValue',strValue)
    // if(strValue){
    let re=''
    const strLength = strValue.strLength
    const byteleng = strValue.replace(/[^\x00-\xff]/g, '**').strLength
    console.log('byteleng',byteleng)
    if(byteleng<=20){
      return strValue
    }
    for(let i=0,bytenum=0;i<strLength;i++){
      let byte=strValue.charAt(i)
      if(/[\x00-\xff]/.test(byte)){
        bytenum++
      }else{
        bytenum+=2
      }
      if(bytenum<=20){
        re+=byte
      }else{
        return re
      }
    }
    // }
  }
  // useEffect(() => {
  //   limitStr(textRef?.current?.value)
  //   console.log('??',limitStr(textRef?.current?.value))
  // },[textRef?.current?.value])

 

 
  function limitWords() {
    var ChiLength=0;//中文汉字数
    var maxLength=0;
    function strlen(str){
        var newDeviceNameVal='';
        var len = 0;//字符长度
        ChiLength=0;//中文汉字数
        for (var i=0; i<str.length; i++) {
            var c = str.charCodeAt(i);
            var one = (c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f);//是否有单字节字符
            console.log('one',one)
            //单字节加1
            if (one&&len<=20) {
                len++;
                maxLength++;
            } else if (len<20) {
                len+=2;
                ChiLength++;
                maxLength++;
            }else {
              break
            }
            newDeviceNameVal+=str[i]
        }
        console.log('len',len)
        return {len,newDeviceNameVal};
    }
    var deviceNameVal=textRef.current.value;
    console.log('deviceNameVal',deviceNameVal)
    var deviceNameInput=textRef.current;
    var strLength=strlen(deviceNameVal).len;
    var filteredStr=strlen(deviceNameVal).newDeviceNameVal;
    console.log('ChiLength',ChiLength)
    //根据字符串中的汉字数,截取最终显示的字符
    console.log('filteredStr',filteredStr)
    console.log('cut-str',textRef.current.value.substring(0,20-ChiLength))
    deviceNameInput.value=textRef.current.value.substring(0,20-ChiLength);
    if(strLength>=20){
        var newMaxLength=maxLength;
        deviceNameInput.setAttribute("maxlength",newMaxLength);
    }else{
        deviceNameInput.setAttribute("maxlength",20);
    }
  }
  function getFinalStr(){
    console.log('is-entering')
    // setStr(textRef.current.value)
    textDiv.current.textContent = textRef.current.value
   
    if (!inputLock) {
      limitWords()
    }
  }

 


 
  return (
    <div className='inputWrapper' ref={textWrapper}>
      <div className='titleDiv' ref={textDiv}></div>
      {/* <pre style={{margin:0}}><span className='titleDiv' ref={textDiv}></span><br/></pre> */}
      <textarea
        // value={str}
        maxLength={20}
        className='title'
        ref={textRef}
        onKeyDown={handleKeyDown}
        // onChange={limitWords}
        onInput={getFinalStr}
        onCompositionStart={()=>{setInutLock(true)}}
        onCompositionEnd={()=>{setInutLock(false);limitWords()}}
      />
    </div>
  );
}
export default Title2;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值