All files / app/address/googleAutocomplete GoogleAutocomplete.tsx

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 15725x 25x   25x     25x 25x                                         25x       10x 10x 10x           25x   12x 12x 12x 12x     12x 12x 12x   12x                                     10x                                         10x                               25x                                             25x             25x             25x               25x   25x  
import { noop } from 'lodash';
import React, { PureComponent, ReactNode } from 'react';
 
import { Autocomplete, AutocompleteItem } from '../../ui/autocomplete';
 
import { GoogleAutocompleteOptionTypes } from './googleAutocompleteTypes';
import './GoogleAutocomplete.scss';
import GoogleAutocompleteService from './GoogleAutocompleteService';
 
interface GoogleAutocompleteProps {
    initialValue?: string;
    componentRestrictions?: google.maps.places.ComponentRestrictions;
    fields?: string[];
    apiKey: string;
    nextElement?: HTMLElement;
    inputProps?: any;
    isAutocompleteEnabled?: boolean;
    types?: GoogleAutocompleteOptionTypes[];
    onSelect?(place: google.maps.places.PlaceResult, item: AutocompleteItem): void;
    onToggleOpen?(state: { inputValue: string; isOpen: boolean }): void;
    onChange?(value: string, isOpen: boolean): void;
}
 
interface GoogleAutocompleteState {
    items: AutocompleteItem[];
    autoComplete: string;
}
 
class GoogleAutocomplete extends PureComponent<GoogleAutocompleteProps, GoogleAutocompleteState> {
    googleAutocompleteService: GoogleAutocompleteService;
 
    constructor(props: GoogleAutocompleteProps) {
        super(props);
        this.googleAutocompleteService = new GoogleAutocompleteService(props.apiKey);
        this.state = {
            items: [],
            autoComplete: 'off',
        };
    }
 
    render(): ReactNode {
        const {
            initialValue,
            onToggleOpen = noop,
            inputProps = {},
        } = this.props;
 
        const {
            autoComplete,
            items,
        } = this.state;
 
        return (
            <Autocomplete
                initialHighlightedIndex={ 0 }
                initialValue={ initialValue }
                inputProps={ {
                    ...inputProps,
                    autoComplete,
                } }
                items={ items }
                listTestId="address-autocomplete-suggestions"
                onChange={ this.onChange }
                onSelect={ this.onSelect }
                onToggleOpen={ onToggleOpen }
            >
                <div className="co-googleAutocomplete-footer" />
            </Autocomplete>
        );
    }
 
    private onSelect: (item: AutocompleteItem) => void = item => {
        const {
            fields,
            onSelect = noop,
            nextElement,
        } = this.props;
 
        this.googleAutocompleteService.getPlacesServices().then(service => {
            service.getDetails({
                placeId: item.id,
                fields: fields || ['address_components', 'name'],
            }, result => {
                if (nextElement) {
                    nextElement.focus();
                }
 
                onSelect(result, item);
            });
        });
    };
 
    private onChange: (input: string) => void = input => {
        const {
            isAutocompleteEnabled,
            onChange = noop,
        } = this.props;
 
        onChange(input, false);
 
        if (!isAutocompleteEnabled) {
            return this.resetAutocomplete();
        }
 
        this.setAutocomplete(input);
        this.setItems(input);
    };
 
    private setItems(input: string): void {
        if (!input) {
            this.setState({ items: [] });
 
            return;
        }
 
        const {
            componentRestrictions,
            types,
        } = this.props;
 
        this.googleAutocompleteService.getAutocompleteService().then(service => {
            service.getPlacePredictions({
                input,
                types: types || ['geocode'],
                componentRestrictions,
            }, results =>
                this.setState({ items: this.toAutocompleteItems(results) })
            );
        });
    }
 
    private resetAutocomplete(): void {
        this.setState({
            items: [],
            autoComplete: 'off',
        });
    }
 
    private setAutocomplete(input: string): void {
        this.setState({
            ...this.state,
            autoComplete: input && input.length ? 'nope' : 'off',
        });
    }
 
    private toAutocompleteItems(results?: google.maps.places.AutocompletePrediction[]): AutocompleteItem[] {
        return (results || []).map(result => ({
            label: result.description,
            value: result.structured_formatting.main_text,
            highlightedSlices: result.matched_substrings,
            id: result.place_id,
        }));
    }
}
 
export default GoogleAutocomplete;