-
Notifications
You must be signed in to change notification settings - Fork 21
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
03423a0
commit 3233c36
Showing
10 changed files
with
255 additions
and
10 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
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, settingsContext } 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 ( | ||
<settingsContext.Provider value={context}> | ||
{children} | ||
</settingsContext.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,139 @@ | ||
import { | ||
Button, | ||
Fade, | ||
IconButton, | ||
Paper, | ||
Popper, | ||
TextField, | ||
styled, | ||
} from '@mui/material' | ||
import SettingsIcon from '@mui/icons-material/Settings' | ||
import React, { | ||
FormEvent, | ||
MouseEvent, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
useRef, | ||
useState, | ||
} from 'react' | ||
|
||
import { settingsContext } from 'src/contexts' | ||
|
||
function Settings(): JSX.Element { | ||
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null) | ||
const formRef = useRef<HTMLFormElement>(null) | ||
const open = useMemo(() => Boolean(anchorEl), [anchorEl]) | ||
const popperRef = useRef<HTMLDivElement>(null) | ||
const handleClick = (event: MouseEvent<HTMLElement>): void => { | ||
setAnchorEl(anchorEl ? null : event.currentTarget) | ||
} | ||
|
||
const { longitude, latitude, setLatitude, setLongitude } = | ||
useContext(settingsContext) | ||
|
||
const id = open ? 'simple-popper' : undefined | ||
|
||
function handleSubmit(e: FormEvent<HTMLFormElement>): void { | ||
e.preventDefault() | ||
const { latitude, longitude } = Object.fromEntries( | ||
new FormData(formRef.current).entries() | ||
) | ||
setLatitude(latitude.toString()) | ||
setLongitude(longitude.toString()) | ||
if (latitude && longitude) { | ||
setAnchorEl(null) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
function handleClickOutside(event: globalThis.MouseEvent): void { | ||
if ( | ||
popperRef.current && | ||
!popperRef.current.contains(event.target as Node) && | ||
!anchorEl?.contains(event.target as Node) | ||
) { | ||
setAnchorEl(null) | ||
} | ||
} | ||
|
||
if (open) { | ||
document.addEventListener('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), | ||
})) | ||
|
||
const Row = styled('div')(({ theme }) => ({ | ||
display: 'flex', | ||
gap: theme.spacing(2), | ||
alignItems: 'center', | ||
})) | ||
|
||
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}> | ||
<Row> | ||
<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, | ||
}} | ||
/> | ||
</Row> | ||
|
||
<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,10 @@ | ||
import { Dispatch, SetStateAction, createContext } from 'react' | ||
|
||
export interface ISettingsContext { | ||
longitude: string | ||
latitude: string | ||
setLongitude: Dispatch<SetStateAction<ISettingsContext['longitude']>> | ||
setLatitude: Dispatch<SetStateAction<ISettingsContext['latitude']>> | ||
} | ||
|
||
export const settingsContext = 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
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