-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from pieterv/frag-component
Added Frag component
- Loading branch information
Showing
8 changed files
with
213 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** @jsx React.DOM */ | ||
"use strict"; | ||
|
||
// This example creates a reusable react component made | ||
// up of ReactGoogleMap components. You can click the map | ||
// to add a new instance or drag the component to move | ||
// it around. | ||
|
||
var React = require('react/addons'); | ||
var ReactGoogleMaps = require('../../'); | ||
var GoogleMapsAPI = window.google.maps; | ||
var Map = ReactGoogleMaps.Map; | ||
var Circle = ReactGoogleMaps.Circle; | ||
var Polyline = ReactGoogleMaps.Polyline; | ||
var Frag = ReactGoogleMaps.Frag; | ||
var LatLng = GoogleMapsAPI.LatLng; | ||
|
||
var DraggableFace = React.createClass({ | ||
render: function() { | ||
var centerLat = this.props.center.lat(); | ||
var centerLng = this.props.center.lng(); | ||
return ( | ||
<Frag | ||
map={this.props.map}> | ||
<Circle | ||
draggable | ||
onDrag={this.props.onDrag} | ||
center={new LatLng(centerLat + 0.05, centerLng + 0.1)} | ||
radius={3000} /> | ||
|
||
<Circle | ||
draggable | ||
onDrag={this.props.onDrag} | ||
center={new LatLng(centerLat + 0.05, centerLng - 0.1)} | ||
radius={3000} /> | ||
|
||
<Polyline | ||
draggable | ||
onDrag={this.props.onDrag} | ||
path={[ | ||
new LatLng(centerLat - 0.05, centerLng + 0.1), | ||
new LatLng(centerLat - 0.05, centerLng - 0.1) | ||
]} /> | ||
</Frag> | ||
); | ||
} | ||
}); | ||
|
||
var GoogleMapFrags = React.createClass({ | ||
getInitialState: function() { | ||
return { | ||
center: new LatLng(41.879535, -87.624333), | ||
zoom: 8, | ||
faces: [ | ||
new LatLng(42.48374, -87.01171) | ||
] | ||
}; | ||
}, | ||
|
||
render: function() { | ||
return ( | ||
<Map | ||
initialZoom={this.state.zoom} | ||
initialCenter={this.state.center} | ||
width={700} | ||
height={700} | ||
onClick={this.handleFaceCreate}> | ||
{this.state.faces.map(this.renderFace)} | ||
</Map> | ||
); | ||
}, | ||
|
||
renderFace: function(position, i) { | ||
return ( | ||
<DraggableFace | ||
center={position} | ||
onDrag={this.handleFaceDrag.bind(null, i)} | ||
key={i} /> | ||
); | ||
}, | ||
|
||
handleFaceDrag: function(i, e) { | ||
var faces = React.addons | ||
.update(this.state.faces, {$splice: [[i, 1, e.latLng]]}); | ||
|
||
this.setState({ | ||
faces: faces | ||
}); | ||
}, | ||
|
||
handleFaceCreate: function(e) { | ||
var faces = React.addons | ||
.update(this.state.faces, {$push: [e.latLng]}); | ||
|
||
this.setState({ | ||
faces: faces | ||
}); | ||
} | ||
}); | ||
|
||
React.renderComponent(<GoogleMapFrags />, document.getElementById('example')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<!doctype html public "restroom"> | ||
<title>Frags</title> | ||
<link href="../global.css" rel="stylesheet" /> | ||
<body> | ||
<h1>Frags</h1> | ||
<div id="example"></div> | ||
<script src="http://maps.googleapis.com/maps/api/js"></script> | ||
<script src="../build/frags.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** @jsx React.DOM */ | ||
"use strict"; | ||
|
||
var React = require('react'); | ||
var cloneWithProps = require('react/lib/cloneWithProps'); | ||
var MapPropTypes = require('../MapPropTypes'); | ||
|
||
function injectMapInto(child) { | ||
return React.isValidComponent(child) ? | ||
cloneWithProps(child, {map: this.props.map}) : child; | ||
} | ||
|
||
var ReactFrag = React.createClass({ | ||
propTypes: { | ||
map: MapPropTypes.Map.isRequired, | ||
}, | ||
|
||
render: function() { | ||
// Inject the `mapProps` into all children that are | ||
// valid components. | ||
var children = React.Children | ||
.map(this.props.children, injectMapInto, this); | ||
|
||
return ( | ||
<span>{children}</span> | ||
); | ||
} | ||
}); | ||
|
||
module.exports = ReactFrag; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** @jsx React.DOM */ | ||
|
||
jest.dontMock('../ReactFrag'); | ||
|
||
describe('ReactFrag', function() { | ||
it('should pass all props to all valid children', function() { | ||
var React = require('react'); | ||
var ReactTestUtils = require('react/lib/ReactTestUtils'); | ||
var ReactFrag = require('../ReactFrag'); | ||
|
||
var didMount = jest.genMockFn(); | ||
var Test = React.createClass({ | ||
render: function() { return null; }, | ||
componentDidMount: function() { | ||
didMount(this.props); | ||
} | ||
}); | ||
|
||
var map = {}; | ||
var testProp = {}; | ||
ReactTestUtils.renderIntoDocument( | ||
<ReactFrag map={map} testProp={testProp}> | ||
<Test /> | ||
{null} | ||
{false} | ||
<ReactFrag> | ||
<Test /> | ||
</ReactFrag> | ||
</ReactFrag> | ||
); | ||
|
||
expect(didMount.mock.calls.length).toBe(2); | ||
expect(didMount.mock.calls[0][0]).toEqual({map: map}); | ||
expect(didMount.mock.calls[1][0]).toEqual({map: map}); | ||
}); | ||
}); |