【问题标题】:ReduxForm - getting select value from Semantic-UI select inputReduxForm - 从 Semantic-UI 选择输入中获取选择值
【发布时间】:2017-09-10 09:17:50
【问题描述】:

我正在尝试使用 Redux Form 从 React Semantic-UI 中的组件获取下拉菜单的值。我已经成功接收到来自普通文本输入的值,但没有选择输入。

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);

每当我提交表单时,即使我选择了其中一个下拉值,它也始终以空对象的形式返回。

【问题讨论】:

  • 解决这个问题有什么好运气吗?

标签: reactjs redux-form semantic-ui-react


【解决方案1】:

我怀疑您可能需要做更多的手动工作才能使 Select 组件了解它应该如何处理您传递给它的 Redux 表单道具 ({...input})。所以例如,您可能需要在SelectonChange signature 和Redux 表单的onChange 签名之间进行一些“翻译”。

  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 表单调度的操作是否包含您期望的数据。

【讨论】:

  • 我不太清楚如何获取语义 ui select 的值,然后将该信息获取到 reduxForm。
  • 如果您尝试我的示例并将console.log(maybe, wrong, order, or, irrelevant) 放在semanticSelectOnChange 函数中。然后,您将能够看到是否正在调用 Select(来自语义 UI)的 onChange 函数。您还可以看到它是如何被调用的(有多少参数等)。前两个参数可能包含您想要发送到 Redux-Form 的值,但您可能只需要挑选相关部分。
  • 如果您阅读了有关 Redux-Form 字段的 onChange 函数,您可以看到它期望的值类型。但是要小心(文档)[redux-form.com/7.0.4/docs/api/field.md/],因为您需要看到您正在查看与您正在使用的版本相同的版本。如果未知数太多,您可能需要回归基础。您可能首先需要了解更多有关 Redux Form(没有语义 UI)的信息。
  • 然后您可以了解如何制作您自己的非常简单的自定义字段,以便您学习使用 Redux Form。那么也许你需要学会自己使用 Semantic-ui。 (至少是一个简单的例子)。然后您将了解更多关于如何将它们组合在一起的信息。
  • 啊,好吧,我得到了表单选择的值。现在我只是对如何将该值传递给 redux 表单感到困惑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-23
  • 2019-08-30
  • 2018-09-19
  • 2021-06-29
  • 2015-10-19
相关资源
最近更新 更多