0

I have the following CustomInput component:

import React from 'react';

const CustomInput = props => (
  <div className="form-group">
    <label className="form-label">{props.title}</label>
    <input
      className="form-input"
      name={props.name}
      type={props.inputType}
      value={props.content}
      onChange={props.controlFunc}
      placeholder={props.placeholder}
    />
  </div>
);

CustomInput.propTypes = {
  inputType: React.PropTypes.oneOf(['text', 'number']).isRequired,
  title: React.PropTypes.string.isRequired,
  name: React.PropTypes.string.isRequired,
  controlFunc: React.PropTypes.func.isRequired,
  content: React.PropTypes.oneOfType([
    React.PropTypes.string,
    React.PropTypes.number,
  ]).isRequired,
  placeholder: React.PropTypes.string,
};

export default CustomInput;

And this is my form:

import React, { PropTypes } from 'react';
import { Field, reduxForm } from 'redux-form';
import CustomInput from '../components/CustomInput';

const renderMyStrangeInput = field => (
  <CustomInput
    inputType={'number'}
    title={'How many items do you currently own?'}
    name={'currentItems'}
    controlFunc={param => field.input.onChange(param.val)}
    content={{ val: field.input.value }}
    placeholder={'Number of items'}
  />
);

class ItemsForm extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    const { handleSubmit } = this.props;
    return (
      <form className="container" onSubmit={handleSubmit}>
        <h1>Nuevo Pedido</h1>
        <Field name="myField" component={renderMyStrangeInput} />
        <div className="form-group">
          <button type="submit" className="btn btn-primary input-group-btn">Submit</button>
        </div>
      </form>
    );
  }
}

ItemsForm.propTypes = {
};

ItemsForm = reduxForm({
  form: 'Items',
})(ItemsForm);

export default ItemsForm;

I can render my component, but there are some issues with the content type. First, if I set the CustomInput to accepts numbers I get:

he specified value "[object Object]" is not a valid number. The value must match to the following regular expression: -?(\d+|\d+\.\d+|\.\d+)([eE][-+]?\d+)?

Second, if I set the inputType to text, it renders a:

[object Object]

so, the console is giving the following warning:

Warning: Failed prop type: Invalid prop `content` supplied to `CustomInput`.

How can I set the content properly?

2 Answers 2

6

I think the issue is that you are trying pass strings as objects

<CustomInput
    inputType="number"
    title="How many items do you currently own?"
    name="currentItems"
    controlFunc={param => field.input.onChange(param.val)}
    content={field.input.value}
    placeholder="Number of items"
  />
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, but the problem was with the content. I can send object via the rest of the props, but I needed to pass just the field.input.value. I am trying to lead with this custom components for redux-form, and it is not clear for me how to pass the content and controlFunc, i.e now I changed the prop sent to content and the warning is solved, but I cannot change the value of the input :( maybe the param needs to be passed in other way
First thing I have noticed were Strings and I focused on them, but yes also content property seems to accept not object but String or Number :)) good luck with further coding!
{param => field.input.onChange(param.val)} this didn't worked for me, but {param => field.input.onChange(param.target.value)} went fine. Maybe it's because of the newer version?
1

You are passing object via props and you must pass string or number.

content={{ val: field.input.value }} // no!
content={field.input.value} // yes! :)

3 Comments

Yeah! it solved the warning and problem. But, now I cannot change the value inside the input, do you know why?
I was first :( :DD
@FacundoGFlores the "controlled input" is the word for You :) link. When You pass value to input by props object, you must give onChange method to input to change that passing object. Solution 1: rename "value" to "defaultValue", Solution2: read about controlled input :)

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.