generated from graasp/graasp-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: export GeolocationPicker, improve map (#60)
- Loading branch information
Showing
40 changed files
with
2,802 additions
and
813 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ node_modules | |
.husky | ||
.nyc_output | ||
.yarn | ||
storybook-static |
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,12 @@ | ||
export const MOCK_USE_SUGGESTIONS = ({ address }: { address: string }) => { | ||
if (address) { | ||
return { | ||
data: [ | ||
{ addressLabel: 'suggestion 1' }, | ||
{ addressLabel: 'suggestion 2' }, | ||
{ addressLabel: 'suggestion 3' }, | ||
], | ||
}; | ||
} | ||
return { data: undefined }; | ||
}; |
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
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
99 changes: 99 additions & 0 deletions
99
src/components/GeolocationPicker/GeolocationPicker.stories.tsx
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,99 @@ | ||
import { expect } from '@storybook/jest'; | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { userEvent, within } from '@storybook/testing-library'; | ||
import type { BoundFunctions } from '@testing-library/dom'; | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { queries } from '@testing-library/dom'; | ||
|
||
import { MOCK_USE_SUGGESTIONS } from '../../../.storybook/fixtures'; | ||
import GeolocationPicker from './GeolocationPicker'; | ||
|
||
const meta = { | ||
title: 'GeolocationPicker', | ||
component: GeolocationPicker, | ||
|
||
argTypes: { | ||
onChangeOption: { | ||
action: 'choose option', | ||
table: { | ||
category: 'events', | ||
}, | ||
}, | ||
}, | ||
} satisfies Meta<typeof GeolocationPicker>; | ||
export default meta; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
const checkSuggestions = async (canvas: BoundFunctions<typeof queries>) => { | ||
// suggestions are showing | ||
await userEvent.type(canvas.getByLabelText('Location'), 'my address'); | ||
const suggestions = MOCK_USE_SUGGESTIONS({ address: 'query' }).data; | ||
suggestions?.forEach((value) => { | ||
expect(canvas.getByText(value.addressLabel)).toBeVisible(); | ||
}); | ||
|
||
// select a suggestion | ||
const toSelect = suggestions![0].addressLabel; | ||
await userEvent.click(canvas.getByText(toSelect)); | ||
expect(canvas.getByLabelText('Location')).toHaveTextContent(toSelect); | ||
}; | ||
|
||
export const Default = { | ||
args: { | ||
useSuggestionsForAddress: MOCK_USE_SUGGESTIONS as any, | ||
}, | ||
play: async ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
|
||
await checkSuggestions(canvas); | ||
}, | ||
} satisfies Story; | ||
|
||
export const InitialValue = { | ||
args: { | ||
useSuggestionsForAddress: MOCK_USE_SUGGESTIONS as any, | ||
initialValue: 'initial value', | ||
}, | ||
play: async ({ canvasElement, args }) => { | ||
const canvas = within(canvasElement); | ||
await expect(canvas.getByLabelText('Location')).toHaveTextContent( | ||
args.initialValue!, | ||
); | ||
|
||
await checkSuggestions(canvas); | ||
}, | ||
} satisfies Story; | ||
|
||
export const Background = { | ||
args: { | ||
useSuggestionsForAddress: MOCK_USE_SUGGESTIONS as any, | ||
initialValue: 'initial value', | ||
}, | ||
parameters: { | ||
backgrounds: { | ||
default: 'default', | ||
values: [{ name: 'default', value: '#00aced' }], | ||
}, | ||
}, | ||
play: async ({ canvasElement, args }) => { | ||
const canvas = within(canvasElement); | ||
await expect(canvas.getByLabelText('Location')).toHaveTextContent( | ||
args.initialValue!, | ||
); | ||
|
||
await checkSuggestions(canvas); | ||
}, | ||
} satisfies Story; | ||
|
||
export const Invisible = { | ||
args: { | ||
useSuggestionsForAddress: MOCK_USE_SUGGESTIONS as any, | ||
invisible: true, | ||
}, | ||
play: async ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
|
||
await checkSuggestions(canvas); | ||
}, | ||
} satisfies Story; |
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,137 @@ | ||
import { ChangeEventHandler, useEffect, useState } from 'react'; | ||
|
||
import { | ||
Box, | ||
LinearProgress, | ||
List, | ||
ListItemButton, | ||
TextField, | ||
} from '@mui/material'; | ||
|
||
import { DEFAULT_LANG } from '@graasp/translations'; | ||
|
||
import i18n, { useMapTranslation } from '../../config/i18n'; | ||
import { MAP } from '../../langs/constants'; | ||
import { QueryClientContextInterface } from '../context/QueryClientContext'; | ||
import { useOutsideClick } from './hook'; | ||
|
||
export type GeolocationPickerProps = { | ||
disabled?: boolean; | ||
useSuggestionsForAddress: QueryClientContextInterface['useSuggestionsForAddress']; | ||
onChangeOption?: (args: { | ||
addressLabel: string; | ||
lat: number; | ||
lng: number; | ||
country?: string; | ||
}) => void; | ||
invisible?: boolean; | ||
initialValue?: string; | ||
endAdornment?: JSX.Element; | ||
}; | ||
|
||
const GeolocationPicker = ({ | ||
disabled = false, | ||
onChangeOption, | ||
useSuggestionsForAddress, | ||
invisible = false, | ||
initialValue = '', | ||
}: GeolocationPickerProps): JSX.Element => { | ||
const { t } = useMapTranslation(); | ||
|
||
const [showSuggestions, setShowSuggestions] = useState(false); | ||
const [query, setQuery] = useState<string | undefined>(initialValue); | ||
const [selectedAddress, setSelectedAddress] = useState<string | undefined>(); | ||
const { data: suggestions, isFetching } = useSuggestionsForAddress({ | ||
address: query !== initialValue ? query : undefined, | ||
lang: i18n.language ?? DEFAULT_LANG, | ||
}); | ||
const ref = useOutsideClick(() => { | ||
setShowSuggestions(false); | ||
}); | ||
|
||
useEffect(() => { | ||
if (initialValue !== query) { | ||
setQuery(initialValue); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [initialValue]); | ||
|
||
const onChange: ChangeEventHandler<HTMLInputElement> = (e) => { | ||
setQuery(e.target.value); | ||
setSelectedAddress(undefined); | ||
}; | ||
|
||
const handleChangeOption = (option: { | ||
addressLabel: string; | ||
lat: number; | ||
lng: number; | ||
country?: string; | ||
}): void => { | ||
onChangeOption?.(option); | ||
|
||
setSelectedAddress(option.addressLabel); | ||
setQuery(undefined); | ||
}; | ||
|
||
return ( | ||
<Box sx={{ position: 'relative', width: '100%' }} ref={ref}> | ||
<TextField | ||
disabled={disabled} | ||
fullWidth | ||
label="Location" | ||
multiline | ||
placeholder={t(MAP.GEOLOCATION_PICKER_PLACEHOLDER)} | ||
onChange={onChange} | ||
onFocus={() => setShowSuggestions(true)} | ||
value={selectedAddress ?? query} | ||
sx={{ minWidth: 250 }} | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...(invisible | ||
? { | ||
variant: 'standard', | ||
InputLabelProps: { | ||
shrink: true, | ||
}, | ||
InputProps: { | ||
disableUnderline: true, | ||
}, | ||
} | ||
: {})} | ||
/> | ||
{isFetching && query && ( | ||
<Box sx={{ width: '100%' }}> | ||
<LinearProgress /> | ||
</Box> | ||
)} | ||
|
||
{showSuggestions && suggestions && !selectedAddress && ( | ||
<List | ||
sx={{ | ||
position: 'absolute', | ||
background: 'white', | ||
top: 70, | ||
width: '100%', | ||
}} | ||
> | ||
{suggestions.map((r) => ( | ||
<ListItemButton | ||
key={r.id} | ||
onMouseDown={() => { | ||
handleChangeOption(r); | ||
setShowSuggestions(false); | ||
}} | ||
> | ||
{r.addressLabel} | ||
</ListItemButton> | ||
))} | ||
{!suggestions.length && | ||
query && | ||
!isFetching && | ||
t(MAP.GEOLOCATION_PICKER_NO_ADDRESS)} | ||
</List> | ||
)} | ||
</Box> | ||
); | ||
}; | ||
|
||
export default GeolocationPicker; |
Oops, something went wrong.