2017-09-10 7 views
1

React Semantic-UIのコンポーネントからRedux Formを使用してドロップダウンメニューの値を取得しようとしています。私は正常なテキスト入力から値を正常に受け取ったが、選択された入力は受け取れなかった。ReduxForm - Semantic-UI select入力から選択値を取得する

import React, { Component } from 'react'; 
import { Field, reduxForm } from 'redux-form'; 
import { Button, Header, Icon, Modal, Transition, Form, Input, Select, TextArea, Dropdown } from 'semantic-ui-react'; 
import '../../index.css'; 

const status = [ 
    { key: 'Online', text: 'Online', value: 'Online' }, 
    { key: 'Offline', text: 'Offline', value: 'Offline' }, 
]; 
class NewInfoForm extends Component { 

    Status({ input, meta: {touched, error}, ...custom }) { 
    console.log({...custom}) 
    return (
     <Form.Field control={Select} name="Status" label="Status" options={status} placeholder="Status" {...input} {...custom} /> 
    ); 
    } 

    submit(values, dispatch) { 
    console.log(values) 
    } 
    render() { 
    const { handleSubmit } = this.props; 
    return (
     <Transition animation="fade"> 
     <Modal trigger={<a className="cursor-pointer"> <Icon name="add" /> </a>} closeIcon> 
      <Header content='New Info'/> 
      <Modal.Content> 
      <Form onSubmit={handleSubmit(this.submit.bind(this))}> 
      <h3 className="ui dividing header">Basic Information</h3> 
       <Form.Group> 
       <Field name="Status" component={this.Status} /> 
       </Form.Group> 
       <Button type="submit" content='Submit Info' icon='add' labelPosition='right' color="green" /> 
      </Form> 
      </Modal.Content> 
      <Modal.Actions> 
      </Modal.Actions> 
     </Modal> 
     </Transition> 
    ); 
    } 
} 

export default reduxForm({ 
    form: 'NewInfo' 
})(NewInfoForm); 

フォームを送信するたびに、ドロップダウン値の1つを選択しても常に空のオブジェクトとして返されます。

+0

これを解決する運がまだありませんか? – jonahe

答えて

1

Selectコンポーネントに、Reduxフォームの小道具({...input})で何をすべきかを理解するために、もっと手作業で作業する必要があると思われます。したがってたとえばの場合、onChangesignatureSelectと1つのonChangeのRedux形式の署名の間の「翻訳」を行う必要があります。

Status({ input, meta: {touched, error}, ...custom }) { 
    const reduxFormOnChange = input.onChange; 

    // the signature for this onChange is specific to 
    // semantic-ui Select 
    const semanticSelectOnChange = (maybe, wrong, order, or, irrelevant) => { 
     // here we make the translation 
     // so that the Redux form onChange gets 
     // called correctly. 
     // (NOTE: this is an example, not actual code suggestion) 
     reduxFormOnChange(or, maybe); 
    }; 

    return (
     <Form.Field control={Select} 
     name="Status" label="Status" 
     options={status} placeholder="Status" 
     {...input} {...custom} 
     onChange={ semanticSelectOnChange } 
    /> 
    ); 
    } 

他のプロパティについても同様のプロセスが必要な場合があります。もちろん、Redux devtools (for Firefox or Chrome)のようなツールを使って、Reduxフォームが送出するアクションに期待するデータが含まれているかどうかを確認することもできます。

+0

私はセマンティックUIの値を取得し、その情報をreduxFormに取得する方法についてはあまりよく分かりません。 – DarkTakua

+0

私の例を試して、 'semanticSelectOnChange'関数の中に' console.log(多分、間違った、順序、または無関係な) 'を置いてください。次に、Select(semantic-uiから)のonChange関数が呼び出されているかどうかを確認することができます。どのように多くの引数などで呼び出されているかを見ることができます。最初の2つの引数には、おそらくRedux-Formに送信する値が含まれていますが、関連する部分だけを選択する必要があります。 – jonahe

+0

Redux-Formフィールドの 'onChange'関数について読んでみると、どのような値が期待されているのか分かります。しかし、(ドキュメント)[https://redux-form.com/7.0.4/docs/api/field.md/]に注意してください。なぜなら、あなたがあなたと同じバージョンを見ているのを見る必要があるからです。使っている。未知数が多すぎる場合は、基本に戻る必要があります。最初にRedux Form(semantic-uiなし)の詳細を理解する必要があります。 – jonahe

関連する問題