3

I'm trying to show a 'tick' next to my input when it contains at least 1 character to show a required field as being valid. The issue I have is that currently when I console.log(textEntered) each time the field changes I receive an object containing all the typed letters instead of a complete string. If I typed in 'hello' for example the log looks this (ignore the undefined):

enter image description here

Here is my Redux Field Component:

handleInput = (textEntered) => {
  console.log(textEntered);
  this.setState({ textEntered }, () => {
    if (this.state.textEntered.length) {
      this.setState({ completed: true });
    } else {
      this.setState({ completed: false });
    }
  });
}

render() {
  return (
    <Field
      name={this.props.placeholderText}
      component={TextInput}
      onChange={this.handleInput}
    />
  );
}

const TextInput = (field) => {
  return (
    <div className="drill-creation-input">
      <input
        {...field.input}
        type="input"
        onChange={e => field.input.onChange(e.target.value)}
      />

      <label htmlFor={field.input.name}>
        <span>{field.input.name}</span>
      </label>
    </div>
  );
};

1 Answer 1

5

OnChange handler has the following signature (see docs)

onChange : (event, newValue, previousValue)

So to read the complete value, just define your handleInput like:

handleInput = (evnt, textEntered) => {
  console.log(textEntered);
  ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

I had the same problem and this solved it. Should be the accepted answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.