【问题标题】:close request is not working on dialog box关闭请求在对话框中不起作用
【发布时间】:2017-02-16 11:40:03
【问题描述】:

单击图标时,应出现一个带有表单的对话框,用于添加选项卡或删除特定选项卡。我在组件中使用了 reactjs、redux 和 material-ui。 我可以在单击图标时显示对话框,但是当我单击取消按钮时,对话框不会关闭。

我应该怎么做才能解决它?

这是我的代码

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 />
      </div>
    );

TabDialog.js

class TabDialog extends Component {
      constructor(props) {
        super(props);
        this.state = {
                      open: false,
                    };
      }

      renderAddTab() {
        const actions = [
          <FlatButton
            label="Cancel"
            primary
            onTouchTap={this.props.createTab.close}
          />,
          <FlatButton
            label="Add Tab"
            primary
            keyboardFocused
            onTouchTap={this.props.createTab.close}
          />,
        ];
        return (
          <div className="device-action">
            <Dialog
                title="Add a Tab"
                actions={actions}
                modal={false}
                open={this.props.createTab.open}
                onRequestClose={this.props.createTab.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.createTab.close}
          />,
          <FlatButton
            label="Delete"
            primary
            keyboardFocused
            onTouchTap={this.props.createTab.close}
          />,
        ];
        return (
        <div className="tab-action">
          <Dialog
              title="Delete"
              actions={actions}
              modal={false}
              open={this.props.createTab.open}
              onRequestClose={this.props.createTab.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 部分

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

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

open props 接受 boolean 和 onRequestClose 和 onTouchTap props 接受函数。

为什么我的代码不起作用?我该如何克服这个问题?

【问题讨论】:

    标签: javascript reactjs redux react-redux material-ui


    【解决方案1】:

    DialogonRequestClose 属性采用 function 而不是 boolean。因此,您需要在该函数内将this.props.createTab.open 的值更改为false。这样,redux 将更改状态,open 的值将作为false 传递。这将关闭对话框。

    【讨论】:

    • 你的意思是说我在 selectTabIcon 函数中将 open 的值更改为 false 吗?
    • @milan 是的,将值更改为 false
    • 我把open的值从true改成了false,没有改变close的值。然后我在打开的道具上传递了 this.props.createTab.open,在 onRequestClose 道具上传递了 this.props.createTab.close。但是要打开一个对话框,打开的 props 应该作为一个值获得 true。
    • 你能解释一下吗?它不是那样工作的。
    【解决方案2】:

    我找到了解决方案。 onRequestClose 和 onTouchTap 都接受函数。他们需要开放才能虚假。我失败的原因是我将错误值传递给关闭标志而不是打开。

    现在我创建了一个新的动作创建者,我通过将打开的有效负载设置为 false 来传递一个动作来关闭它

    export function closeTabIcon() {
      return {
        type: 'CLOSE_ICON',
        open: false
      }
    }
    
    reducer will be something like this
    
       case 'CLOSE_ICON':
         return action.open
    

    现在在 OnRequestProps 中,我通过了 this.props.closetabIcon。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      相关资源
      最近更新 更多