Normalize redux-form field value onBlur

Viewed 4626

How can I normalize the value of the redux-form field when onBlur is triggered. I tried the following, but it didn't seem to work:

const normalizeAmount = (node) => {
  const newValue = node.target.value;

  return `--${newValue}--`;
};

render() {
    const { handleSubmit, pristine, invalid, submitting, error, blur } = this.props;

return (
      <form onSubmit={handleSubmit(submit)}>
        <div name="form-container">
          <Field
            name="foo"
            component={CustomInput}
            onBlur={blurValue => blur(normalizeValue(blurValue))}
          /> 
          ...
);
2 Answers

actually normalize is used before any changing in field so it's used before onBlur event, but you are trying to use it in wrong way.

You can use something like this and to allow user to enter only numbers:

<Field component={TextInputField}                                     
   onBlur={value => this.doWhatEverYouNeed(value)}
   normalize={value => value.replace(/[^\d]/g, '')}
/>

More details about normalize you can find on https://redux-form.com/8.2.2/examples/normalizing/

Related