import React, { Component } from 'react';
import { Button } from 'antd-v4';
import styles from '@/index.less';
import * as commonUtils from '@/utils/utils';

class commonParamComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
    };
  }
  /**   渲染前只执行一次   */
  componentWillMount() {
    this.assignmentWillProps(this.props);
  }
  componentWillReceiveProps(nextProps) {
    this.assignmentWillProps(nextProps);
  }

  handleDoubleClick = (e, child) => {
    const { sReturnFieldName } = this.props;
    const { [`${sReturnFieldName}`]: currValue } = child;
    this.props.onDoubleClick(this.props.name, currValue);
  }

  assignmentWillProps = (props) => {
    this.setState({ disabled: props.disabled, data: commonUtils.isEmptyArr(props.data) ? [] : props.data });
  }
  render() {
    const { data, disabled } = this.state;
    const { sReturnFieldName } = this.props;
    return (
      <div className={styles.tabButton}>
        {
          data.map(child => (
            <Button
              key={child[`${sReturnFieldName}`]}
              disabled={disabled}
              onDoubleClick={e => this.handleDoubleClick(e, child)}
            >
              {child.sName}
            </Button>
          ))
        }
      </div>
    );
  }
}

export default commonParamComponent;
