@placekit/autocomplete-react
Advanced tools
Comparing version 1.0.0-alpha.8 to 1.0.0-alpha.9
@@ -1,2 +0,2 @@ | ||
/*! @placekit/autocomplete-react v1.0.0-alpha.8 | © placekit.io | MIT license | https://github.com/placekit/autocomplete-react#readme */ | ||
/*! @placekit/autocomplete-react v1.0.0-alpha.9 | © placekit.io | MIT license | https://github.com/placekit/autocomplete-react#readme */ | ||
'use strict'; | ||
@@ -15,4 +15,7 @@ | ||
const [client, setClient] = React.useState(); | ||
const [isFreeForm, setIsFreeForm] = React.useState(true); | ||
const [hasGeolocation, setHasGeolocation] = React.useState(false); | ||
const [state, setState] = React.useState({ | ||
isEmpty: true, | ||
isFreeForm: true, | ||
hasGeolocation: false | ||
}); | ||
React.useEffect(() => { | ||
@@ -29,12 +32,26 @@ if (!target.current) { | ||
...opts | ||
}).on('open', handlers?.onOpen).on('close', handlers?.onClose).on('results', handlers?.onResults).on('pick', handlers?.onPick).on('error', handlers?.onError).on('geolocation', (bool, pos) => { | ||
setHasGeolocation(bool); | ||
if (handlers?.onGeolocation) { | ||
handlers.onGeolocation(bool, pos); | ||
}).on('open', handlers?.onOpen).on('close', handlers?.onClose).on('results', handlers?.onResults).on('pick', handlers?.onPick).on('error', handlers?.onError).on('empty', bool => { | ||
setState(prev => ({ | ||
...prev, | ||
isEmpty: bool | ||
})); | ||
if (handlers?.onEmpty) { | ||
handlers.onEmpty(bool); | ||
} | ||
}).on('freeForm', bool => { | ||
setIsFreeForm(bool); | ||
setState(prev => ({ | ||
...prev, | ||
isFreeForm: bool | ||
})); | ||
if (handlers?.onFreeForm) { | ||
handlers.onFreeForm(bool); | ||
} | ||
}).on('geolocation', (bool, pos) => { | ||
setState(prev => ({ | ||
...prev, | ||
hasGeolocation: bool | ||
})); | ||
if (handlers?.onGeolocation) { | ||
handlers.onGeolocation(bool, pos); | ||
} | ||
}); | ||
@@ -50,4 +67,3 @@ setClient(pka); | ||
client, | ||
isFreeForm, | ||
hasGeolocation | ||
state | ||
}; | ||
@@ -87,3 +103,3 @@ }; | ||
client, | ||
hasGeolocation | ||
state | ||
} = usePlaceKit(apiKey, opts); | ||
@@ -103,6 +119,6 @@ React.useEffect(() => { | ||
type: "button", | ||
className: ['pka-input-geolocation', hasGeolocation ? 'pka-enabled' : ''].filter(c => c).join(' '), | ||
className: ['pka-input-geolocation', state.hasGeolocation ? 'pka-enabled' : ''].filter(c => c).join(' '), | ||
title: "Activate geolocation", | ||
role: "switch", | ||
"aria-checked": hasGeolocation, | ||
"aria-checked": state.hasGeolocation, | ||
onClick: client?.requestGeolocation, | ||
@@ -116,3 +132,3 @@ disabled: inputProps.disabled | ||
title: "Clear value", | ||
"aria-hidden": !target.current?.value, | ||
"aria-hidden": state.isEmpty, | ||
onClick: client?.clear, | ||
@@ -156,4 +172,5 @@ disabled: inputProps.disabled | ||
onError: PropTypes.func, | ||
onGeolocation: PropTypes.func, | ||
onFreeForm: PropTypes.func | ||
onEmpty: PropTypes.func, | ||
onFreeForm: PropTypes.func, | ||
onGeolocation: PropTypes.func | ||
}; | ||
@@ -160,0 +177,0 @@ |
@@ -1,2 +0,2 @@ | ||
/*! @placekit/autocomplete-react v1.0.0-alpha.8 | © placekit.io | MIT license | https://github.com/placekit/autocomplete-react#readme */ | ||
/*! @placekit/autocomplete-react v1.0.0-alpha.9 | © placekit.io | MIT license | https://github.com/placekit/autocomplete-react#readme */ | ||
import type { PKAClient, PKAHandlers, PKAOptions } from '@placekit/autocomplete-js'; | ||
@@ -10,2 +10,3 @@ | ||
onError: PKAHandlers['error']; | ||
onEmpty: PKAHandlers['empty']; | ||
onFreeForm: PKAHandlers['freeForm']; | ||
@@ -27,6 +28,9 @@ onGeolocation: PKAHandlers['geolocation']; | ||
export type PlaceKitHooks = { | ||
target: React.RefObject<HTMLInputElement>, | ||
client: PKAClient, | ||
isFreeForm: boolean; | ||
hasGeolocation: boolean; | ||
target: React.RefObject<HTMLInputElement>; | ||
client: PKAClient; | ||
state: { | ||
isEmpty: boolean; | ||
isFreeForm: boolean; | ||
hasGeolocation: boolean; | ||
}; | ||
}; | ||
@@ -33,0 +37,0 @@ |
@@ -1,2 +0,2 @@ | ||
/*! @placekit/autocomplete-react v1.0.0-alpha.8 | © placekit.io | MIT license | https://github.com/placekit/autocomplete-react#readme */ | ||
/*! @placekit/autocomplete-react v1.0.0-alpha.9 | © placekit.io | MIT license | https://github.com/placekit/autocomplete-react#readme */ | ||
import _extends from '@babel/runtime/helpers/extends'; | ||
@@ -13,4 +13,7 @@ import PropTypes from 'prop-types'; | ||
const [client, setClient] = useState(); | ||
const [isFreeForm, setIsFreeForm] = useState(true); | ||
const [hasGeolocation, setHasGeolocation] = useState(false); | ||
const [state, setState] = useState({ | ||
isEmpty: true, | ||
isFreeForm: true, | ||
hasGeolocation: false | ||
}); | ||
useEffect(() => { | ||
@@ -27,12 +30,26 @@ if (!target.current) { | ||
...opts | ||
}).on('open', handlers?.onOpen).on('close', handlers?.onClose).on('results', handlers?.onResults).on('pick', handlers?.onPick).on('error', handlers?.onError).on('geolocation', (bool, pos) => { | ||
setHasGeolocation(bool); | ||
if (handlers?.onGeolocation) { | ||
handlers.onGeolocation(bool, pos); | ||
}).on('open', handlers?.onOpen).on('close', handlers?.onClose).on('results', handlers?.onResults).on('pick', handlers?.onPick).on('error', handlers?.onError).on('empty', bool => { | ||
setState(prev => ({ | ||
...prev, | ||
isEmpty: bool | ||
})); | ||
if (handlers?.onEmpty) { | ||
handlers.onEmpty(bool); | ||
} | ||
}).on('freeForm', bool => { | ||
setIsFreeForm(bool); | ||
setState(prev => ({ | ||
...prev, | ||
isFreeForm: bool | ||
})); | ||
if (handlers?.onFreeForm) { | ||
handlers.onFreeForm(bool); | ||
} | ||
}).on('geolocation', (bool, pos) => { | ||
setState(prev => ({ | ||
...prev, | ||
hasGeolocation: bool | ||
})); | ||
if (handlers?.onGeolocation) { | ||
handlers.onGeolocation(bool, pos); | ||
} | ||
}); | ||
@@ -48,4 +65,3 @@ setClient(pka); | ||
client, | ||
isFreeForm, | ||
hasGeolocation | ||
state | ||
}; | ||
@@ -85,3 +101,3 @@ }; | ||
client, | ||
hasGeolocation | ||
state | ||
} = usePlaceKit(apiKey, opts); | ||
@@ -101,6 +117,6 @@ useEffect(() => { | ||
type: "button", | ||
className: ['pka-input-geolocation', hasGeolocation ? 'pka-enabled' : ''].filter(c => c).join(' '), | ||
className: ['pka-input-geolocation', state.hasGeolocation ? 'pka-enabled' : ''].filter(c => c).join(' '), | ||
title: "Activate geolocation", | ||
role: "switch", | ||
"aria-checked": hasGeolocation, | ||
"aria-checked": state.hasGeolocation, | ||
onClick: client?.requestGeolocation, | ||
@@ -114,3 +130,3 @@ disabled: inputProps.disabled | ||
title: "Clear value", | ||
"aria-hidden": !target.current?.value, | ||
"aria-hidden": state.isEmpty, | ||
onClick: client?.clear, | ||
@@ -154,6 +170,7 @@ disabled: inputProps.disabled | ||
onError: PropTypes.func, | ||
onGeolocation: PropTypes.func, | ||
onFreeForm: PropTypes.func | ||
onEmpty: PropTypes.func, | ||
onFreeForm: PropTypes.func, | ||
onGeolocation: PropTypes.func | ||
}; | ||
export { PlaceKit, usePlaceKit }; |
{ | ||
"name": "@placekit/autocomplete-react", | ||
"version": "1.0.0-alpha.8", | ||
"version": "1.0.0-alpha.9", | ||
"author": "PlaceKit <support@placekit.io>", | ||
@@ -40,19 +40,19 @@ "description": "PlaceKit Autocomplete React library", | ||
"devDependencies": { | ||
"@babel/core": "^7.20.5", | ||
"@babel/core": "^7.20.12", | ||
"@babel/plugin-transform-runtime": "^7.19.6", | ||
"@babel/preset-react": "^7.18.6", | ||
"@babel/runtime": "^7.20.6", | ||
"@babel/runtime": "^7.20.7", | ||
"@rollup/plugin-babel": "^6.0.3", | ||
"@rollup/plugin-commonjs": "^23.0.4", | ||
"@rollup/plugin-commonjs": "^24.0.0", | ||
"@rollup/plugin-node-resolve": "^15.0.1", | ||
"eslint": "^8.29.0", | ||
"eslint": "^8.32.0", | ||
"eslint-config-google": "^0.14.0", | ||
"eslint-plugin-react": "^7.31.11", | ||
"eslint-plugin-react": "^7.32.0", | ||
"npm-watch": "^0.11.0", | ||
"rimraf": "^3.0.2", | ||
"rollup": "^3.7.4", | ||
"rimraf": "^4.0.7", | ||
"rollup": "^3.10.0", | ||
"rollup-plugin-copy": "^3.4.0" | ||
}, | ||
"dependencies": { | ||
"@placekit/autocomplete-js": "^1.0.0-alpha.7", | ||
"@placekit/autocomplete-js": "^1.0.0-alpha.9", | ||
"@types/react": "^18.0.26", | ||
@@ -59,0 +59,0 @@ "prop-types": "^15.8.1" |
@@ -87,2 +87,3 @@ <h1 align="center"> | ||
onError={(error) => {}} | ||
onEmpty={(isEmpty) => {}} | ||
onFreeForm={(isFreeForm) => {}} | ||
@@ -115,3 +116,3 @@ onGeolocation={(hasGeolocation, position) => {}} | ||
const MyComponent = (props) => { | ||
const { target, client, isFreeForm, hasGeolocation } = usePlaceKit('<your-api-key>', {}); | ||
const { target, client, state } = usePlaceKit('<your-api-key>', {}); | ||
@@ -130,3 +131,3 @@ return ( | ||
- Don't pass a destructured object into `usePlaceKit` second argument (options), this will cause an infinite update loop as a destructured object will constantly be a fresh new value by nature. | ||
- `isFreeForm` and `hasGeolocation` are separately set inside the hook, as `client.isFreeForm` and `client.hasGeolocation` are stateless. | ||
- `state` exposes stateless client properties (`isEmpty`, `isFreeForm` and `hasGeolocation`) as stateful ones. | ||
@@ -133,0 +134,0 @@ ⚠️ **NOTE:** you are **not** allowed to hide the PlaceKit logo unless we've delivered a special authorization. To request one, please contact us using [our contact form](https://placekit.io/about#contact) |
19021
365
136