-
Notifications
You must be signed in to change notification settings - Fork 212
@W-18762700: Implemented address autocomplete dropdown using mock addresses #2589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
harshini-magesh
wants to merge
9
commits into
SalesforceCommerceCloud:develop
Choose a base branch
from
harshini-magesh:W-18698963.harshini-magesh.create_address_suggestion_dropdown
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
52653f0
Implemented address autocomplete dropdown using mock addresses
harshini-magesh f919acd
@W-18762700: Resolved PR comments by renaming and moving files to app…
harshini-magesh 410de36
@W-18762700: Add address suggestion dropdown component with comprehen…
harshini-magesh bb811d3
@W-18762700: Moved and renamed files, added tests, and reverted old c…
harshini-magesh 9dde590
@W-18762700: Removed city/state/country field population logic for cu…
harshini-magesh acd6d99
@W-18762700: properly formatted all files and moved them to appropria…
harshini-magesh 235f0df
@W-18762700: Renamed test files and updated imports
harshini-magesh ced4478
Renamed test file and defined constants
harshini-magesh 47ef052
Remove unintended files from app/components directory
harshini-magesh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
200 changes: 200 additions & 0 deletions
200
packages/template-retail-react-app/app/components/forms/AddressSuggestionDropdown.jsx
This file contains hidden or 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,200 @@ | ||
| /* | ||
| * Copyright (c) 2021, salesforce.com, inc. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
| */ | ||
| import React, {useEffect, useRef} from 'react' | ||
| import PropTypes from 'prop-types' | ||
| import { | ||
| Box, | ||
| Flex, | ||
| Text, | ||
| IconButton, | ||
| VStack, | ||
| HStack, | ||
| Spinner | ||
| } from '@salesforce/retail-react-app/app/components/shared/ui' | ||
| import {CloseIcon} from '@salesforce/retail-react-app/app/components/icons' | ||
|
|
||
| /** | ||
| * Address Suggestion Dropdown Component | ||
| * Displays Google-powered address suggestions in a dropdown format | ||
| */ | ||
| const AddressSuggestionDropdown = ({ | ||
| suggestions = [], | ||
| isLoading = false, | ||
| onClose, | ||
| onSelectSuggestion, | ||
| isVisible = false, | ||
| position = 'absolute' | ||
| }) => { | ||
| const dropdownRef = useRef(null) | ||
|
|
||
| // Handle click outside | ||
| useEffect(() => { | ||
| const handleClickOutside = (event) => { | ||
| if (dropdownRef.current && !dropdownRef.current.contains(event.target)) { | ||
| onClose() | ||
| } | ||
| } | ||
|
|
||
| if (isVisible) { | ||
| document.addEventListener('mousedown', handleClickOutside) | ||
| } | ||
|
|
||
| return () => { | ||
| document.removeEventListener('mousedown', handleClickOutside) | ||
| } | ||
| }, [isVisible, onClose]) | ||
|
|
||
| if (!isVisible || suggestions.length === 0) { | ||
| return null | ||
| } | ||
|
|
||
| if (isLoading) { | ||
| return ( | ||
| <Box | ||
| position="absolute" | ||
| top="100%" | ||
| left={0} | ||
| right={0} | ||
| bg="white" | ||
| border="1px solid" | ||
| borderColor="gray.200" | ||
| borderRadius="md" | ||
| boxShadow="md" | ||
| zIndex={1000} | ||
| p={4} | ||
| > | ||
| <Flex align="center" justify="center"> | ||
| <Spinner size="sm" mr={2} /> | ||
| <Text>Loading suggestions...</Text> | ||
| </Flex> | ||
| </Box> | ||
| ) | ||
| } | ||
|
|
||
| return ( | ||
| <Box | ||
| ref={dropdownRef} | ||
| data-testid="address-suggestion-dropdown" | ||
| position={position} | ||
| top="100%" | ||
| left={0} | ||
| right={0} | ||
| zIndex={1000} | ||
| bg="white" | ||
| border="1px solid" | ||
| borderColor="gray.200" | ||
| borderRadius="md" | ||
| boxShadow="md" | ||
| mt={1} | ||
| maxH="300px" | ||
| overflowY="auto" | ||
| > | ||
| <Flex | ||
| px={4} | ||
| py={2} | ||
| borderBottom="1px solid" | ||
| borderColor="gray.200" | ||
| justifyContent="space-between" | ||
| alignItems="center" | ||
| > | ||
| <Text fontSize="sm" fontWeight="medium" color="gray.600"> | ||
| Suggested | ||
| </Text> | ||
| <IconButton | ||
| aria-label="Close suggestions" | ||
| icon={<CloseIcon />} | ||
| variant="ghost" | ||
| size="sm" | ||
| onClick={onClose} | ||
| /> | ||
| </Flex> | ||
| {suggestions.map((suggestion, index) => ( | ||
| <Box | ||
| key={index} | ||
| px={4} | ||
| py={2} | ||
| cursor="pointer" | ||
| _hover={{bg: 'gray.50'}} | ||
| onClick={() => onSelectSuggestion(suggestion)} | ||
| role="button" | ||
| tabIndex={0} | ||
| onKeyPress={(e) => { | ||
| if (e.key === 'Enter' || e.key === ' ') { | ||
| onSelectSuggestion(suggestion) | ||
| } | ||
| }} | ||
| > | ||
| <Flex alignItems="center" gap={2}> | ||
| {/* Location Marker */} | ||
| <Box position="relative" w={4} h={4}> | ||
| <Box | ||
| position="absolute" | ||
| top={0} | ||
| left={0} | ||
| w={4} | ||
| h={4} | ||
| bg="blue.500" | ||
| borderRadius="full" | ||
| opacity={0.2} | ||
| /> | ||
| <Box | ||
| position="absolute" | ||
| top={1} | ||
| left={1} | ||
| w={2} | ||
| h={2} | ||
| bg="blue.500" | ||
| borderRadius="full" | ||
| /> | ||
| </Box> | ||
|
|
||
| {/* Address Text */} | ||
| <Box flex={1}> | ||
| <Text fontSize="sm" noOfLines={1}> | ||
| {suggestion.mainText} | ||
| </Text> | ||
| {suggestion.secondaryText && ( | ||
| <Text fontSize="xs" color="gray.500" noOfLines={1}> | ||
| {suggestion.secondaryText} | ||
| </Text> | ||
| )} | ||
| </Box> | ||
| </Flex> | ||
| </Box> | ||
| ))} | ||
| </Box> | ||
| ) | ||
| } | ||
|
|
||
| AddressSuggestionDropdown.propTypes = { | ||
| /** Array of address suggestions to display */ | ||
| suggestions: PropTypes.arrayOf( | ||
| PropTypes.shape({ | ||
| id: PropTypes.string, | ||
| address: PropTypes.string, | ||
| mainText: PropTypes.string, | ||
| secondaryText: PropTypes.string | ||
| }) | ||
| ), | ||
|
|
||
| /** Whether the dropdown should be visible */ | ||
| isVisible: PropTypes.bool, | ||
|
|
||
| /** Callback when close button is clicked */ | ||
| onClose: PropTypes.func.isRequired, | ||
|
|
||
| /** Callback when a suggestion is selected */ | ||
| onSelectSuggestion: PropTypes.func.isRequired, | ||
|
|
||
| /** CSS position property for the dropdown */ | ||
| position: PropTypes.oneOf(['absolute', 'relative', 'fixed']), | ||
|
|
||
| /** Whether the dropdown is loading */ | ||
| isLoading: PropTypes.bool | ||
| } | ||
|
|
||
| export default AddressSuggestionDropdown | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this file be moved outside of this folder into a separate one under components/
We want to have a dedicated folder for this dropdown, and then import it here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow current pattern in the soure code for file name. It is snake-case, not camel case.