0
votes

When an icon is clicked, a dialog box with form should appear to either add a tab or delete specific tab. I have used reactjs, redux and material-ui for components. I could show the dialog box when icon is clicked but i get an error of

Invalid prop open of type function supplied to Dialog, expected boolean in dialog

The dialog gets open with the above error and when i click close button nothing happens.

What should i do to resolve it?

Here is my code

App.js

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
                  max_char: 32,
                  open: false,
                };
    this.handleChange = this.handleChange.bind(this);
    this.handleLogout = this.handleLogout.bind(this);
  }

  handleClose = () => this.setState({ open: false });

  handleOpen = () => this.setState({ open: true });

  render() {
      return (
            <div>
                <Header
                      tab={this.state.tabs}
                      open={this.state.open}
                      handleClose={this.handleClose}
                      handleToggle={this.handleToggle}
                />
                <Content
                    handleOpen={this.handleOpen}
                    handleClose={this.handleClose}
                />
            </div>
      );
  }
}

Header.js

class Header extends Component {
  render() {
    const tabs = _.map(this.props.tab, (tab) =>
         <span className="tab" key={tab.id}><a href="">{tab.description}</a></span>
    );

    return (
      <div>
        <AppBar
            title={tabs}
            iconElementRight={navigation}
            onLeftIconButtonTouchTap={this.props.handleToggle}
            style={{ background: '#fff' }}
        />
      </div>
    );
  }
}

function mapStateToProps(state) {
  return {
    iconList: state.iconList
  };
}

function mapDispatchToProps(dispatch) {
  return bindActionCreators({
    selectTabIcon
  }, dispatch);
}


const Content = (props) =>
    (
      <div className="content-section">
        <TabDialog
          open={props.handleOpen}
          close={props.handleClose}
        />
      </div>
    );

TabDialog.js

class TabDialog extends Component {

      renderAddTab() {
        const actions = [
          <FlatButton
            label="Cancel"
            primary
            onTouchTap={this.props.close}
          />,
          <FlatButton
            label="Add Tab"
            primary
            keyboardFocused
            onTouchTap={this.props.close}
          />,
        ];
        return (
          <div className="device-action">
            <Dialog
                title="Add a Tab"
                actions={actions}
                modal={false}
                open={this.props.open}
                onRequestClose={this.props.close}
            >
            <div className="tab-name">
            <TextField
              floatingLabelText="Name"
              floatingLabelStyle={{ color: '#1ab394' }}
              floatingLabelFocusStyle={{ color: '#1db4c2' }}
              underlineStyle={{ borderColor: '#1ab394' }}
            />
            </Dialog>
          </div>
      );
      }

      renderDeleteTab() {
        const actions = [
          <FlatButton
            label="Cancel"
            primary
            onTouchTap={this.props.close}
          />,
          <FlatButton
            label="Delete"
            primary
            keyboardFocused
            onTouchTap={this.props.close}
          />,
        ];
        return (
        <div className="tab-action">
          <Dialog
              title="Delete"
              actions={actions}
              modal={false}
              open={this.props.open}
              onRequestClose={this.props.close} />
        </div>
      );
      }


      render() {
        const iconSelected = this.props.createTab;
        if (!iconSelected) {
          return (<div>Select</div>);
        }
        if (iconSelected === '1') {
          return (this.renderDeleteTab());
        }
        if (iconSelected === '2') {
          return (this.renderAddTab());
        }
    }
    }

    function mapStateToProps(state) {
      return {
        iconList: state.iconList,
        createTab: state.createTab,
      };
    }

    function mapDispatchToProps(dispatch) {
      return bindActionCreators({
        addTab,
        selectTabIcon
      }, dispatch);
    }

redux part

export function selectTabIcon(selectTab) {
      console.log('selected', selectTab.target.id);
      return {
        type: 'TAB_ICON_SELECTED',
        payload: selectTab.target.id,
      };
    }

    switch (action.type) {
      case 'TAB_ICON_SELECTED':
        console.log('tab', action.payload);
        return action.payload;

UPDATE:

Dialog open props accepts boolean and onRequestClose, onTouchTap accepts function so i did the following but closing is not working now

changed my selectTabIcon function as

export function selectTabIcon(selectTab) {
  return {
    type: 'TAB_ICON_SELECTED',
    id: selectTab.target.id,
    open: true,
    close: () => false
  };
}

and in Dialog i passed this.props.createTab.close on onRequestClose and onTouchTap but no luck.

2

2 Answers

1
votes

open prop for Dialog should be a boolean value as per the Material ui docs http://www.material-ui.com/#/components/dialog.

So hope this snippet of your code helps you out:

class App extends Component {
      constructor(props) {
        super(props);
        this.state = {
          max_char: 32,
        };
        this.handleChange = this.handleChange.bind(this);
        this.handleLogout = this.handleLogout.bind(this);
      }

      render() {
        return (
          <div>
            <Header tab={this.state.tabs} />
            <Content />
          </div>
        );
      }
    }

class Header extends Component {
      render() {
        const tabs = _.map(this.props.tab, (tab) =>
          <span className="tab" key={tab.id}><a href="">{tab.description}</a></span>
        );

        return (
          <div>
            <AppBar
              title={tabs}
              iconElementRight={navigation}
              onLeftIconButtonTouchTap={this.props.handleToggle}
              style={{ background: '#fff' }}
            />
          </div>
        );
      }
    }
function mapStateToProps(state) {
      return {
        iconList: state.iconList
      };
    }

    function mapDispatchToProps(dispatch) {
      return bindActionCreators({
        selectTabIcon
      }, dispatch);
    }

class TabDialog extends Component {
      constructor(props) {
        super(props);

        this.state = {
          open: false
        }

        this.handleOpen = this.handleOpen.bind(this)
        this.handleClose = this.handleClose.bind(this)
      }

      handleOpen = () => {
        this.setState({ open: true });
      }

      handleClose = () => {
        this.setState({ open: false });
      }

      renderAddTab() {
        const actions = [
          <FlatButton
            label="Cancel"
            primary
            onTouchTap={this.handleClose}
          />,
          <FlatButton
            label="Add Tab"
            primary
            keyboardFocused
            onTouchTap={this.handleClose}
          />,
        ];
        return (
          <div className="device-action">
            <Dialog
              title="Add a Tab"
              actions={actions}
              modal={false}
              open={this.state.open}
              onRequestClose={this.handleClose}
            >
            <div className="tab-name">
            <TextField
              floatingLabelText="Name"
              floatingLabelStyle={{ color: '#1ab394' }}
              floatingLabelFocusStyle={{ color: '#1db4c2' }}
              underlineStyle={{ borderColor: '#1ab394' }}
            />
            </Dialog>
          </div>
      );
      }

      renderDeleteTab() {
        const actions = [
          <FlatButton
            label="Cancel"
            primary
            onTouchTap={this.handleClose}
          />,
          <FlatButton
            label="Delete"
            primary
            keyboardFocused
            onTouchTap={this.handleClose}
          />,
        ];
        return (
          <div className="tab-action">
            <Dialog
              title="Delete"
              actions={actions}
              modal={false}
              open={this.state.open}
              onRequestClose={this.handleClose} 
            />
          </div>
        );
      }


      render() {
        const iconSelected = this.props.createTab;
        if (!iconSelected) {
          return (<div>Select</div>);
        }
        if (iconSelected === '1') {
          return (this.renderDeleteTab());
        }
        if (iconSelected === '2') {
          return (this.renderAddTab());
        }
      }
    }

    function mapStateToProps(state) {
      return {
        iconList: state.iconList,
        createTab: state.createTab,
      };
    }

    function mapDispatchToProps(dispatch) {
      return bindActionCreators({
        addTab,
        selectTabIcon
      }, dispatch);
    }

I doubt the onTouchTap prop set on the FlatButton and also there is no connect method which connects the redux store to the component

0
votes

The "open" prop of Dialog is a bool. You need to pass the result of your "handleOpen" function, not a reference the function itself. Add the parens like so:

<Content handleOpen={this.handleOpen()}
        handleClose={this.handleClose()}
/>