<commit_before>let React = require('react');
let Checkbox = require('../checkbox');
let TableRowColumn = require('./table-row-column');
let ClickAwayable = require('../mixins/click-awayable');
let StylePropable = require('../mixins/style-propable');


let TableBody = React.createClass({

  mixins: [ClickAwayable, StylePropable],

  propTypes: {
    allRowsSelected: React.PropTypes.bool,
    deselectOnClickaway: React.PropTypes.bool,
    displayRowCheckbox: React.PropTypes.bool,
    multiSelectable: React.PropTypes.bool,
    onCellClick: React.PropTypes.func,
    onCellHover: React.PropTypes.func,
    onCellHoverExit: React.PropTypes.func,
    onRowHover: React.PropTypes.func,
    onRowHoverExit: React.PropTypes.func,
    onRowSelection: React.PropTypes.func,
    preScanRows: React.PropTypes.bool,
    selectable: React.PropTypes.bool,
    showRowHover: React.PropTypes.bool,
    stripedRows: React.PropTypes.bool,
    style: React.PropTypes.object,
  },

  getDefaultProps() {
    return {
      allRowsSelected: false,
      deselectOnClickaway: true,
      displayRowCheckbox: true,
      multiSelectable: false,
      preScanRows: true,
      selectable: true,
    };
  },

  getInitialState() {
    // Determine what rows are 'pre-selected'.
    let preSelectedRows = [];
    if (this.props.selectable && this.props.preScanRows) {
      let index = 0;
      React.Children.forEach(this.props.children, (child) => {
        if (React.isValidElement(child)) {
          if (child.props.selected && (preSelectedRows.length === 0 || this.props.multiSelectable)) {
            preSelectedRows.push(index);
          }

          index++;
        }
      });
    }

    return {
      selectedRows: preSelectedRows,
    };
  },

  componentWillReceiveProps(nextProps) {
    if (this.props.allRowsSelected && !nextProps.allRowsSelected) {
      let lastSelectedRow = (this.state.selectedRows.length) ?
        this.state.selectedRows[this.state.selectedRows.length - 1] : undefined;

      this.setState({ selectedRows: [lastSelectedRow] });
    }
  },

  componentClickAway() {
    if (this.props.deselectOnClickaway && this.state.selectedRows.length) {
      this.setState({ selectedRows: [] });
    }
  },

  render() {
    let {
      className,
      style,
      ...other,
    } = this.props;
    let classes = 'mui-table-body';
    if (className) classes += ' ' + className;

    let rows = this._createRows();

    return (
      <tbody className={classes} style={style}>
        {rows}
      </tbody>
    );
  },

  _createRows() {
    let numChildren = React.Children.count(this.props.children);
    let rowNumber = 0;
    const handlers = {
      onCellClick: this._onCellClick,
      onCellHover: this._onCellHover,
      onCellHoverExit: this._onCellHoverExit,
      onRowHover: this._onRowHover,
      onRowHoverExit: this._onRowHoverExit,
      onRowClick: this._onRowClick,
    };

    return React.Children.map(this.props.children, (child) => {
      if (React.isValidElement(child)) {
        let props = {
          displayRowCheckbox: this.props.displayRowCheckbox,
          hoverable: this.props.showRowHover,
          selected: this._isRowSelected(rowNumber),
          striped: this.props.stripedRows && (rowNumber % 2 === 0),
          rowNumber: rowNumber++,
        };
        let checkboxColumn = this._createRowCheckboxColumn(props);

        if (rowNumber === numChildren) {
          props.displayBorder = false;
        }

        let children = [checkboxColumn];
        React.Children.forEach(child.props.children, (child) => {
          children.push(child);
        });

        return React.cloneElement(child, {...props, ...handlers}, children);
      }
    });
  },

  _createRowCheckboxColumn(rowProps) {
    if (!this.props.displayRowCheckbox) return null;

    let key = rowProps.rowNumber + '-cb';
    let checkbox =
      <Checkbox
        ref='rowSelectCB'
        name={key}
        value='selected'
        disabled={!this.props.selectable}
        checked={rowProps.selected} />;

    return (
      <TableRowColumn
        key={key}
        columnNumber={0}
        style={{width: 24}}>
        {checkbox}
      </TableRowColumn>
    );
  },

  _isRowSelected(rowNumber) {
    if (this.props.allRowsSelected) {
      return true;
    }

    for (let i = 0; i < this.state.selectedRows.length; i++) {
      let selection = this.state.selectedRows[i];

      if (typeof selection === 'object') {
        if (this._isValueInRange(rowNumber, selection)) return true;
      }
      else {
        if (selection === rowNumber) return true;
      }
    }

    return false;
  },
<commit_msg>work towards row selection update<commit_after>let React = require('react');
let Checkbox = require('../checkbox');
let TableRowColumn = require('./table-row-column');
let ClickAwayable = require('../mixins/click-awayable');
let StylePropable = require('../mixins/style-propable');


let TableBody = React.createClass({

  mixins: [ClickAwayable, StylePropable],

  propTypes: {
    allRowsSelected: React.PropTypes.bool,
    deselectOnClickaway: React.PropTypes.bool,
    displayRowCheckbox: React.PropTypes.bool,
    multiSelectable: React.PropTypes.bool,
    onCellClick: React.PropTypes.func,
    onCellHover: React.PropTypes.func,
    onCellHoverExit: React.PropTypes.func,
    onRowHover: React.PropTypes.func,
    onRowHoverExit: React.PropTypes.func,
    onRowSelection: React.PropTypes.func,
    preScanRows: React.PropTypes.bool,
    selectable: React.PropTypes.bool,
    showRowHover: React.PropTypes.bool,
    stripedRows: React.PropTypes.bool,
    style: React.PropTypes.object,
  },

  getDefaultProps() {
    return {
      allRowsSelected: false,
      deselectOnClickaway: true,
      displayRowCheckbox: true,
      multiSelectable: false,
      preScanRows: true,
      selectable: true,
    };
  },

  getInitialState() {
    return {
      selectedRows: this._calculatePreselectedRows(this.props),
    };
  },

  componentWillReceiveProps(nextProps) {
    if (nextProps.rowData !== this.props.rowData) {
      this.setState({
        selectedRows: this._calculatePreselectedRows(nextProps),
      });
    } else if (this.props.allRowsSelected && !nextProps.allRowsSelected) {
      let lastSelectedRow = (this.state.selectedRows.length) ?
        this.state.selectedRows[this.state.selectedRows.length - 1] : undefined;

      this.setState({ 
        selectedRows: [lastSelectedRow] 
      });
    }
  },

  componentClickAway() {
    if (this.props.deselectOnClickaway && this.state.selectedRows.length) {
      this.setState({ selectedRows: [] });
    }
  },

  render() {
    let {
      className,
      style,
      ...other,
    } = this.props;
    let classes = 'mui-table-body';
    if (className) classes += ' ' + className;

    let rows = this._createRows();

    return (
      <tbody className={classes} style={style}>
        {rows}
      </tbody>
    );
  },

  _createRows() {
    let numChildren = React.Children.count(this.props.children);
    let rowNumber = 0;
    const handlers = {
      onCellClick: this._onCellClick,
      onCellHover: this._onCellHover,
      onCellHoverExit: this._onCellHoverExit,
      onRowHover: this._onRowHover,
      onRowHoverExit: this._onRowHoverExit,
      onRowClick: this._onRowClick,
    };

    return React.Children.map(this.props.children, (child) => {
      if (React.isValidElement(child)) {
        let props = {
          displayRowCheckbox: this.props.displayRowCheckbox,
          hoverable: this.props.showRowHover,
          selected: this._isRowSelected(rowNumber),
          striped: this.props.stripedRows && (rowNumber % 2 === 0),
          rowNumber: rowNumber++,
        };
        let checkboxColumn = this._createRowCheckboxColumn(props);

        if (rowNumber === numChildren) {
          props.displayBorder = false;
        }

        let children = [checkboxColumn];
        React.Children.forEach(child.props.children, (child) => {
          children.push(child);
        });

        return React.cloneElement(child, {...props, ...handlers}, children);
      }
    });
  },

  _createRowCheckboxColumn(rowProps) {
    if (!this.props.displayRowCheckbox) return null;

    let key = rowProps.rowNumber + '-cb';
    let checkbox =
      <Checkbox
        ref='rowSelectCB'
        name={key}
        value='selected'
        disabled={!this.props.selectable}
        checked={rowProps.selected} />;

    return (
      <TableRowColumn
        key={key}
        columnNumber={0}
        style={{width: 24}}>
        {checkbox}
      </TableRowColumn>
    );
  },

  _calculatePreselectedRows (props) {
    // Determine what rows are 'pre-selected'.
    let preSelectedRows = [];

    if (props.selectable && props.preScanRows) {
      let index = 0;
      React.Children.forEach(props.children, (child) => {
        if (React.isValidElement(child)) {
          if (child.props.selected && (preSelectedRows.length === 0 || props.multiSelectable)) {
            preSelectedRows.push(index);
          }

          index++;
        }
      });
    }

    return preSelectedRows;
  },

  _isRowSelected(rowNumber) {
    if (this.props.allRowsSelected) {
      return true;
    }

    for (let i = 0; i < this.state.selectedRows.length; i++) {
      let selection = this.state.selectedRows[i];

      if (typeof selection === 'object') {
        if (this._isValueInRange(rowNumber, selection)) return true;
      }
      else {
        if (selection === rowNumber) return true;
      }
    }

    return false;
  },

{'fraction_non_alphanumeric': 0.12861029719547928, 'fraction_numerical': 0.002092925910422771, 'mean_word_length': 2.5996233521657253, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4859315', 'n_tokens_mistral': 2964, 'n_tokens_neox': 2910, 'n_words': 519}