-
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.
add offline support for location fields
- Loading branch information
Showing
6 changed files
with
97 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,24 @@ | ||
import React from "react"; | ||
import { TextField } from "formik-mui"; | ||
import { FastField } from "formik"; | ||
import { TextFieldProps } from "@mui/material"; | ||
import { GooglePlacesAutocompleteField } from "@dylmye/mui-google-places-autocomplete"; | ||
|
||
import { useCustomTheme } from "../../contexts/CustomTheme"; | ||
import poweredByGoogleLightMode from "../../assets/images/powered_by_google_light_mode.png"; | ||
import poweredByGoogleDarkMode from "../../assets/images/powered_by_google_dark_mode.png"; | ||
import LocationField from "../fields/LocationField"; | ||
import styles from "./styles.module.css"; | ||
|
||
const FormStepOne = () => { | ||
const { theme } = useCustomTheme(); | ||
|
||
const googleAttributionHelperText: TextFieldProps = { | ||
helperText: ( | ||
<img | ||
src={ | ||
theme === "light" ? poweredByGoogleLightMode : poweredByGoogleDarkMode | ||
} | ||
alt="This location search field uses Google APIs." | ||
className={styles.googleAttribution} | ||
/> | ||
), | ||
}; | ||
|
||
return ( | ||
<div className={styles.formFieldsContainer}> | ||
<FastField | ||
component={TextField} | ||
fullWidth | ||
name="title" | ||
label="Name of your trip" | ||
/> | ||
<FastField | ||
component={GooglePlacesAutocompleteField} | ||
name="location" | ||
label="Where are you going?" | ||
inputProps={googleAttributionHelperText} | ||
/> | ||
</div> | ||
); | ||
}; | ||
const FormStepOne = () => ( | ||
<div className={styles.formFieldsContainer}> | ||
<FastField | ||
component={TextField} | ||
fullWidth | ||
name="title" | ||
label="Name of your trip" | ||
/> | ||
<FastField | ||
component={LocationField} | ||
name="location" | ||
label="Where are you going?" | ||
/> | ||
</div> | ||
); | ||
|
||
export default FormStepOne; |
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,56 @@ | ||
import React from "react"; | ||
import { TextField } from "formik-mui"; | ||
import { FieldProps } from "formik"; | ||
import { TextFieldProps } from "@mui/material"; | ||
import { GooglePlacesAutocompleteField } from "@dylmye/mui-google-places-autocomplete"; | ||
|
||
import { useOnlineStatus } from "../../../contexts/OnlineStatus"; | ||
import { useCustomTheme } from "../../../contexts/CustomTheme"; | ||
import poweredByGoogleLightMode from "../../../assets/images/powered_by_google_light_mode.png"; | ||
import poweredByGoogleDarkMode from "../../../assets/images/powered_by_google_dark_mode.png"; | ||
import styles from "./styles.module.css"; | ||
|
||
interface LocationFieldProps extends FieldProps { | ||
inputProps?: TextFieldProps; | ||
} | ||
|
||
/** A wrapper around GooglePlacesAutocompleteField with an offline fallback */ | ||
const LocationField = ({ | ||
inputProps = {}, | ||
...props | ||
}: LocationFieldProps & any) => { | ||
const { theme } = useCustomTheme(); | ||
const { online: isOnline } = useOnlineStatus(); | ||
|
||
const googleAttributionHelperText: TextFieldProps = { | ||
helperText: ( | ||
<img | ||
src={ | ||
theme === "light" ? poweredByGoogleLightMode : poweredByGoogleDarkMode | ||
} | ||
alt="This location search field uses Google APIs." | ||
className={styles.googleAttribution} | ||
/> | ||
), | ||
}; | ||
|
||
if (isOnline) { | ||
return ( | ||
<GooglePlacesAutocompleteField | ||
inputProps={{ ...googleAttributionHelperText, ...inputProps }} | ||
{...props} | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<TextField | ||
fullWidth | ||
{...props} | ||
helperText="Location suggestions aren't available offline" | ||
{...inputProps} | ||
/> | ||
); | ||
}; | ||
|
||
export default LocationField; |
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,4 @@ | ||
.googleAttribution { | ||
height: 13px; | ||
width: auto; | ||
} |
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