Redux形式的Material-UI下拉列表不显示下拉列表,选择

时间:2019-03-13 10:10:36

标签: reactjs material-ui redux-form

我使用Material-ui选择repo:status,并将redux-form作为HOC。我已经创建了表单,并显示选择框,但选项未显示。

这是我的组件

@material-ui/core/Select

它显示如下 enter image description here 单击组合框选项时,如此处 enter image description here

我已经创建了一个codeandbox实例 https://codesandbox.io/s/l2pqoryvvl?fontsize=14

2 个答案:

答案 0 :(得分:1)

只需将MenuItems更改为

  <MenuItem value="1">value 1</MenuItem>
  <MenuItem value="2">value 2</MenuItem>
  <MenuItem value="3">value 3</MenuItem>

菜单项不采用主要文字作为道具。

Description of solution

答案 1 :(得分:0)

@ S.Haviv提到我何时更改

<MenuItem value="1" text="value 1" /><MenuItem value='1'>value 1</MenuItem>菜单项出现,但单击不成功。所以我不得不在Select字段中摆脱onclick处理程序 而且我还为组件添加了标签

这是完整组件的代码 https://codesandbox.io/s/l2pqoryvvl?fontsize=14

import React from "react";
import { Field, reduxForm } from "redux-form";

import SelectField from "@material-ui/core/Select";
import MenuItem from "@material-ui/core/MenuItem";
import InputLabel from '@material-ui/core/InputLabel';
import FormControl from '@material-ui/core/FormControl';

const renderSelectField = ({
  input,
  label,
  meta: { touched, error },
  children,
  ...custom
}) => (
    <FormControl>
      <InputLabel >{label}</InputLabel>
      <SelectField
        floatingLabelText={label}
        errorText={touched && error}
        {...input}
        children={children}
        {...custom}
        />
    </FormControl>

  );

const Form = props => {
  const { handleSubmit, pristine, reset, submitting } = props;
  return (
    <form onSubmit={handleSubmit}>
      <div>
        <Field
          name="value"
          component={renderSelectField}
          label="Choose a Value"
          value="1"
        >
          <MenuItem value="1">value 1 </MenuItem>
          <MenuItem value="2">value 2 </MenuItem>
          <MenuItem value="3">value 3 </MenuItem>
        </Field>
      </div>

      <div>
        <button type="submit" disabled={pristine || submitting}>
          Submit
        </button>
        <button type="button" disabled={pristine || submitting} onClick={reset}>
          Clear Values
        </button>
      </div>
    </form>
  );
};

export default reduxForm({
  form: "form" // a unique identifier for this form
})(Form);