hibiken / react-places-autocomplete

React component for Google Maps Places Autocomplete
https://hibiken.github.io/react-places-autocomplete/
MIT License
1.38k stars 388 forks source link

redux form onSelect #162

Open alexbigapps23 opened 6 years ago

alexbigapps23 commented 6 years ago

hi, how I can use onSelect on the redux form ?

Ethaan commented 6 years ago

redux-form passess the prop input which is an Object with some methods such as onChange, and that onChange dispatch to your redux state, so.

// GoogleAutocomplete.js
import React from 'react';
import PropTypes from 'prop-types';
import { FormControl } from 'material-ui/Form';
import { InputLabel } from 'material-ui/Input';
import { withStyles } from 'material-ui/styles';
import PlacesAutocomplete from 'react-places-autocomplete';

const styles = {
  controlWidth: {
    width: '100%',
  },
};

class GoogleAutocomplete extends React.Component {
  constructor(props) {
    super(props);
    this.state = { address: '' };
    this.onAddressChange = this.onAddressChange.bind(this);
  }

  onAddressChange(address) {
    const { input } = this.props;
    const { onChange } = input;
    this.setState({ address });
    onChange(address);
  }

  render() {
    const { classes } = this.props;
    const inputProps = {
      value: this.state.address,
      type: 'search',
      placeholder: 'Search Places...',
      onChange: this.onAddressChange,
    };

    return (
      <FormControl className={classes.controlWidth}>
        <InputLabel htmlFor="address2">Address:</InputLabel>
        <PlacesAutocomplete inputProps={inputProps} />
      </FormControl>
    );
  }
}

GoogleAutocomplete.defaultProps = {
  input: {},
  classes: {},
};

GoogleAutocomplete.propTypes = {
  input: PropTypes.object,
  classes: PropTypes.object,
};

export default withStyles(styles)(GoogleAutocomplete);
// YourForm.js
<Field
 name="weight"
 type="text"
 label="Wie viel Material? (kg)"
 placeholder="Wie viel Material? (kg)"
 component={FormInput}
/>
curtisolson commented 6 years ago

This is an excellent example, but I think your YourForm.js doesn't match with the other code. Does the component just need to be changed to "component={GoogleAutocomplete}"?

matiasherranz commented 4 years ago

@Ethaan Thanks for your answer! Quick tip. You can remove this line from the constructor method:

this.onAddressChange = this.onAddressChange.bind(this);

If you define the ``onAddressChange method like this:

onAddressChange = (address) => {
  ...
}

Instead of like this:

onAddressChange(address) {
  ...
}
ybakhshi commented 4 years ago

@Ethaan @curtisolson @matiasherranz Thanks for your support in advance but i get this error implementing the same code image

could you please explain me what i have to do to make it work?