代码之家  ›  专栏  ›  技术社区  ›  Debopriyo Basu

通过单击ReactJS中的材质UI浮动操作按钮切换Dialogflow聊天窗口

  •  0
  • Debopriyo Basu  · 技术社区  · 8 年前

    在下面的代码片段中,我尝试切换 <ChatWidget /> ,这是一个Dialogflow聊天窗口,当用户单击 <FloatingActionButton/> 默认状态设置为 false . 单击“材质UI浮动操作”按钮时,不会将状态切换为 true . 我使用的是UI v1.0.0-beta34材质。 以下是代码片段:

    import React, { Component } from 'react';
    import ChatWidget from './ChatWidget';
    import FloatingActionButton from './Fab';
    class ChatComponent extends Component {
    constructor( props ){
      super( props )
      this.state = { show : false };
      this.toggleDiv = this.toggleDiv.bind(this)
     }
    toggleDiv = () => {
      const { show } = this.state;
      this.setState( { show : !show } )
    }
    render() {
      return (
      <div>
        <div>
          { this.state.show && <ChatWidget /> }
        </div>
      <FloatingActionButton onClick={ this.toggleDiv } />
      </div>
       );
      }
     }
     export default ChatComponent;
    

    未单击晶圆厂的页面如下所示:

    default state

    单击FAB时所需的功能如下所示:

    desired functionality on click of FAB when state is toggled to true

    有关切换的正确方式的任何帮助或建议 <聊天工具/> 单击 <FloatingActionButton /> 非常感谢。

    1 回复  |  直到 8 年前
        1
  •  2
  •   Debopriyo Basu    8 年前

    我找到了解决办法。问题是 <FloatingActionButton /> 是我制作的自定义组件。如果必须使FloatingActionButton响应单击,则必须在材质UI中添加OnClick属性 <Button/> 组件本身。 有效的修改代码:

    import React, { Component } from 'react';
    import ChatWidget from './ChatWidget';
    import Button from 'material-ui/Button';
    const style = {
     margin: 0,
     top: 'auto',
     right: 20,
     bottom: 20,
     left: 'auto',
     position: 'fixed',
     backgroundColor:'#E65100',
     color:'#FFFFFF',
    };
    
      class ChatComponent extends Component {
       constructor( props ){
        super( props )
        this.state = { show : false };
    
        this.toggleDiv = this.toggleDiv.bind(this)
       }
    
       toggleDiv = () => {
        const { show } = this.state;
        this.setState( { show : !show } )
      }
      render() {
        return (
        <div>
          <div>
           { this.state.show && <ChatWidget /> }
          </div>
         <Button variant="fab" aria-label="add" style={style} onClick={ 
          this.toggleDiv } >
        <i class="fas fa-comment"></i>
         </Button>
         </div>
         );
       }
      }
      export default ChatComponent;