2017-01-23 11 views
0

私は、次のCustomInputコンポーネントがあります。カスタム入力 - Reduxのフォーム

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; 

をそして、これは私のフォームです:私は私のコンポーネントをレンダリングすることができ

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; 

が、コンテンツ・タイプを持ついくつかの問題があります。私はCustomInputを設定した場合、まず、数字を受け入れるために私が手:

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+)? 

第二に、私はテキストにinputTypeを設定した場合、それはレンダリング:

[object Object] 

をので、コンソールは次の警告を与えています:

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

コンテンツを正しく設定するにはどうすればよいですか。

答えて

4

は、私は問題はあなたが小道具を経由して、オブジェクトを渡しているとあなたは文字列または数値を渡さなければなりません

<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" 
    /> 
+0

ありがとうございました。問題はコンテンツにありました。私はオブジェクトの残りの部分を介して送信することができますが、私は 'field.input.value'だけを渡す必要がありました。私はこのカスタムコンポーネントを 'redux-form'のために使いこなそうとしていますが、' content'と 'controlFunc'をどのように渡すべきかはわかりません。つまり、今度はpropを内容に変更して警告を解決しました。しかし、私は入力の値を変更することはできません:(多分paramは別の方法で渡す必要があります – FacundoGFlores

+1

私が気づいた最初のことはStringsと私はそれらに焦点を当てましたが、はいまたcontentプロパティはオブジェクトではなくStringまたはNumberを受け入れるようです: ))さらに幸運とコーディング! – Kejt

1

をオブジェクトとして使用すると、パス文字列をしようとしているということだと思います。

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

うん!それは警告と問題を解決しました。しかし、今私は入力内の値を変更することはできません、あなたはなぜ知っていますか? – FacundoGFlores

+0

私は最初です:(:DD – Kejt

+1

@FacundoGFlores "制御入力"はあなたのための単語です:) [link](https://facebook.github.io/react/docs/forms.html)。値を渡してpropsオブジェクトから入力するときは、渡すオブジェクトを変更するためにonChangeメソッドを入力する必要があります。 解決策1:「value」の名前を「defaultValue」に変更します。 解決策2:制御入力について読む –

関連する問題