来自反应颜色的颜色选择器无法正常工作

Color picker from react-color doesn't work properly

我正在使用 react-color and this tutorial 来实现 ChromePicker。

我如何期待它的工作:

到目前为止它是这样工作的:

这是它的截图:

为什么ChromePicker这么烂?有办法解决吗?而且,我想仅在 ChromePicker 关闭后才将新颜色保存在 Company 上(似乎 onChangeComplete 不是由此触发的)

这是代码:

 import React from 'react';
    import { Button, Icon } from 'semantic-ui-react';
    import { ChromePicker } from 'react-color';
    import { connect } from 'react-redux';
    import { Creators } from '../../actions';
    
    
    class Banner extends React.PureComponent {
      constructor(props) {
        super(props);
        this.state = {
          displayColorPicker: false,
        };
      }
    
      handleClick = () => {
        this.setState({ displayColorPicker: true });
      };
    
      handleClose = () => {
        this.setState({ displayColorPicker: false });
      };
    
      handleChangeComplete = colors => {
        const {
          name,
          color,
        } = this.state;
        this.setState({ color: colors.hex });
        const { updateCompany } = this.props; // company is the entity from props that is updated
                                              // it contains 2 values, its name and its color
        updateCompany(this.props.company._id, {
          name,
          color,
        });
      };
    
    
    
      render() {
        this.props.color.color.color = this.state.color;
        const popover = { // this is not essential, it's some styling for the picker
          position: 'absolute',
          zIndex: '2',
        };
        const cover = { // same as for popover
          position: 'fixed',
          top: '0px',
          right: '0px',
          bottom: '0px',
          left: '0px',
        };
        const {company } = this.props; // gets the company from props
        return (
          <div className="banner-container settings-banner">   
     
      //the below div with its style is updating in real time when the color is changed 
                  <div style={{ backgroundColor: this.state.color }}>              
                    <div>
                      <Button
                        className="select-color-btn"
                        onClick={this.handleClick}>
                        Select a color
                      </Button>
                      {this.state.displayColorPicker ? (
                        <div style={popover}>
                          <div
                            style={cover}
                            onClick={this.handleClose}
                            onKeyDown={this.handleClick}
                            role="button"
                            tabIndex="0"
                            aria-label="Save"
                          />
                          <ChromePicker
                            color={this.props.company.color}
                            onChangeComplete={this.handleChangeComplete}
                          />
                        </div>
                      ) : null}
                    </div>
                  </div>
            
          </div>
        );
      }
    }
    
    const mapStateToProps = state => ({
      company: state.companies.selectedCompany,
    });
    
    const mapDispatchToProps = {
      updateCompany: Creators.updateCompanyRequest,
    };
    
    export default connect(mapStateToProps, mapDispatchToProps)(Banner);

为了使 ChromePicker 正常工作(移动色相条和“大方块”内的圆圈,必须添加 onChange 功能并将其粘贴到 onHandleChange

此外,在 onHandleChangeComplete 中,颜色不能从状态中获取,而是从函数的参数中获取,colors:

从 'react' 导入 React; 从 'semantic-ui-react' 导入 { 按钮,图标 }; 从 'react-color' 导入 { ChromePicker }; 从 'react-redux' 导入 { 连接 }; import { Creators } from '../../actions';

class Banner extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      displayColorPicker: false,
    };
  }

  handleClick = () => {
    this.setState({ displayColorPicker: true });
  };

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

  handleChange2 = colors => {
    this.setState({ background: colors.hex });
 };

  handleChangeComplete = colors => {
    this.setState({ background: colors.hex });
    const {
      name,
    } = this.state;
    const color = colors.hex;
    this.setState({ color: colors.hex });
    const { updateCompany } = this.props;                                           
    updateCompany(this.props.company._id, {
      name,
      color,
    });
  };



  render() {
    this.props.color.color.color = this.state.color;
    const popover = { // this is not essential, it's some styling for the picker
      position: 'absolute',
      zIndex: '2',
    };
    const cover = { // same as for popover
      position: 'fixed',
      top: '0px',
      right: '0px',
      bottom: '0px',
      left: '0px',
    };
    const {company } = this.props; // gets the company from props
    return (
      <div className="banner-container settings-banner">   
              <div style={{ backgroundColor: this.state.color }}>              
                <div>
                  <Button
                    className="select-color-btn"
                    onClick={this.handleClick}>
                    Select a color
                  </Button>
                  {this.state.displayColorPicker ? (
                    <div style={popover}>
                      <div
                        style={cover}
                        onClick={this.handleClose}
                        onKeyDown={this.handleClick}
                        role="button"
                        tabIndex="0"
                        aria-label="Save"
                      />
                      <ChromePicker
                        color={this.state.background}
                        onChange={this.handleChange2} 
                        onChangeComplete={this.handleChangeComplete}
                      />
                    </div>
                  ) : null}
                </div>
              </div>
        
      </div>
    );
  }
}

const mapStateToProps = state => ({
  company: state.companies.selectedCompany,
});

const mapDispatchToProps = {
  updateCompany: Creators.updateCompanyRequest,
};

export default connect(mapStateToProps, mapDispatchToProps)(Banner);