<commit_before>import React, { Component } from 'react';
import classnames from 'classnames';
import BScroll from 'better-scroll';
import { BaseWheelProps } from './PropsType';
import { isArray } from '../utils/validate';

const getValue = (props, defaultValue?: any) => {
  if ('value' in props) {
    return props.value;
  }
  if ('defaultValue' in props) {
    return props.defaultValue;
  }
  if (isArray(props.dataSource) && props.dataSource[0]) {
    return props.dataSource[0][props.valueMember];
  }
  return defaultValue;
};

export interface WheelProps extends BaseWheelProps {
  prefixCls?: string;
  className?: string;
}

export default class Wheel extends Component<WheelProps, any> {
  private BScroll;

  private wrapper;

  private isChangedByProps;

  static defaultProps = {
    prefixCls: 'za-wheel',
    dataSource: [],
    valueMember: 'value',
    itemRender: (item) => item.label,
  };

  componentDidMount() {
    const { prefixCls, dataSource, disabled, onTransition, valueMember } = this.props;
    const value = getValue(this.props);
    const initIndex = this.getSelectedIndex(value, dataSource);
    this.BScroll = new BScroll(this.wrapper, {
      wheel: {
        selectedIndex: initIndex,
        wheelWrapperClass: `${prefixCls}-content`,
        wheelItemClass: `${prefixCls}-item`,
      },
      probeType: 3,
    });

    disabled && this.BScroll.disable();

    this.BScroll.on('scroll', () => {
      if (typeof onTransition === 'function') {
        onTransition!(this.BScroll.isInTransition);
      }
    });
    this.BScroll.on('scrollEnd', () => {
      if (this.isChangedByProps) {
        this.isChangedByProps = false;
        return;
      }
      const index = this.BScroll.getSelectedIndex();
      const child = dataSource![index];
      onTransition!(this.BScroll.isInTransition);
      if (child) {
        this.fireValueChange(child[valueMember!]);
      }
      // else if (console.warn) {
      //   console.warn('child not found', dataSource, index);
      // }
    });
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.disabled) {
      this.BScroll.disable();
    }
  }

  componentDidUpdate(prevProps) {
    const { value, dataSource } = this.props;
    this.BScroll.refresh();
    const oldIndex = this.getSelectedIndex(prevProps.value, prevProps.dataSource);
    const newIndex = this.getSelectedIndex(value, dataSource);
    if (newIndex !== oldIndex) {
      this.isChangedByProps = true;
      this.BScroll.wheelTo(newIndex);<commit_msg>fix: add current dataSource<commit_after>import React, { Component } from 'react';
import classnames from 'classnames';
import BScroll from 'better-scroll';
import { BaseWheelProps } from './PropsType';
import { isArray } from '../utils/validate';

const getValue = (props, defaultValue?: any) => {
  if ('value' in props) {
    return props.value;
  }
  if ('defaultValue' in props) {
    return props.defaultValue;
  }
  if (isArray(props.dataSource) && props.dataSource[0]) {
    return props.dataSource[0][props.valueMember];
  }
  return defaultValue;
};

export interface WheelProps extends BaseWheelProps {
  prefixCls?: string;
  className?: string;
}

export default class Wheel extends Component<WheelProps, any> {
  private BScroll;

  private wrapper;

  private isChangedByProps;

  static defaultProps = {
    prefixCls: 'za-wheel',
    dataSource: [],
    valueMember: 'value',
    itemRender: item => item.label,
  };

  componentDidMount() {
    const { prefixCls, dataSource, disabled, onTransition, valueMember } = this.props;
    const value = getValue(this.props);
    const initIndex = this.getSelectedIndex(value, dataSource);
    this.BScroll = new BScroll(this.wrapper, {
      wheel: {
        selectedIndex: initIndex,
        wheelWrapperClass: `${prefixCls}-content`,
        wheelItemClass: `${prefixCls}-item`,
      },
      probeType: 3,
    });

    disabled && this.BScroll.disable();

    this.BScroll.on('scroll', () => {
      if (typeof onTransition === 'function') {
        onTransition!(this.BScroll.isInTransition);
      }
    });
    this.BScroll.on('scrollEnd', () => {
      const { dataSource: curDataSource } = this.props;
      if (this.isChangedByProps) {
        this.isChangedByProps = false;
        return;
      }
      const index = this.BScroll.getSelectedIndex();
      const child = curDataSource![index];

      onTransition!(this.BScroll.isInTransition);
      if (child) {
        this.fireValueChange(child[valueMember!]);
      }
      // else if (console.warn) {
      //   console.warn('child not found', dataSource, index);
      // }
    });
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.disabled) {
      this.BScroll.disable();
    }
  }

  componentDidUpdate(prevProps) {
    const { value, dataSource } = this.props;
    this.BScroll.refresh();
    const oldIndex = this.getSelectedIndex(prevProps.value, prevProps.dataSource);
    const newIndex = this.getSelectedIndex(value, dataSource);
    if (newIndex !== oldIndex) {
      this.isChangedByProps = true;
      this.BScroll.wheelTo(newIndex);
{'fraction_non_alphanumeric': 0.13303291536050157, 'fraction_numerical': 0.0011755485893416929, 'mean_word_length': 3.1741618969746526, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '4980191', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1526, 'n_words': 326}