<commit_before>import React from 'react';
import PropTypes from 'prop-types';

export default class ReactGoogleAutocomplete extends React.Component {
  static propTypes = {
    onPlaceSelected: PropTypes.func,
    types: PropTypes.array,
    componentRestrictions: PropTypes.object,
    bounds: PropTypes.object,
    fields: PropTypes.array,
    inputAutocompleteValue: PropTypes.string,
    apiKey: PropTypes.string
  };

  constructor(props) {
    super(props);
    this.autocomplete = null;
    this.event = null;
  }

  componentDidMount() {
    const {
      types = ['(cities)'],
      componentRestrictions,
      bounds,
      apiKey,
      fields = [
        'address_components',
        'geometry.location',
        'place_id',
        'formatted_address'
      ]
    } = this.props;
    const config = {
      types,
      bounds,
      fields
    };

    if (componentRestrictions) {
      config.componentRestrictions = componentRestrictions;
    }

    this.disableAutofill();

    const handleAutoComplete = () => {
      this.autocomplete = new google.maps.places.Autocomplete(
        this.refs.input,
        config
      );

      this.event = this.autocomplete.addListener(
        'place_changed',
        this.onSelected.bind(this)
      );
    };

    if (apiKey) {
      this.handleLoadScript().then(() => handleAutoComplete());
    } else {
      handleAutoComplete();
    }
  }

  disableAutofill() {
    // Autofill workaround adapted from https://stackoverflow.com/questions/29931712/chrome-autofill-covers-autocomplete-for-google-maps-api-v3/49161445#49161445
    if (window.MutationObserver) {
      const observerHack = new MutationObserver(() => {
        observerHack.disconnect();
        if (this.refs && this.refs.input) {
          this.refs.input.autocomplete = this.props.inputAutocompleteValue || 'new-password';
        }
      });
      observerHack.observe(this.refs.input, {
        attributes: true,
        attributeFilter: ['autocomplete']
      });
    }
  }

  componentWillUnmount() {
    if (this.event) this.event.remove();
  }

  onSelected() {
    if (this.props.onPlaceSelected && this.autocomplete) {
      this.props.onPlaceSelected(this.autocomplete.getPlace(), this.refs.input);
    }
  }

  handleLoadScript = () => {
    const googleMapsScriptUrl = `https://maps.googleapis.com/maps/api/js?key=${this.props.apiKey}&libraries=places`;

    // Check if script exists already
    if (
      document.querySelectorAll(`script[src="${googleMapsScriptUrl}"]`).length >
      0
    ) {
      return Promise.resolve();
    }

    this.googleMapsScript = document.createElement('script');
    this.googleMapsScript.src = googleMapsScriptUrl;

    document.body.appendChild(this.googleMapsScript);

    return new Promise((resolve) => {
      this.googleMapsScript.addEventListener('load', () => resolve());
    });
  };

  render() {
    const {
      onPlaceSelected,
      types,
      componentRestrictions,
      bounds,
      apiKey,
      ...rest
    } = this.props;

    return <input ref="input" {...rest} />;
  }
}
<commit_msg>added fix for inputAutocompleteValue dom warning<commit_after>import React from 'react';
import PropTypes from 'prop-types';

export default class ReactGoogleAutocomplete extends React.Component {
  static propTypes = {
    onPlaceSelected: PropTypes.func,
    types: PropTypes.array,
    componentRestrictions: PropTypes.object,
    bounds: PropTypes.object,
    fields: PropTypes.array,
    inputAutocompleteValue: PropTypes.string,
    apiKey: PropTypes.string
  };

  constructor(props) {
    super(props);
    this.autocomplete = null;
    this.event = null;
  }

  componentDidMount() {
    const {
      types = ['(cities)'],
      componentRestrictions,
      bounds,
      apiKey,
      fields = [
        'address_components',
        'geometry.location',
        'place_id',
        'formatted_address'
      ]
    } = this.props;
    const config = {
      types,
      bounds,
      fields
    };

    if (componentRestrictions) {
      config.componentRestrictions = componentRestrictions;
    }

    this.disableAutofill();

    const handleAutoComplete = () => {
      this.autocomplete = new google.maps.places.Autocomplete(
        this.refs.input,
        config
      );

      this.event = this.autocomplete.addListener(
        'place_changed',
        this.onSelected.bind(this)
      );
    };

    if (apiKey) {
      this.handleLoadScript().then(() => handleAutoComplete());
    } else {
      handleAutoComplete();
    }
  }

  disableAutofill() {
    // Autofill workaround adapted from https://stackoverflow.com/questions/29931712/chrome-autofill-covers-autocomplete-for-google-maps-api-v3/49161445#49161445
    if (window.MutationObserver) {
      const observerHack = new MutationObserver(() => {
        observerHack.disconnect();
        if (this.refs && this.refs.input) {
          this.refs.input.autocomplete = this.props.inputAutocompleteValue || 'new-password';
        }
      });
      observerHack.observe(this.refs.input, {
        attributes: true,
        attributeFilter: ['autocomplete']
      });
    }
  }

  componentWillUnmount() {
    if (this.event) this.event.remove();
  }

  onSelected() {
    if (this.props.onPlaceSelected && this.autocomplete) {
      this.props.onPlaceSelected(this.autocomplete.getPlace(), this.refs.input);
    }
  }

  handleLoadScript = () => {
    const googleMapsScriptUrl = `https://maps.googleapis.com/maps/api/js?key=${this.props.apiKey}&libraries=places`;

    // Check if script exists already
    if (
      document.querySelectorAll(`script[src="${googleMapsScriptUrl}"]`).length >
      0
    ) {
      return Promise.resolve();
    }

    this.googleMapsScript = document.createElement('script');
    this.googleMapsScript.src = googleMapsScriptUrl;

    document.body.appendChild(this.googleMapsScript);

    return new Promise((resolve) => {
      this.googleMapsScript.addEventListener('load', () => resolve());
    });
  };

  render() {
    const {
      onPlaceSelected,
      types,
      componentRestrictions,
      bounds,
      apiKey,
      inputAutocompleteValue,
      ...rest
    } = this.props;

    return <input ref="input" {...rest} />;
  }
}

{'fraction_non_alphanumeric': 0.1273426237385872, 'fraction_numerical': 0.008329328848310107, 'mean_word_length': 2.6493278784336645, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 4, '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': '2584728', 'n_tokens_mistral': 1942, 'n_tokens_neox': 1851, 'n_words': 300}