使用材料ui v1.0 beta 26的下拉组件出错

时间:2018-01-02 21:20:34

标签: javascript reactjs material-ui

我正在使用Material UI v1.0 beta.26而且我遇到了下拉组件的问题,在这个新版本中你必须使用Select组件与MenuItem结合。

我的下拉列表是在应用程序渲染时填充的,但当我从中选择任何选项时,我收到以下错误:

enter image description here

这是我的代码:

import React from 'react';
import Select from 'material-ui/Select';
import {MenuItem, MenuIcon} from 'material-ui/Menu';

//CONSTANTS
import {CREATE_LS_DISCOUNT_TYPE_DD} from './commons/constants';
import {CREATE_LS_OFFER_TYPE_DD} from './commons/constants';

import cr from '../styles/general.css';


export default class ExampleDropDown extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      DiscountTypeData: [],
      OfferTypeData: [],
      DiscountTypeState: '',
      OfferTypeState: ''
    };

    this.renderDiscountTypeOptions = this.renderDiscountTypeOptions.bind(this);
    this.renderOfferTypeOptions = this.renderOfferTypeOptions.bind(this);

    this.handleChangeDiscountType = this.handleChangeDiscountType.bind(this);
    this.handleChangeOfferType = this.handleChangeOfferType.bind(this);

  }

  componentDidMount() {

    fetch(CREATE_LS_DISCOUNT_TYPE_DD)
      .then(Response => Response.json())
      .then(findResponse => {
        console.log(findResponse);

        this.setState({
          DiscountTypeData: findResponse.discountTypes,
        });
      });
  }

  handleChangeDiscountType(event, index, value) {
    this.setState({ DiscountTypeState: (value)});

    fetch(CREATE_LS_OFFER_TYPE_DD)
      .then(Response => Response.json())
      .then(findResponse => {
        console.log(findResponse);

        this.setState({
          OfferTypeData: findResponse.offerTypes
        });
      });
  }

  handleChangeOfferType(event, index, value) {
    this.setState({ OfferTypeState: event.target.value });
  }

  renderDiscountTypeOptions() {
    return this.state.DiscountTypeData.map((dt) => {
      return (
        <MenuItem
          key={dt.id}
          value={dt.text}>
          {dt.text}
        </MenuItem>
      );
    });
  }

  renderOfferTypeOptions() {
    return this.state.OfferTypeData.map((dt) => {
      return (
        <MenuItem
          key={dt.offerTypeCode}
          value={dt.offerTypeDesc}>
          {dt.offerTypeDesc}
        </MenuItem>
      );
    });
  }

  render() {

    return (

      <div className={cr.container}>
        <div>
          <Select

            value={this.state.DiscountTypeState}
            onChange={this.handleChangeDiscountType}>
            {this.renderDiscountTypeOptions()}

          </Select>
        </div>
        <br/>
        <div>
          <Select

            value={this.state.OfferTypeState}
            onChange={this.handleChangeOfferType}>
            {this.renderOfferTypeOptions()}

          </Select>
        </div>
      </div>

    );
  }

}

在下面的方法( handleChangeDiscountType )中,如果我这样离开它“this.setState({DiscountTypeState:value})”我在上面的屏幕截图中得到了错误但是如果我改变那条线就像这个“this.setState({DiscountTypeState:event.target.value})它有效,所以我想了解原因

  handleChangeDiscountType(event, index, value) {
    this.setState({ DiscountTypeState: value});

    fetch(CREATE_LS_OFFER_TYPE_DD + 1)
      .then(Response => Response.json())
      .then(findResponse => {
        console.log(findResponse);

        this.setState({
          OfferTypeData: findResponse.offerTypes
        });
      });
  }

我想要做的是获取我的选择索引,以便将其传递给我的第二个Web服务调用,但我不知道如何做到这一点,在之前版本的Material UI中我只是把“索引“并且工作但在新版本中不起作用,所以我想知道添加该参数的新方法。

fetch(CREATE_LS_OFFER_TYPE_DD + PASS INDEX HERE)
  .then(Response => Response.json())
  .then(findResponse => {
    console.log(findResponse);

    this.setState({
      OfferTypeData: findResponse.offerTypes
    });
  });

我会感激任何帮助......

1 个答案:

答案 0 :(得分:1)

使用onChangeSelect丰富的目标调用value提供给name的处理程序,因此您需要从event.target中提取值:

handleChangeDiscountType(event) {
  const {
    DiscountTypeData
  } = this.state;

  // you're using the text property as the value, but you should probably use its id
  // still, here's how you'd find the item using the selected item's value
  const selectedDiscount = DiscountTypeData.filter(
    discount => discount.text === event.target.value,
  );

  // use a templated literal to specify the endpoint with the selected item's id
  fetch(`${CREATE_LS_OFFER_TYPE_DD}/${selectedDiscount.id}`)
    .then(Response => Response.json())
    .then(findResponse => {
      console.log(findResponse);

      this.setState({
        OfferTypeData: findResponse.offerTypes,
      });
    });
}

您的代码无效的原因是因为onChange未使用第三个参数调用,因此您使用value将状态设置为undefined

有关详细信息,请参阅Selects demo