如何获得字符数限制,包括默认名称-Reactjs

How to get character count limitation including default name-Reactjs

我正在尝试获取输入框的字符限制,但输入字段有默认值。

如果字符限制超过文本必须出现在 0 限制(不弹出)

如果我使用下面的代码,它不能很好地满足我的要求(假设默认值已经存在)。

我能知道如何包含默认值并在它达到 0 时设置消息吗

class Company extends React.Component {
state = {
      Title: this.props.selectedTable.Title,
      chars_left: 50,
      max_char:50
    }

   handleCharacterCount= (event) => {
    const charCount = event.target.value.length;
    const maxChar = this.state.max_char;
    const charLength = maxChar - charCount;
    this.setState({ chars_left: charLength });
    //this.setState({sTitle: e.target.value});
   }
   render() {
    return (
     <div>
      <textArea
        type="text"
        maxLength="50"
        required
        onChange={this.handleCharacterCount}
        value={this.state.Title}/>
      />
      <p>{this.state.chars_left}</p>
    </div>
  )
 }
}

你可以试试这个

state = {
  Title: this.props.selectedTable.Title,
  chars_left: 50 - this.props.selectedTable.Title.length,
  max_char:50
}



handleCharacterCount= (event) => {
const charCount = event.target.value.length;
const maxChar = this.state.max_char;
const charLength = maxChar - charCount;
this.setState({ chars_left: charLength,Title: event.target.value })}



render() {
return (
 <div>
  <textArea
    type="text"
    maxLength="50"
    required
    onChange={this.handleCharacterCount}
    value={this.state.Title}/>
  />
  {
    !this.state.chars_left &&
      <p>text has to appear at 0 limit</p>
  }
  <p>{this.state.chars_left}</p>
</div>)}