-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#1290383): [Example App] Add latitude and Longitude Field in a S…
…ettings Component
- Loading branch information
1 parent
3a3cbc3
commit 3a1bd72
Showing
7 changed files
with
208 additions
and
7 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
29 changes: 29 additions & 0 deletions
29
front/example-app/src/components/Providers/SettingsProvider/SettingsProvider.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,29 @@ | ||
import React, { ReactNode, useMemo, useState } from 'react' | ||
import { ISettingsContext, locationContext } from '../../../contexts' | ||
|
||
interface IProps { | ||
children: ReactNode | ||
} | ||
|
||
function SettingsProvider({ children }: IProps): JSX.Element { | ||
const [longitude, setLongitude] = useState<string>('') | ||
const [latitude, setLatitude] = useState<string>('') | ||
|
||
const context = useMemo<ISettingsContext>( | ||
() => ({ | ||
longitude, | ||
latitude, | ||
setLatitude, | ||
setLongitude, | ||
}), | ||
[longitude, latitude, setLatitude, setLongitude] | ||
) | ||
|
||
return ( | ||
<locationContext.Provider value={context}> | ||
{children} | ||
</locationContext.Provider> | ||
) | ||
} | ||
|
||
export default SettingsProvider |
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,136 @@ | ||
import { | ||
Button, | ||
Fade, | ||
IconButton, | ||
Paper, | ||
Popper, | ||
TextField, | ||
styled, | ||
} from '@mui/material' | ||
import SettingsIcon from '@mui/icons-material/Settings' | ||
import React, { | ||
FormEvent, | ||
useContext, | ||
useEffect, | ||
useRef, | ||
useState, | ||
} from 'react' | ||
import { locationContext } from 'src/contexts' | ||
|
||
function Settings(): JSX.Element { | ||
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null) | ||
const formRef = useRef<HTMLFormElement>(null) | ||
const [open, setOpen] = useState<boolean>(false) | ||
const popperRef = useRef<HTMLDivElement>(null) | ||
const handleClick = (event: React.MouseEvent<HTMLElement>): void => { | ||
setAnchorEl(anchorEl ? null : event.currentTarget) | ||
setOpen((curr) => !curr) | ||
} | ||
|
||
const { longitude, latitude, setLatitude, setLongitude } = | ||
useContext(locationContext) | ||
|
||
const id = open ? 'simple-popper' : undefined | ||
|
||
function handleSubmit(e: FormEvent<HTMLFormElement>): void { | ||
e.preventDefault() | ||
const { latitude, longitude } = Object.fromEntries( | ||
new FormData(formRef.current).entries() | ||
) | ||
if (latitude && longitude) { | ||
setLatitude(latitude.toString()) | ||
setLongitude(longitude.toString()) | ||
setOpen(false) | ||
setAnchorEl(null) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
function handleClickOutside(event: MouseEvent): void { | ||
if ( | ||
popperRef.current && | ||
!popperRef.current.contains(event.target as Node) && | ||
!anchorEl?.contains(event.target as Node) | ||
) { | ||
setOpen(false) | ||
setAnchorEl(null) | ||
} | ||
} | ||
|
||
if (open) { | ||
document.addEventListener('mousedown', handleClickOutside) | ||
} else { | ||
document.removeEventListener('mousedown', handleClickOutside) | ||
} | ||
|
||
return () => { | ||
document.removeEventListener('mousedown', handleClickOutside) | ||
} | ||
}, [open, anchorEl]) | ||
|
||
const CutomForm = styled('form')(({ theme }) => ({ | ||
padding: theme.spacing(2), | ||
display: 'flex', | ||
flexDirection: 'column', | ||
gap: theme.spacing(2), | ||
})) | ||
|
||
return ( | ||
<> | ||
<IconButton onClick={handleClick} sx={{ marginLeft: '20px' }}> | ||
<SettingsIcon htmlColor="#fff" /> | ||
</IconButton> | ||
<Popper | ||
id={id} | ||
open={open} | ||
anchorEl={anchorEl} | ||
ref={popperRef} | ||
sx={{ | ||
zIndex: 1200, | ||
}} | ||
transition | ||
> | ||
{({ TransitionProps }): JSX.Element => ( | ||
<Fade {...TransitionProps} timeout={350}> | ||
<Paper> | ||
<CutomForm ref={formRef} onSubmit={handleSubmit}> | ||
<TextField | ||
id="outlined-basic" | ||
label="Latitude" | ||
variant="outlined" | ||
name="latitude" | ||
type="number" | ||
defaultValue={latitude} | ||
inputProps={{ | ||
step: '0.000001', | ||
min: -90, | ||
max: 90, | ||
}} | ||
/> | ||
<TextField | ||
id="outlined-basic" | ||
label="Longitude" | ||
variant="outlined" | ||
type="number" | ||
name="longitude" | ||
defaultValue={longitude} | ||
inputProps={{ | ||
step: '0.000001', | ||
min: -180, | ||
max: 180, | ||
}} | ||
/> | ||
|
||
<Button type="submit" variant="outlined"> | ||
Appliquer | ||
</Button> | ||
</CutomForm> | ||
</Paper> | ||
</Fade> | ||
)} | ||
</Popper> | ||
</> | ||
) | ||
} | ||
|
||
export default Settings |
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,11 @@ | ||
import { Dispatch, SetStateAction, createContext } from 'react' | ||
|
||
export interface ISettingsContext { | ||
longitude: string | ||
latitude: string | ||
// onChange: (newLongitude: string, newLattitude: sting) => void, | ||
setLongitude: Dispatch<SetStateAction<ISettingsContext['longitude']>> | ||
setLatitude: Dispatch<SetStateAction<ISettingsContext['latitude']>> | ||
} | ||
|
||
export const locationContext = createContext<ISettingsContext>(null) |
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