删除表单内字符串两端的空格 - React

Remove white spaces from both ends of a string inside a form - React

我有一个接收名字和姓氏的简单表单,我需要删除字符串开头和结尾的白色space。我可以用 .trim() 方法来做到这一点,但是因为是一种形式,它不允许我写一个有两个单词的名字,比如 Ana Maria,因为它不允许我按 space 键在单词的末尾。我怎样才能去掉字符串两边的白色 space 但仍然让我按 space 并写一个以上的字?

这是组件:

export default function ScheduleInput(
  { label, required, onChange, value, ...extraProps },
) {
  return (
    <div className="item_container">
      {label}:
      {required &&
        <span style={{ color: 'red' }}> *</span>
      }
      <br />
      <input
        type="text"
        onChange={onChange}
        value={value.trim()}
        {...extraProps}
      />
    </div>
  );
}

不要修剪 onChange,而是在 onBlur 回调中执行此操作:

<input onBlur={this.props.formatInput} {...} />

可能看起来像这样的地方:

formatInput = (event) => {
  const attribute = event.target.getAttribute('name')
  this.setState({ [attribute]: event.target.value.trim() })
}