提供给`Dialog`

时间:2016-10-06 11:28:31

标签: javascript reactjs redux material-ui

单击图标时,应显示带有表单的对话框,以添加选项卡或删除特定选项卡。我已经将reactjs,redux和material-ui用于组件。单击图标时我可以显示对话框,但是出现错误

  

预期提供给open的{​​{1}}类型的道具function无效   对话框中的Dialog

对话框因上述错误而打开,当我点击关闭按钮时没有任何反应。

我该怎么做才能解决它?

这是我的代码

App.js

boolean

Header.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>
      );
  }
}

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

redux部分

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

更新

Dialog open props接受boolean和onRequestClose,onTouchTap接受函数,所以我做了以下但是现在关闭不起作用

将我的selectTabIcon函数更改为

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;

在Dialog中我在onRequestClose和onTouchTap上传递了this.props.createTab.close,但没有运气。

2 个答案:

答案 0 :(得分:0)

对于Dialog,{p> open prop应该是一个布尔值,根据Material ui docs http://www.material-ui.com/#/components/dialog

希望您的代码片段可以帮助您:

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

我怀疑FlatButton上的onTouchTap prop设置,并且没有连接方法将redux存储连接到组件

答案 1 :(得分:-1)

&#34;打开&#34;对话的道具是一个布尔。你需要传递你的&#34; handleOpen&#34;的结果。功能,不是功能本身的参考。像这样添加parens:

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