<commit_before>import React from 'react';
import ReactDOM from 'react-dom';
import { Component, PropTypes, randomid } from '../utils/';
import Input from '../input/';
import Tag from '../tag';
import Transition from '../transition';
import Popper from '../popper/';

function getChildrensComponent(_children) {
  if (!_children) _children = [];
  let items = [];
  if (_children.length > 0) {
    _children.forEach((item) => {
      if (Array.isArray(item)) {
        item.forEach((_item) => {
          items = items.concat(getChildrensComponent(_item.props.children));
        });
      } else if (item.type.names === 'option') {
        items.push(item);
      } else {
        items = items.concat(getChildrensComponent(item.props.children));
      }
    });
  }
  return items;
}

export default class Select extends Component {
  constructor(props) {
    super(props);
    this.state = {
      placeholder: props.placeholder || '请选择',
      inputHovering: false,
      selected: props.multiple ? [] : undefined,
      selectedLabel: props.value, // 默认选中的值 多选为数组
      value: props.value, // 多选或单选值
      visible: false, // 菜单是否显示
      options: [], // 在可搜索的时候，需要调用option里面的方法
      query: '', // 多标签使用
      icon: 'arrow-down',
      inputWidth: 0,
      filterItems: [], // 搜索到的内容暂存
    };
    this.toggleMenu = this.toggleMenu.bind(this);
    this.onQueryChange = this.onQueryChange.bind(this);
  }
  getChildContext() {
    return { component: this };
  }
  componentWillReceiveProps(props) {
    if (props.placeholder !== this.props.placeholder) {
      this.setState({
        placeholder: props.placeholder,
      });
    }
    if (props.value !== this.state.value) {
      this.setState({
        value: props.value,
      }, () => {
        this.selectedData();
      });
    }
  }
  componentWillUpdate(props, state) {
    if (state.query !== this.state.query) {
      this.onQueryChange(state.query);
    }
  }
  componentDidMount() {
    if (this.input.input) {
      const input = ReactDOM.findDOMNode(this.input.input);
      this.setState({
        inputWidth: input.getBoundingClientRect().width,
      });
    }
    this.selectedData(true);
  }
  handleClickOutside(e) {
    // Ignore clicks on the component it self
    // https://codepen.io/graubnla/pen/EgdgZm
    // Detect a click outside of a React Component
    // https://www.dhariri.com/posts/57c724e4d1befa66e5b8e056
    const domNode = ReactDOM.findDOMNode(this);
    if ((!domNode || !domNode.contains(e.target))) {
      this.setState({ visible: false });
    }
  }
  // 将所有渲染后的组件，寄存在当前state option上面
  onOptionCreate(option) {
    this.state.options.push(option);
    this.setState(this.state);
  }
  showLabelText(props) {
    return props.label ? props.label : props.value;
  }
  // 初始化默认选中
  selectedData(init) {
    const { multiple, children } = this.props;
    let { selectedLabel, selected } = this.state;
    const { value } = this.state;
    if (multiple && Array.isArray(value)) {
      selected = getChildrensComponent(children).reduce((prev, curr) => {
        return value.indexOf(curr.props.value) > -1 ? prev.concat(curr) : prev;
      }, []);
      selectedLabel = selected.map((option) => {
        return this.showLabelText(option.props);
      });
      this.setState({
        selected, selectedLabel,
      }, () => {
        this.resetInputHeight(init);
      });
    } else {
      // 过滤改变 selectedLabel 的value对应的值
      selected = getChildrensComponent(children).filter((option) => {
        return option.props.value === value;
      })[0];
      if (selected) {
        this.setState({
          selected,
          selectedLabel: this.showLabelText(selected.props),
        });
      }
    }
  }
  resetInputHeight(init) {
    const { filterable } = this.props;
    this.input.input.style.height = `${this.tags.clientHeight}px`;
    if (!init) {
      if (filterable) {
        this.inputMultipleFocus();
      } else {
        this.input.focus();
      }
    }
  }
  onQueryChange(query) {
    const { options } = this.state;
    const { filterable } = this.props;
    const filterItems = [];
    filterable && options.forEach((option) => {
      const visible = option.queryChange(query);
      if (visible) filterItems.push(option);
    });
    this.setState({ filterItems });
  }
  // 触发onChange事件
  onSelectedChange(option) {
    const { onChange } = this.props;
    const { value } = this.state;
    onChange && onChange(option, option.props.value, value);
  }
  // 点击选中事件, 选中设置Select值
  onOptionClick(option) {
    const { multiple } = this.props;
    let { value } = this.state;
    if (multiple) {
      if (value.indexOf(option.props.value) > -1) {
        value.splice(value.indexOf(option.props.value), 1);
      } else {
        value.push(option.props.value);
      }
    } else {
      value = option.props.value;
      this.setState({ visible: false });
    }
    this.setState({ value, query: '' }, () => {
      this.selectedData();<commit_msg>Fix the Select multiple return parameters<commit_after>import React from 'react';
import ReactDOM from 'react-dom';
import { Component, PropTypes, randomid } from '../utils/';
import Input from '../input/';
import Tag from '../tag';
import Transition from '../transition';
import Popper from '../popper/';

function getChildrensComponent(_children) {
  if (!_children) _children = [];
  let items = [];
  if (_children.length > 0) {
    _children.forEach((item) => {
      if (Array.isArray(item)) {
        item.forEach((_item) => {
          items = items.concat(getChildrensComponent(_item.props.children));
        });
      } else if (item.type.names === 'option') {
        items.push(item);
      } else {
        items = items.concat(getChildrensComponent(item.props.children));
      }
    });
  }
  return items;
}

export default class Select extends Component {
  constructor(props) {
    super(props);
    this.state = {
      placeholder: props.placeholder || '请选择',
      inputHovering: false,
      selected: props.multiple ? [] : undefined,
      selectedLabel: props.value, // 默认选中的值 多选为数组
      value: props.value, // 多选或单选值
      visible: false, // 菜单是否显示
      options: [], // 在可搜索的时候，需要调用option里面的方法
      query: '', // 多标签使用
      icon: 'arrow-down',
      inputWidth: 0,
      filterItems: [], // 搜索到的内容暂存
    };
    this.toggleMenu = this.toggleMenu.bind(this);
    this.onQueryChange = this.onQueryChange.bind(this);
  }
  getChildContext() {
    return { component: this };
  }
  componentWillReceiveProps(props) {
    if (props.placeholder !== this.props.placeholder) {
      this.setState({
        placeholder: props.placeholder,
      });
    }
    if (props.value !== this.state.value) {
      this.setState({
        value: props.value,
      }, () => {
        this.selectedData();
      });
    }
  }
  componentWillUpdate(props, state) {
    if (state.query !== this.state.query) {
      this.onQueryChange(state.query);
    }
  }
  componentDidMount() {
    if (this.input.input) {
      const input = ReactDOM.findDOMNode(this.input.input);
      this.setState({
        inputWidth: input.getBoundingClientRect().width,
      });
    }
    this.selectedData(true);
  }
  handleClickOutside(e) {
    // Ignore clicks on the component it self
    // https://codepen.io/graubnla/pen/EgdgZm
    // Detect a click outside of a React Component
    // https://www.dhariri.com/posts/57c724e4d1befa66e5b8e056
    const domNode = ReactDOM.findDOMNode(this);
    if ((!domNode || !domNode.contains(e.target))) {
      this.setState({ visible: false });
    }
  }
  // 将所有渲染后的组件，寄存在当前state option上面
  onOptionCreate(option) {
    this.state.options.push(option);
    this.setState(this.state);
  }
  showLabelText(props) {
    return props.label ? props.label : props.value;
  }
  // 初始化默认选中
  selectedData(init) {
    const { multiple, children } = this.props;
    let { selectedLabel, selected } = this.state;
    const { value } = this.state;
    if (multiple && Array.isArray(value)) {
      selected = getChildrensComponent(children).reduce((prev, curr) => {
        return value.indexOf(curr.props.value) > -1 ? prev.concat(curr) : prev;
      }, []);
      selectedLabel = selected.map((option) => {
        return this.showLabelText(option.props);
      });
      this.setState({
        selected, selectedLabel,
      }, () => {
        this.resetInputHeight(init);
      });
    } else {
      // 过滤改变 selectedLabel 的value对应的值
      selected = getChildrensComponent(children).filter((option) => {
        return option.props.value === value;
      })[0];
      if (selected) {
        this.setState({
          selected,
          selectedLabel: this.showLabelText(selected.props),
        });
      }
    }
  }
  resetInputHeight(init) {
    const { filterable } = this.props;
    this.input.input.style.height = `${this.tags.clientHeight}px`;
    if (!init) {
      if (filterable) {
        this.inputMultipleFocus();
      } else {
        this.input.focus();
      }
    }
  }
  onQueryChange(query) {
    const { options } = this.state;
    const { filterable } = this.props;
    const filterItems = [];
    filterable && options.forEach((option) => {
      const visible = option.queryChange(query);
      if (visible) filterItems.push(option);
    });
    this.setState({ filterItems });
  }
  // 触发onChange事件
  onSelectedChange(option) {
    const { onChange } = this.props;
    const { value } = this.state;
    onChange && onChange(option, value, option.props.value);
  }
  // 点击选中事件, 选中设置Select值
  onOptionClick(option) {
    const { multiple } = this.props;
    let { value } = this.state;
    if (multiple) {
      if (value.indexOf(option.props.value) > -1) {
        value.splice(value.indexOf(option.props.value), 1);
      } else {
        value.push(option.props.value);
      }
    } else {
      value = option.props.value;
      this.setState({ visible: false });
    }
    this.setState({ value, query: '' }, () => {
      this.selectedData();
{'fraction_non_alphanumeric': 0.14855653568564556, 'fraction_numerical': 0.00400962309542903, 'mean_word_length': 2.5179830747531735, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3584525', 'n_tokens_mistral': 3216, 'n_tokens_neox': 3277, 'n_words': 580}