Enabling dropdowns according to selection in Material UI

kennechu

I have 3 dropdown components with Material UI, what I want to do is to disable the second and third components and enable them after choosing an option from the previous dropdown. For example, the second dropdown will enable after selecting something from the first dropdown, and the third will enable when I select an option from the second and so on.

This is my code:

import React from 'react';
import DropDownMenu from 'material-ui/DropDownMenu';
import MenuItem from 'material-ui/MenuItem';

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


export default class CreateLinksave extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      startDate: '',
      endDate: '',
      DivisionData: [],
      StoreGroupingData: [],
      OfferTypeData: [],
      DivisionState: '',
      OfferTypeState: '',
      StoreGroupingState: ''
    };
    this.handleChange = this.handleChange.bind(this);
    this.renderStoreGroupingOptions = this.renderStoreGroupingOptions.bind(this);
    this.renderDivisionOptions = this.renderDivisionOptions.bind(this);
    this.renderOfferTypeOptions = this.renderOfferTypeOptions.bind(this);
    this.handleChangeDivision = this.handleChangeDivision.bind(this);
    this.handleChangeStoreGrouping = this.handleChangeStoreGrouping.bind(this);
    this.handleChangeDiscountType = this.handleChangeDiscountType.bind(this);
  }

  componentDidMount() {
    const divisionWS = 'http://localhost:8080/services/Divisions/getAll';
    const offerTypeWS = 'http://localhost:8080/services/OfferType/getAll';
    const storeGroupingWS = 'http://localhost:8080/services/Rules/getRuleTextDtoQuery/Y/ENG';

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

        this.setState({
          DivisionData: findResponse,
          DivisionState: findResponse[0].divDeptShrtDesc
        });
      });

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

        this.setState({
          StoreGroupingData: findResponse,
          StoreGroupingState: findResponse[0].ruleDesc
        });
      });

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

        this.setState({
          OfferTypeData: findResponse,
          OfferTypeState: findResponse[0].offerTypeDesc
        });
      });
  }

  handleChange(event, index, value) {this.setState({value});}

  handleChangeDivision(event, index, value) {
    this.setState({ DivisionState: (value) });
  }

  handleChangeStoreGrouping(event, index, value) {
    this.setState({ StoreGroupingState: (value) });
  }

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

  renderDivisionOptions() {
    return this.state.DivisionData.map((dt, i) => {
      return (
        <MenuItem
          key={i}
          value={dt.divDeptShrtDesc}
          primaryText={dt.divDeptShrtDesc} />
      );
    });
  }

  renderStoreGroupingOptions() {
    return this.state.StoreGroupingData.map((dt, i) => {
      return (
        <MenuItem
          key={i}
          value={dt.ruleDesc}
          primaryText={dt.ruleDesc} />
      );
    });
  }

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

  render() {

    return (
      <div className={cr.container}>
        <div className={cr.rows}>
          <div>
            <DropDownMenu
              value={this.state.DivisionState}
              onChange={this.handleChangeDivision}>
              {this.renderDivisionOptions()}
            </DropDownMenu>
            <br/>
            <DropDownMenu
              value={this.state.StoreGroupingState}
              onChange={this.handleChangeStoreGrouping}>
              {this.renderStoreGroupingOptions()}
            </DropDownMenu>
            <br/>
            <DropDownMenu
              value={this.state.OfferTypeState}
              onChange={this.handleChangeDiscountType}>
              {this.renderOfferTypeOptions()}
            </DropDownMenu>
            <br/>
          </div>
        </div>
      </div>
    );
  }
}

Also one more thing, now when I fetch the result coming from the WS I displaying the data in position 0, what I want to change is to have like a default option instead of the 0 position.

Some help will be nice.

Regards.

Kareem Elbahrawy

Start by not setting DivisionState, StoreGroupingState, OfferTypeState on fetch successful, for example

this.setState({
    DivisionData: findResponse,
    // DivisionState: findResponse[0].divDeptShrtDesc <= Remove this 
});

Next, In every dropdown render a default option, for example

<DropDownMenu
    value={this.state.DivisionState}
    onChange={this.handleChangeDivision}>
    <MenuItem value={''} primaryText={'Select option'} />
    {this.renderDivisionOptions()}
</DropDownMenu>

Last thing set disabled property to true if the previous is not set

    <DropDownMenu
        value={this.state.DivisionState}
        onChange={this.handleChangeDivision}>
        <MenuItem value={''} primaryText={'Select option'} />
        {this.renderDivisionOptions()}
    </DropDownMenu>
    <br/>
    <DropDownMenu
        disabled={!this.state.DivisionState}
        value={this.state.StoreGroupingState}
        onChange={this.handleChangeStoreGrouping}>
        <MenuItem value={''} primaryText={'Select option'} />
        {this.renderStoreGroupingOptions()}
    </DropDownMenu>
    <br/>
    <DropDownMenu
        disabled={!this.state.StoreGroupingState}
        value={this.state.OfferTypeState}
        onChange={this.handleChangeDiscountType}>
        <MenuItem value={''} primaryText={'Select option'} />
        {this.renderOfferTypeOptions()}
    </DropDownMenu>
    <br/>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to add regex patterns to input based on selection of dropdowns in angular material

Creating dropdowns according to the database

Material UI Select Multiple Selection in Array

material-ui Select Box not showing selection

Dialog Overlap in Date Selection Box, Material UI

Radio icon disappears on selection - using Material UI

How to display selection according to the condition ? (element-ui table)

How to change props in a Material UI component according to URL?

How to change Material UI component property according to props

Tabs with dropdowns angular ui

Update multiple dropdowns with user selection

Bootstrap Dropdown selection is setting selection on *all* dropdowns

Can I initialize the checkbox selection in a material-ui DataGrid?

Get row item on checkbox Selection in React Material-UI DataGrid

Material UI Autocomplete - Disable listbox after the item selection

TextField in Material UI won't display label after selection

Redux-form Material-UI dropdown not showing dropdown, selection

How to set Radio button selection programatically in react material-ui

Angular/material ui - programatically select mat-selection-list

React Js - Material UI - Change Min Date as per previous selection

Create custom Country-State selection with React + Material UI

Material-UI: Disable multiple row selection in DataGrid

Enabling Text selection in Extjs tab

Obtain selection start, end, and selection value from Material UI's TextField

Disabling or Enabling Radio Buttons According To Groups

How to generete charts according to the user selection using Modern UI (Metro) Charts?

Angular 7 - Multiple Dropdowns Selection Change

Datatables with column filter dropdowns and multiple checkbox selection

Set Hidden Value Depending on Selection of two Dropdowns