├── .gitignore ├── .travis.yml ├── README.md ├── example ├── .env ├── .gitignore ├── README.md ├── package.json ├── public │ ├── favicon.ico │ ├── index.html │ └── manifest.json ├── src │ ├── App.css │ ├── App.js │ ├── App.test.js │ ├── entities │ │ ├── WithMultipleMarkers.js │ │ └── WithSingleMarker.js │ ├── index.css │ ├── index.js │ ├── logo.svg │ ├── mocks │ │ └── data.js │ └── serviceWorker.js └── yarn.lock ├── lib ├── copy_package.js ├── copy_readme.js └── test.js ├── package-lock.json ├── package.json ├── scripts └── publish.sh ├── src ├── Map.js ├── SearchBox.js ├── index.js └── utils.js ├── webpack.config.js └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | .idea 8 | 9 | # testing 10 | /coverage 11 | 12 | # production 13 | /dist 14 | 15 | # misc 16 | .DS_Store 17 | .env.local 18 | .env.development.local 19 | .env.test.local 20 | .env.production.local 21 | 22 | npm-debug.log* 23 | yarn-debug.log* 24 | yarn-error.log* 25 | 26 | /.history -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '10' 4 | dist: dist 5 | sudo: required 6 | script: 7 | - npm ci 8 | - npm run build 9 | cache: 10 | pip: true 11 | directories: 12 | - node_modules 13 | before_deploy: 14 | - if [[ "$TRAVIS_PULL_REQUEST" = false && $TRAVIS_BRANCH == "master" ]]; then 15 | echo "//registry.npmjs.org/:_authToken=\${NPM_TOKEN}" > .npmrc; 16 | fi 17 | deploy: 18 | provider: script 19 | script: bash scripts/publish.sh 20 | skip_cleanup: true 21 | target-branch: $TRAVIS_BRANCH 22 | on: 23 | branches: 24 | only: 25 | - master 26 | # Trigger a push build on master and greenkeeper branches + PRs build on every branches 27 | # Avoid double build on PRs (See https://github.com/travis-ci/travis-ci/issues/1147) 28 | branches: 29 | only: 30 | - master 31 | - /^greenkeeper/.*$/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @fusionworks/ra-google-maps-input 2 | 3 | A simple map input for [react-admin](https://github.com/marmelab/react-admin) that uses [react-google-maps](https://github.com/tomchentw/react-google-maps) to set & show points on map. 4 | 5 | # Install 6 | 7 | Using npm: npm i @fusionworks/ra-google-maps-input 8 | 9 | Using yarn: yarn add @fusionworks/ra-google-maps-input 10 | 11 | # Usage 12 | 13 | ```jsx 14 | import { Edit, SimpleForm, TextInput } from 'react-admin'; 15 | // import GMapInput component 16 | import { GMapInput } from '@fusionworks/ra-google-maps-input'; 17 | 18 | // use GMapInput in Edit form 19 | export const EntityEdit = props => ( 20 | 21 | 22 | 23 | 24 | 28 | 29 | 30 | ); 31 | ``` 32 | You can also use that component to work also with multiple markers on map: 33 | 34 | ```jsx 35 | import { Edit, SimpleForm, TextInput } from 'react-admin'; 36 | // import GMapInput component 37 | import { GMapInput } from '@fusionworks/ra-google-maps-input'; 38 | 39 | // use GMapInput in Edit form with "multipleMarkers" prop 40 | export const EntityEdit = props => ( 41 | 42 | 43 | 44 | 45 | 50 | 51 | 52 | ); 53 | ``` 54 | 55 | That component provides a prop that allows to use a search input to search for a specific location on map, and to put a marker on that location 56 | 57 | ```jsx 58 | import { Edit, SimpleForm, TextInput } from 'react-admin'; 59 | // import GMapInput component 60 | import { GMapInput } from '@fusionworks/ra-google-maps-input'; 61 | 62 | // use GMapInput in Edit form with "searchable" prop 63 | export const EntityEdit = props => ( 64 | 65 | 66 | 67 | 68 | 73 | 74 | 75 | ); 76 | ``` 77 | 78 | Also, there exists an Field component, that can be used in Show Views 79 | 80 | ```jsx 81 | 82 | import { Show, SimpleShowLayout, TextField } from 'ra-google-maps-input'; 83 | /// import GMapField component 84 | import { GMapField } from 'ra-google-maps-input'; 85 | 86 | // use GMapField component in show View 87 | export const ShowEntity = props => ( 88 | 89 | 90 | 91 | 92 | 96 | 97 | 98 | ); 99 | ``` 100 | 101 | 102 | # Props 103 | 104 | |Prop|Type|Description|Default| 105 | |:---:|:---:|:---:|:---:| 106 | |googleKey|**string**|the google aplication key for your map| 107 | |source|**string**|source field in your resource model| 108 | |multipleMarkers|**boolean**|source field in your resource model|**false**| 109 | |searchable|**boolean**|adds a search input to your map|**false**| 110 | 111 | For ```GMapField ``` ar aveilable just **source** and **googleKey** props 112 | 113 | # Example 114 | You can find an example of react-admin aplication that uses ```@fusionworks/ra-google-maps-input``` in ```/example``` directory of component github repository 115 | 116 | To run this example you need to: 117 | clone the repository, navigate in example folder, install dependencies, and run the app. 118 | 119 | - ``` git clone git@github.com:FusionWorks/react-admin-google-maps.git && cd react-admin-google-maps``` 120 | 121 | - ``` cd example ``` 122 | 123 | - ``` npm i ``` or ``` yarn ``` 124 | 125 | - ```npm start ``` or ``` yarn start ``` -------------------------------------------------------------------------------- /example/.env: -------------------------------------------------------------------------------- 1 | SKIP_PREFLIGHT_CHECK=true -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | /.history 21 | 22 | npm-debug.log* 23 | yarn-debug.log* 24 | yarn-error.log* 25 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `npm start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `npm test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `npm run build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `npm run eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 35 | 36 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 37 | 38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | 46 | ### Code Splitting 47 | 48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 49 | 50 | ### Analyzing the Bundle Size 51 | 52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 53 | 54 | ### Making a Progressive Web App 55 | 56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 57 | 58 | ### Advanced Configuration 59 | 60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 61 | 62 | ### Deployment 63 | 64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 65 | 66 | ### `npm run build` fails to minify 67 | 68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 69 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@fusionworks/ra-google-maps-input": "^0.1.2", 7 | "prop-types": "^15.7.2", 8 | "ra-data-fakerest": "^2.6.0", 9 | "react": "^16.8.6", 10 | "react-admin": "^2.9.2", 11 | "react-dom": "^16.8.6", 12 | "react-google-maps": "^9.4.5", 13 | "react-scripts": "3.0.1" 14 | }, 15 | "scripts": { 16 | "start": "react-scripts start", 17 | "build": "react-scripts build", 18 | "test": "react-scripts test", 19 | "eject": "react-scripts eject" 20 | }, 21 | "eslintConfig": { 22 | "extends": "react-app" 23 | }, 24 | "browserslist": { 25 | "production": [ 26 | ">0.2%", 27 | "not dead", 28 | "not op_mini all" 29 | ], 30 | "development": [ 31 | "last 1 chrome version", 32 | "last 1 firefox version", 33 | "last 1 safari version" 34 | ] 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /example/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FusionWorks/react-admin-google-maps/c7b5a8ac087a2dfdacdc233968a86716303b03ec/example/public/favicon.ico -------------------------------------------------------------------------------- /example/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 22 | React App 23 | 24 | 25 | 26 |
27 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /example/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": ".", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /example/src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | animation: App-logo-spin infinite 20s linear; 7 | height: 40vmin; 8 | pointer-events: none; 9 | } 10 | 11 | .App-header { 12 | background-color: #282c34; 13 | min-height: 100vh; 14 | display: flex; 15 | flex-direction: column; 16 | align-items: center; 17 | justify-content: center; 18 | font-size: calc(10px + 2vmin); 19 | color: white; 20 | } 21 | 22 | .App-link { 23 | color: #61dafb; 24 | } 25 | 26 | @keyframes App-logo-spin { 27 | from { 28 | transform: rotate(0deg); 29 | } 30 | to { 31 | transform: rotate(360deg); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /example/src/App.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { Admin, Resource } from 'react-admin'; 3 | import { EntityCreate, EntityEdit, EntityList, EntityShow } from './entities/WithSingleMarker'; 4 | import { CreateEntity, EditEntity, ListEntity, ShowEntity } from './entities/WithMultipleMarkers'; 5 | import jsonRestProvider from 'ra-data-fakerest'; 6 | import data from './mocks/data'; 7 | 8 | 9 | const dataProvider = jsonRestProvider(data, true); 10 | const App = () => ( 11 | 12 | 13 | 14 | 15 | ); 16 | 17 | export default App; -------------------------------------------------------------------------------- /example/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | ReactDOM.unmountComponentAtNode(div); 9 | }); 10 | -------------------------------------------------------------------------------- /example/src/entities/WithMultipleMarkers.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { 3 | List, 4 | Datagrid, 5 | TextField, 6 | Edit, 7 | SimpleForm, 8 | TextInput, 9 | NumberInput, 10 | Create, 11 | EditButton, 12 | Show, 13 | SimpleShowLayout, 14 | } from 'react-admin'; 15 | import { GMapInput, GMapField } from '@fusionworks/ra-google-maps-input'; 16 | 17 | export const ListEntity = props => { 18 | return ( 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | ) 27 | }; 28 | 29 | export const EditEntity = props => ( 30 | 31 | 32 | 33 | 34 | 39 | 40 | 41 | ); 42 | 43 | export const ShowEntity = props => ( 44 | 45 | 46 | 47 | 48 | 52 | 53 | 54 | ) 55 | 56 | export const CreateEntity = props => ( 57 | 58 | 59 | 60 | 61 | 66 | 67 | 68 | ); -------------------------------------------------------------------------------- /example/src/entities/WithSingleMarker.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { 3 | List, 4 | Datagrid, 5 | TextField, 6 | Edit, 7 | SimpleForm, 8 | TextInput, 9 | NumberInput, 10 | Create, 11 | Show, 12 | SimpleShowLayout, 13 | EditButton, 14 | } from 'react-admin'; 15 | import { GMapInput, GMapField } from '@fusionworks/ra-google-maps-input'; 16 | 17 | export const EntityList = props => ( 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | ); 26 | 27 | export const EntityEdit = props => ( 28 | 29 | 30 | 31 | 32 | 37 | 38 | 39 | ); 40 | 41 | export const EntityShow = props => ( 42 | 43 | 44 | 45 | 46 | 50 | 51 | 52 | ) 53 | 54 | export const EntityCreate = props => ( 55 | 56 | 57 | 58 | 59 | 64 | 65 | 66 | ); -------------------------------------------------------------------------------- /example/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 4 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /example/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import * as serviceWorker from './serviceWorker'; 6 | 7 | ReactDOM.render(, document.getElementById('root')); 8 | 9 | // If you want your app to work offline and load faster, you can change 10 | // unregister() to register() below. Note this comes with some pitfalls. 11 | // Learn more about service workers: https://bit.ly/CRA-PWA 12 | serviceWorker.unregister(); 13 | -------------------------------------------------------------------------------- /example/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/src/mocks/data.js: -------------------------------------------------------------------------------- 1 | export default { 2 | singleMarker: [ 3 | { 4 | id: 1, 5 | name: "test", 6 | coordinates: { 7 | lat: 48.1351253, 8 | lng: 11.581980499999986 9 | } 10 | }, 11 | { 12 | id: 2, 13 | name: "test2", 14 | coordinates: { 15 | lat: 4.1351253, 16 | lng: 11.581980499999986 17 | } 18 | } 19 | ], 20 | multipleMarkers: [ 21 | { 22 | id: 1, 23 | name: "TEST1", 24 | coordinates : [ 25 | { 26 | lng: 4.21875, 27 | lat: 20.632784250388028 28 | }, 29 | { 30 | lng: 27.24609375, 31 | lat: 25.005972656239187 32 | } 33 | ] 34 | } 35 | ] 36 | } -------------------------------------------------------------------------------- /example/src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.1/8 is considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl) 104 | .then(response => { 105 | // Ensure service worker exists, and that we really are getting a JS file. 106 | const contentType = response.headers.get('content-type'); 107 | if ( 108 | response.status === 404 || 109 | (contentType != null && contentType.indexOf('javascript') === -1) 110 | ) { 111 | // No service worker found. Probably a different app. Reload the page. 112 | navigator.serviceWorker.ready.then(registration => { 113 | registration.unregister().then(() => { 114 | window.location.reload(); 115 | }); 116 | }); 117 | } else { 118 | // Service worker found. Proceed as normal. 119 | registerValidSW(swUrl, config); 120 | } 121 | }) 122 | .catch(() => { 123 | console.log( 124 | 'No internet connection found. App is running in offline mode.' 125 | ); 126 | }); 127 | } 128 | 129 | export function unregister() { 130 | if ('serviceWorker' in navigator) { 131 | navigator.serviceWorker.ready.then(registration => { 132 | registration.unregister(); 133 | }); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /lib/copy_package.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | 3 | fs.copyFile('./package.json', './dist/package.json', (err) => { 4 | if (err) { 5 | throw err; 6 | } 7 | console.log('package.json was copied to destination folder.'); 8 | }); -------------------------------------------------------------------------------- /lib/copy_readme.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | 3 | fs.copyFile('./README.md', './dist/README.md', (err) => { 4 | if (err) { 5 | throw err; 6 | } 7 | console.log('README.md was copied to destination folder.'); 8 | }); -------------------------------------------------------------------------------- /lib/test.js: -------------------------------------------------------------------------------- 1 | console.log('Nothing to test... yet.'); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@fusionworks/ra-google-maps-input", 3 | "version": "0.1.7", 4 | "author": "nzuza", 5 | "private": false, 6 | "main": "index.js", 7 | "homepage": "https://github.com/FusionWorks/react-admin-google-maps#readme", 8 | "description": "React-admin field/input to show markers on a map.", 9 | "license": "ISC", 10 | "scripts": { 11 | "build": "webpack --mode production", 12 | "build:dev": "webpack --mode development --watch", 13 | "postbuild": "node lib/copy_package && node lib/copy_readme", 14 | "patch": "npm version patch -m \"[skip travis] Release version: %s\"", 15 | "prerelease": "npm whoami && npm run patch && npm run build", 16 | "release": "npm publish dist --access public" 17 | }, 18 | "repository": { 19 | "type": "git", 20 | "url": "git+https://github.com/FusionWorks/react-admin-google-maps.git" 21 | }, 22 | "bugs": { 23 | "url": "https://github.com/FusionWorks/react-admin-google-maps/issues" 24 | }, 25 | "keywords": [ 26 | "ra", 27 | "react-admin", 28 | "react-google-maps", 29 | "react-admin input", 30 | "react-admin map input", 31 | "ra-input", 32 | "google, maps" 33 | ], 34 | "dependencies": { 35 | "react-google-maps": "^9.4.5", 36 | "react": "^16.8.6", 37 | "react-dom": "^16.8.6", 38 | "ra-core": "^2.9.2" 39 | }, 40 | "devDependencies": { 41 | "@babel/cli": "^7.0.0-beta.46", 42 | "@babel/core": "^7.0.0-beta.46", 43 | "@babel/plugin-proposal-class-properties": "^7.0.0-beta.46", 44 | "@babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.46", 45 | "@babel/preset-env": "^7.0.0-beta.46", 46 | "@babel/preset-react": "^7.0.0-beta.46", 47 | "babel-loader": "^8.0.0-beta.0", 48 | "react-scripts": "3.0.1", 49 | "webpack": "^4.33.0", 50 | "webpack-cli": "^3.3.2" 51 | }, 52 | "browserslist": { 53 | "production": [ 54 | ">0.2%", 55 | "not dead", 56 | "not op_mini all" 57 | ], 58 | "development": [ 59 | "last 1 chrome version", 60 | "last 1 firefox version", 61 | "last 1 safari version" 62 | ] 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /scripts/publish.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | PACKAGE_VERSION=$(node -p -e "require('./package.json').version") 3 | REMOTE_VERSION=$(npm show @fusionworks/ra-google-maps-input version) 4 | LAST_AUTHOR="$(git log -1 --pretty=format:'%an')" 5 | echo "Local version: $PACKAGE_VERSION" 6 | echo "Remote version: $REMOTE_VERSION" 7 | 8 | if [[ $PACKAGE_VERSION != $REMOTE_VERSION ]]; then 9 | echo "Version mismatch. Skip deploy." 10 | elif [[ $LAST_AUTHOR == "Travis CI User" ]]; then 11 | echo "Skip deploy as last commit was made by travis." 12 | elif [[ $TRAVIS_PULL_REQUEST = false && $TRAVIS_BRANCH == "master" ]]; then 13 | git checkout master 14 | git pull origin master 15 | npm run release 16 | # https://github.com/FusionWorks/react-admin-google-maps 17 | git push "https://${GITHUB_TOKEN}@github.com/FusionWorks/react-admin-google-maps.git" master 18 | 19 | git checkout - 20 | else 21 | echo "Thats not master or pull-request to master. Skip deploy." 22 | fi -------------------------------------------------------------------------------- /src/Map.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { 3 | withGoogleMap, 4 | withScriptjs, 5 | Marker, 6 | GoogleMap, 7 | } from 'react-google-maps'; 8 | 9 | const Map = withScriptjs(withGoogleMap( 10 | props => { 11 | const { 12 | center, 13 | onMapClick, 14 | onMarkerClick, 15 | markers, 16 | defaultZoom, 17 | } = props; 18 | 19 | const putMarkers = () => { 20 | if (!markers) { 21 | return; 22 | } 23 | 24 | if (markers instanceof Array) { 25 | return markers.map((mrk, i) => ( 26 | onMarkerClick(e)} 30 | /> 31 | )); 32 | } 33 | 34 | return ( 35 | onMarkerClick(e)} 39 | /> 40 | ); 41 | }; 42 | 43 | return ( 44 | onMapClick(e)} 48 | > 49 | {putMarkers()} 50 | 51 | ); 52 | }, 53 | )); 54 | 55 | export default Map; 56 | -------------------------------------------------------------------------------- /src/SearchBox.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { withScriptjs } from 'react-google-maps'; 3 | import { StandaloneSearchBox } from 'react-google-maps/lib/components/places/StandaloneSearchBox'; 4 | 5 | 6 | class SearchBox extends Component { 7 | constructor(props) { 8 | super(props); 9 | this.input = React.createRef(); 10 | 11 | this.onPlacesChanged = () => { 12 | const { input, multipleMarkers, putMarker } = this.props; 13 | const places = this.input.current.getPlaces(); 14 | const markerPos = { 15 | lat: places[0].geometry.location.lat(), 16 | lng: places[0].geometry.location.lng(), 17 | }; 18 | putMarker({ markerPos, input, multipleMarkers }); 19 | }; 20 | 21 | this.defaultSearchStyles = { 22 | boxSizing: 'border-box', 23 | border: '1px solid transparent', 24 | width: '240px', 25 | height: '32px', 26 | padding: '0 12px', 27 | borderRadius: '3px', 28 | boxShadow: '0 2px 6px rgba(0, 0, 0, 0.3)', 29 | fontSize: '14px', 30 | outline: 'none', 31 | textOverflow: 'ellipses', 32 | marginBottom: '15px', 33 | position: 'absolute', 34 | top: '15px', 35 | left: '0px', 36 | right: '0px', 37 | margin: '0 auto', 38 | zIndex: '1', 39 | }; 40 | } 41 | 42 | render() { 43 | const { props, onPlacesChanged, input } = this; 44 | 45 | return ( 46 |
47 | 52 | 59 | 60 |
61 | ); 62 | } 63 | } 64 | 65 | const WithScriptSearchBox = withScriptjs(props => ); 66 | 67 | export default WithScriptSearchBox; 68 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React, { Component, Fragment } from 'react'; 2 | import { addField } from 'ra-core'; 3 | import Map from './Map'; 4 | import SearchBox from './SearchBox'; 5 | import { getMarkers, getPosition } from './utils'; 6 | 7 | export default class GMap extends Component { 8 | constructor(props) { 9 | super(props); 10 | const { defaultZoom, defaultCenter } = this.props; 11 | 12 | this.state = { 13 | center: defaultCenter || { lat: 0, lng: 0 }, 14 | zoom: defaultZoom || 3, 15 | }; 16 | 17 | this.putMarker = ({ markerPos, input, multipleMarkers }) => { 18 | const currentValue = getMarkers(input); 19 | if (multipleMarkers) { 20 | if (currentValue && currentValue !== null) { 21 | return input.onChange([...currentValue, markerPos]); 22 | } 23 | return input.onChange([markerPos]); 24 | } 25 | return input.onChange(markerPos); 26 | }; 27 | 28 | this.setCenter = markerPos => this.setState({ center: markerPos }); 29 | 30 | this.putMarkerFromSearch = ({ 31 | markerPos, input, justShow, multipleMarkers, 32 | }) => { 33 | this.putMarker({ markerPos, input, multipleMarkers }); 34 | this.setCenter(markerPos); 35 | }; 36 | 37 | this.deleteMarker = ({ markerPos, input, multipleMarkers }) => { 38 | const currentValue = getMarkers(input); 39 | let newValue; 40 | if (multipleMarkers) { 41 | newValue = currentValue.filter(mrk => mrk.lat !== markerPos.lat 42 | && mrk.lng !== markerPos.lng); 43 | if (!newValue.length) { 44 | newValue = null; 45 | } 46 | } else { newValue = null; } 47 | input.onChange(newValue); 48 | }; 49 | } 50 | 51 | componentDidMount() { 52 | const { input } = this.props; 53 | const markers = getMarkers(input); 54 | if (markers) { 55 | if (markers instanceof Array) { 56 | this.setState({ center: markers[markers.length - 1] }); 57 | } else { 58 | this.setState({ center: markers }); 59 | } 60 | } 61 | } 62 | 63 | render() { 64 | const { 65 | googleKey, 66 | input, 67 | multipleMarkers, 68 | searchable, 69 | justShow, 70 | } = this.props; 71 | const childrenProps = { 72 | input, 73 | markers: getMarkers(input), 74 | multipleMarkers, 75 | loadingElement:
, 76 | containerElement:
, 77 | googleMapURL: `https://maps.googleapis.com/maps/api/js?key=${ 78 | googleKey 79 | }&v=3.exp&libraries=geometry,drawing,places`, 80 | }; 81 | 82 | const { center, zoom } = this.state; 83 | 84 | return ( 85 | 86 |
87 | {!!searchable 88 | && ( 89 | { }} 91 | deleteMarker={!justShow ? this.deleteMarker : () => { }} 92 | input={input} 93 | markers={getMarkers(input)} 94 | multipleMarkers={multipleMarkers} 95 | loadingElement={
} 96 | containerElement={
} 97 | googleMapURL={`https://maps.googleapis.com/maps/api/js?key=${ 98 | googleKey 99 | }&v=3.exp&libraries=geometry,drawing,places`} 100 | /> 101 | ) 102 | } 103 | } 105 | center={center} 106 | defaultZoom={zoom} 107 | onMapClick={e => this.putMarker({ 108 | input, 109 | multipleMarkers, 110 | markerPos: getPosition(e), 111 | })} 112 | onMarkerClick={e => this.deleteMarker({ 113 | input, 114 | multipleMarkers, 115 | markerPos: getPosition(e), 116 | })} 117 | {...childrenProps} 118 | /> 119 |
120 | 121 | ); 122 | } 123 | } 124 | 125 | export const GMapInput = addField(GMap); 126 | export const GMapField = ({ record, source, ...props }) => ( 127 | 133 | ); 134 | -------------------------------------------------------------------------------- /src/utils.js: -------------------------------------------------------------------------------- 1 | 2 | export const getMarkers = input => input.value || null; 3 | 4 | export const getPosition = e => ({ 5 | lng: e.latLng.lng(), 6 | lat: e.latLng.lat(), 7 | }); 8 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | 3 | module.exports = { 4 | entry: path.join(__dirname, 'src/'), 5 | output: { 6 | path: path.join(__dirname, 'dist'), 7 | filename: 'index.js', 8 | library: '', 9 | libraryTarget: 'commonjs-module' 10 | }, 11 | module: { 12 | rules: [ 13 | { 14 | test: /\.(js|jsx)$/, 15 | exclude: /node_modules/, 16 | use: { 17 | loader: 'babel-loader', 18 | options: { 19 | presets: ["@babel/env", "@babel/react"], 20 | plugins: [ 21 | "@babel/plugin-proposal-object-rest-spread", 22 | "@babel/plugin-proposal-class-properties" 23 | ] 24 | } 25 | }, 26 | } 27 | ] 28 | }, 29 | resolve: { 30 | extensions: ['.js', '.jsx'], 31 | }, 32 | } --------------------------------------------------------------------------------