import React, { Fragment, Component } from 'react';
import PropTypes from 'prop-types';
import { storiesOf } from '@storybook/react';
import { action } from '@storybook/addon-actions';
import { Configure, Highlight, connectHits } from 'react-instantsearch-dom';
import {
GoogleMapsLoader,
GeoSearch,
Marker,
CustomMarker,
Redo,
Control,
} from 'react-instantsearch-dom-maps';
import { WrapWithHits } from './util';
import Places from './places';
const stories = storiesOf('GeoSearch', module);
const Container = ({ children }) => (
{children}
);
Container.propTypes = {
children: PropTypes.node.isRequired,
};
const apiKey = 'AIzaSyBawL8VbstJDdU5397SUX7pEt9DslAwWgQ';
const endpoint = 'https://maps.googleapis.com/maps/api/js?v=weekly';
const initialZoom = 12;
const initialPosition = {
lat: 40.71,
lng: -74.01,
};
stories
.add('default', () => (
{(google) => (
{({ hits }) => (
{hits.map((hit) => (
))}
)}
)}
))
.add('with default refinement', () => (
{(google) => (
{({ hits }) => (
{hits.map((hit) => (
))}
)}
)}
))
.add('with refine disabled', () => (
{(google) => (
{({ hits }) => (
{hits.map((hit) => (
))}
)}
)}
));
// Only UI
stories
.add('with zoom & center', () => (
{(google) => (
{({ hits }) => (
{hits.map((hit) => (
))}
)}
)}
))
.add('with map options', () => (
{(google) => (
{({ hits }) => (
{hits.map((hit) => (
))}
)}
)}
))
.add('with options', () => (
{(google) => (
{({ hits }) => (
{hits.map((hit) => (
{}}
/>
))}
)}
)}
))
.add('with events', () => (
{(google) => (
{({ hits }) => (
{hits.map((hit) => (
))}
)}
)}
))
.add('with component', () => (
{(google) => (
{({ hits }) => (
{hits.map((hit) => (
))}
)}
)}
))
.add('with component', () => (
{(google) => (
{({ hits }) => (
{hits.map((hit) => (
))}
)}
)}
))
.add('with component disabled', () => (
{(google) => (
{({ hits }) => (
{hits.map((hit) => (
))}
)}
)}
))
.add('with ', () => (
{(google) => (
{({ hits }) => (
{hits.map((hit) => (
{hit.price_formatted}
))}
)}
)}
))
.add('with events', () => (
{(google) => (
{({ hits }) => (
{hits.map((hit) => (
{hit.price_formatted}
))}
)}
)}
));
// With Places
stories.add('with Places', () => (
{(google) => (
{({ hits }) => (
{hits.map((hit) => (
))}
)}
)}
));
stories.add('with InfoWindow', () => {
class Example extends Component {
static propTypes = {
google: PropTypes.object.isRequired,
};
InfoWindow = new this.props.google.maps.InfoWindow();
onClickMarker = ({ hit, marker }) => {
if (this.InfoWindow.getMap()) {
this.InfoWindow.close();
}
this.InfoWindow.setContent(hit.name);
this.InfoWindow.open(marker.getMap(), marker);
};
renderGeoHit = (hit) => (
{
this.onClickMarker({
hit,
marker,
});
}}
/>
);
render() {
const { google } = this.props;
return (
{({ hits }) => {hits.map(this.renderGeoHit)}}
);
}
}
return (
{(google) => }
);
});
stories.add('with hits communication (custom)', () => {
const CustomHits = connectHits(({ hits, selectedHit, onHitOver }) => (
{hits.map((hit) => {
const classNames = [
'hit',
'hit--airbnb',
selectedHit && selectedHit.objectID === hit.objectID
? 'hit--airbnb-active'
: '',
];
return (
onHitOver(hit)}
onMouseLeave={() => onHitOver(null)}
>
);
})}
));
class Example extends Component {
state = {
selectedHit: null,
};
onHitOver = (hit) =>
this.setState(() => ({
selectedHit: hit,
}));
renderGeoHit = (hit) => {
const { selectedHit } = this.state;
const classNames = [
'my-custom-marker',
selectedHit && selectedHit.objectID === hit.objectID
? 'my-custom-marker--active'
: '',
];
return (
this.onHitOver(hit)}
onMouseLeave={() => this.onHitOver(null)}
>
{hit.price_formatted}
);
};
render() {
const { selectedHit } = this.state;
return (
}
>
{(google) => (
{({ hits }) => (
{hits.map(this.renderGeoHit)}
)}
)}
);
}
}
return ;
});
stories.add('with unmount', () => {
class Example extends Component {
state = {
visible: true,
};
onToggle = () =>
this.setState(({ visible }) => ({
visible: !visible,
}));
render() {
const { visible } = this.state;
return (
{visible && (
{(google) => (
{({ hits }) => (
{hits.map((hit) => (
))}
)}
)}
)}
);
}
}
return ;
});